4

アクティビティ、コンテンツ プロバイダー、ブロードキャスト レシーバーで構成されるアプリケーションの 1 つで MvvmCross v3 を使用しようとしています。しかし、私はあまり成功していません。

アプリケーションは、ロジック、モデル、ビューモデルを含む Core PCL と、MonoDroid 固有のものをすべて含む Droid アプリケーションで構成されます。

Core には App:MvxApplication クラスがあり、Droid には Setup:MvxSetup クラスがあり、App-instance を作成して初期化します。

コンテンツ プロバイダー、ブロードキャスト レシーバー、Mvx 以外のアクティビティで IOC パーツを問題なく使用できます。MvxActivity を追加しようとすると、バラバラになります。

Mvx アクティビティが起動すると、「Cirious.CrossCore.Exceptions.MvxException: MvxTrace は既に初期化されています」という例外が発生します。

明らかに、間違った順序/間違った場所で物事を初期化しています。しかし、正しい方向へのポインターが必要です。

私のアプリクラス

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():

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

私の 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);
    }
}
4

1 に答える 1