使用来自内容提供商和活动的MvvmCross [英] Using MvvmCross from content providers and activities

查看:67
本文介绍了使用来自内容提供商和活动的MvvmCross的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的一个由活动,内容提供者和广播接收者组成的应用程序中使用MvvmCross v3.但是,我不太成功.

I am trying to use MvvmCross v3 in one of my applications which consists of activities, content providers and broadcast receivers. However, I am not quite succeeding.

该应用程序由一个包含逻辑,模型和视图模型的Core PCL和一个包含所有MonoDroid特定内容的Droid应用程序组成.

The application consists of a Core PCL which contains logic, models and viewmodels and a Droid application which contains all MonoDroid-specific stuff.

在Core中,我有一个App:MvxApplication类,在Droid中,我有一个Setup:MvxSetup类,该类创建一个App实例并初始化内容.

In Core I have an App:MvxApplication class and in Droid I have a Setup:MvxSetup class which creates an App-instance and initialises stuff.

我可以将IOC部件与内容提供商,广播接收器和非Mvx活动一起使用,而不会出现问题.现在,当我想添加MvxActivity时,它会崩溃.

I can use the IOC parts with content providers, broadcast receivers and non-Mvx-activities without problems. When I now want to add an MvxActivity it falls apart.

启动Mvx活动时,出现异常"Cirrious.CrossCore.Exceptions.MvxException:MvxTrace已初始化".

When the Mvx Activity launches I get an exception "Cirrious.CrossCore.Exceptions.MvxException: MvxTrace already initialized".

很明显,我是在错误的顺序/错误的位置初始化事物.但是,我需要一个正确方向的指针.

Obviously I am initialising things in the wrong order / wrong place. But, I need a pointer in the right direction.

我的应用程序类

public class App
    : MvxApplication
{
    public override void Initialize()
    {
        base.Initialize();
        InitialisePlugins();
        InitaliseServices();
        InitialiseStartNavigation();
    }

    private void InitaliseServices()
    {
        CreatableTypes().EndingWith("Service").AsInterfaces().RegisterAsLazySingleton();
    }

    private void InitialiseStartNavigation()
    {            
    }

    private void InitialisePlugins()
    {
        // initialise any plugins where are required at app startup
        // e.g. Cirrious.MvvmCross.Plugins.Visibility.PluginLoader.Instance.EnsureLoaded();
    }
}

我的设置程序

public class Setup
    : MvxAndroidSetup
{
    public Setup(Context applicationContext)
        : base(applicationContext)
    {
    }

    protected override IMvxApplication CreateApp()
    {
        return new App();
    }

    protected override IMvxNavigationSerializer CreateNavigationSerializer()
    {
        return new MvxJsonNavigationSerializer();
    }

    public override void LoadPlugins(Cirrious.CrossCore.Plugins.IMvxPluginManager pluginManager)
    {
        pluginManager.EnsurePluginLoaded<Cirrious.MvvmCross.Plugins.Json.PluginLoader>();
        base.LoadPlugins(pluginManager);
    }

    public void RegisterServices()
    {
        // I register a bunch of singletons here
    }

    // The following is called from my content provider's OnCreate()
    // Which is the first code that is run
    public static void DoSetup(Context applicationContext)
    {
        var setup = new Setup(applicationContext);
        setup.Initialize();
        setup.RegisterServices();
    }

我的内容提供商的OnCreate():

My Content provider's OnCreate():

    public override bool OnCreate()
    {
        Log.Debug(Tag, "OnCreate");
        _context = Context;
        Setup.DoSetup(_context);
        return true;
    }

我的Mvx活动:

[Activity(Label = "@string/ApplicationName", MainLauncher = true)]
[IntentFilter(new[] { "Settings" })]
public class SettingsView 
    : MvxActivity
{
    public new SettingsViewModel ViewModel
    {
        get { return (SettingsViewModel) base.ViewModel; }
        set { base.ViewModel = value; }
    }

    protected override void OnViewModelSet()
    {
        SetContentView(Resource.Layout.Page_SettingsView);
    }
}

推荐答案

简短答案(我在手机上的机场)

Short answer (I'm in an airport on mobile)

  • 所有mvx android视图将检查设置单例是否已创建-

  • all the mvx android views will check the setup singleton has been created - https://github.com/slodge/MvvmCross/blob/vnext/Cirrious/Cirrious.MvvmCross.Droid/Platform/MvxAndroidSetupSingleton.cs (vnext tree - but similar on v3)

因此,如果您正在创建设置,但未设置此单例,那么您将在首次显示视图时创建第二个设置

so if you are creating a setup, but not setting this singleton, then you will get a second setup created when you first show a view

我怀疑您只能通过singleton类创建设置,但是如果这不能满足您的需要,请在github上记录问题

i suspect you can just get your setup created via the singleton class, but if this isn't flexible enough for your needs, then please log an issue on github

我也希望看到一些有关此的博客-我没有使用定制内容提供商(根本没有!)

would also love to see some blogging about this - I've not used custom content providers much (at all!)

这篇关于使用来自内容提供商和活动的MvvmCross的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆