Using MvvmCross from content providers and activities

我怕爱的太早我们不能终老 提交于 2019-12-19 03:41:10

问题


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.

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

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.

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.

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.

My App Class

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();
    }
}

And my setup class

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();
    }

My Content provider's OnCreate():

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

My MvxActivity:

[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);
    }
}

回答1:


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

  • 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

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!)



来源:https://stackoverflow.com/questions/15862980/using-mvvmcross-from-content-providers-and-activities

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!