Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了xamarin.android – 升级到6.0版后,MvvmCross App无法启动大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我将我的应用程序升级到MvvmCross 6.0版.现在它可以进入启动画面并且不做任何其他事情.通过查看控制台,我可以看到服务正在启动.这是我的app.cs:
public class App : MvxApplication
{
    public override void Initialize()
    {
        MPs.ApplicationName = SetTings.ApplicationName;
        EventLog.ApplicationName = SetTings.ApplicationName;
        BlobCache.ApplicationName = SetTings.ApplicationName;

        CreatableTypes()
            .EndingWith("service")
            .AsInterfaces()
            .RegisterAsLazySingleton();

        CreatableTypes()
            .EndingWith("Singleton")
            .AsInterfaces()
            .RegisterAsSingleton();

        //RegisterAppStart(new CustomAppStart());
        //RegisterNavigationserviceAppStart<Loginviewmodel>();
        RegisterAppStart<Loginviewmodel>();
    }

}

这是非常基本的.我已经转换为新的导航系统因此RegisterNavigationserviceAppStart.那将不再解决,所以我回到了直接的RegisterAppStart.启动画面出现然后停止.如果重要,splashscreen.cs如下:

[Activity(
    Label = "@String/ApplicationName",MainLauncher = true,Icon = "@drawable/icon",Theme = "@style/Theme.Splash",NoHistory = truE)]
    //,ScreenOrientation = ScreenOrientation.LandscapE)]
public class SplashScreen : MvxSplashScreenActivity
{
    public SplashScreen()
        : base(resource.Layout.SplashScreen)
    {
    }
}

这很香草,但我知道事情已经发生了变化.我的setup.cs如下:

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

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

    //protected override IMvxTrace CreateDebugTrace()
    //{
    //    return new DebugTrace();
    //}

    protected override IMvxAndroidViewPresenter CreateViewPresenter()
    {
        return new MvxAppCompatViewPresenter(AndroidViewAssemblies);
    }

    protected override void FillValueConverters(IMvxValueConverterRegistry registry)
    {
        base.FillValueConverters(registry);
        registry.AddOrOverwrite("DateToStringConverter",new DateToStringConverter());
        registry.AddOrOverwrite("FloatToStringConverter",new FloatToStringConverter());
        registry.AddOrOverwrite("decimalToStringConverter",new decimalToStringConverter());
        registry.AddOrOverwrite("BoolToViewStatesConverter",new BoolToViewStatesValueConverter());
        registry.AddOrOverwrite("ShipmentToOriginConverter",new ShipmentToOriginConverter());
        registry.AddOrOverwrite("ShipmentToDesTinationConverter",new ShipmentToDesTinationConverter());
        //registry.AddOrOverwrite("Customname2",new AnotherVerySpecialValueConverter("Summer"));
    }

    protected override void FillTargetFactories(MvvmCross.binding.bindings.Target.Construction.IMvxTargetBindingFactoryRegistry registry)
    {
        base.FillTargetFactories(registry);
        registry.RegisterCustomBindingFactory<EditText>("FocusText",textView => new MvxEditTextFocusBinding(textView));
        registry.RegisterCustomBindingFactory<TextView>("FocusChange",textView => new MvxTextViewFocusChangeBinding(textView));
        //registry.RegisterCustomBindingFactory<MvxSpinner>("ItemSELEcted",//                                          spinner => new MvxSpinnerItemSELEctedBinding(spinner));
    }

    protected override IEnumerable<Assembly> AndroidViewAssemblies => new List<Assembly>(base.AndroidViewAssemblies)
    {
        typeof(MvvmCross.Droid.Support.V7.RecyclerView.MvxRecyclerView).Assembly
    };

}

我对它做的唯一改变是删除构造函数.根据6.0版的文档,构造函数不再有参数,所以我没有理由调用它.有人可以帮忙吗?

**更新**

添加了@L_543_9@mainApplication.cs,如下所示:

[Application]
public class MainApplication : MvxAppCompatApplication<Setup,App>
{
    public MainApplication(IntPtr javaReference,JniHandleownership transfer) : base(javaReference,transfer)
    {
    }
}

这让我超越了启动画面,但挂起了Loginviewmodel的Initialize.

*日志*

也许这会有所帮助.以下是事件日志条目:

2018-04-17 12:17:06 [TRACE] (MvvmCross.Core.MvxSetup) Setup: PriMary start
2018-04-17 12:17:06 [TRACE] (MvvmCross.Core.MvxSetup) Setup: FirstChance start
2018-04-17 12:17:06 [TRACE] (MvvmCross.Core.MvxSetup) Setup: Platformservices start
2018-04-17 12:17:06 [TRACE] (MvvmCross.Core.MvxSetup) Setup: MvvmCross setTings start
2018-04-17 12:17:06 [TRACE] (MvvmCross.Core.MvxSetup) Setup: Singleton Cache start
2018-04-17 12:17:06 [TRACE] (MvvmCross.Core.MvxSetup) Setup: ViewDispatcher start
2018-04-17 12:17:06 [TRACE] (MvvmCross.Core.MvxSetup) Setup: Bootstrap actions
2018-04-17 12:17:07 [TRACE] (MvvmCross.Logging.MvxLog) No view model type finder available - assuming we are looking for a splash screen - returning null
2018-04-17 12:17:07 [TRACE] (MvvmCross.Core.MvxSetup) Setup: StringToTypeParser start
2018-04-17 12:17:07 [TRACE] (MvvmCross.Core.MvxSetup) Setup: CommandHelper start
2018-04-17 12:17:07 [TRACE] (MvvmCross.Core.MvxSetup) Setup: PluginManagerFramework start
2018-04-17 12:17:07 [TRACE] (MvvmCross.Core.MvxSetup) Setup: Create App
2018-04-17 12:17:07 [TRACE] (MvvmCross.Core.MvxSetup) Setup: Navigationservice
2018-04-17 12:17:07 [TRACE] (MvvmCross.Core.MvxSetup) Setup: Load navigation routes
2018-04-17 12:17:07 [TRACE] (MvvmCross.Core.MvxSetup) Setup: App start
2018-04-17 12:17:07 [TRACE] (MvvmCross.Core.MvxSetup) Setup: Application Initialize - On BACkground thread
2018-04-17 12:17:08 [TRACE] (MvvmCross.Core.MvxSetup) Setup: viewmodelTypeFinder start
2018-04-17 12:17:08 [TRACE] (MvvmCross.Core.MvxSetup) Setup: ViewsContainer start
2018-04-17 12:17:08 [TRACE] (MvvmCross.Core.MvxSetup) Setup: Views start
2018-04-17 12:17:08 [TRACE] (MvvmCross.Core.MvxSetup) Setup: CommandCollectionBuilder start
2018-04-17 12:17:08 [TRACE] (MvvmCross.Core.MvxSetup) Setup: Navigationserializer start
2018-04-17 12:17:08 [TRACE] (MvvmCross.Core.MvxSetup) Setup: InpcInterception start
2018-04-17 12:17:08 [TRACE] (MvvmCross.Core.MvxSetup) Setup: InpcInterception start
2018-04-17 12:17:08 [TRACE] (MvvmCross.Core.MvxSetup) Setup: LastChance start
2018-04-17 12:17:08 [TRACE] (MvvmCross.Core.MvxSetup) Setup: Secondary end
2018-04-17 12:17:08 [TRACE] (MvvmCross.Logging.MvxLog) AppStart: Application Startup - On UI thread

我进一步追踪它.它挂在视图模型中的Initialize()上.我创建了一个测试来演示:

public class Firstviewmodel : Mvxviewmodel
{

    public Firstviewmodel()
    {
        Task.Run(async () =>
        {
            var l = await ListDatasource.GetLOCATIOns();
            var m = l;
        });
    }

    public async override Task Initialize()
    {
        await base.Initialize();
        var l = await ListDatasource.GetLOCATIOns();
        var m = l;
    }

}

如果我在两个var m = l上设置一个断点,它将到达构造函数中的断点,但永远不会到达Initialize中的断点. GetLOCATIOns是:

public async static Task<LOCATIOnList> GetLOCATIOns()
    {
        ListserviceClient client = NewClient();

        LOCATIOnList ret = null;
        bool TryCache = false;

        try
        {
            //ret = await client.GetLOCATIOnListAsync();
            ret = await Task<LOCATIOnList>.Factory.FromAsync((asyncCallBACk,asyncStatE) => client.beginGetLOCATIOnList(asyncCallBACk,asyncStatE),(asyncResult) => client.EndGetLOCATIOnList(asyncResult),null);

            client.Close();
            await BlobCache.LocalMachine.InsertObject("LOCATIOns",ret,datetiR_736_11845@eOffset.Now.AddDays(SetTings.CacheDays));
        }
        catch (TimeoutException eX)
        {
            client.Abort();
            EventLog.Error(ex.ToString());
            TryCache = true;
        }
        catch (CommunicationException eX)
        {
            client.Abort();
            EventLog.Error(ex.ToString());
            TryCache = true;
        }
        catch (Exception eX)
        {
            EventLog.Error(ex.ToString());
            TryCache = true;
        }

        if (TryCachE)
        {
            try
            {
                ret = await BlobCache.LocalMachine.GetObject<LOCATIOnList>("LOCATIOns");
            }
            catch (KeyNotFoundException)
            {
                ret = null;
            }
        }

        return ret;
    }

如果在Client.Close()上设置一个断点,如果从构造函数调用它,它将到达那里,但如果从Initialize调用则不会.

解决方法

我的问题是双重的.首先,当您使用Android升级到6.0版时,您现在必须包含MainApplication.cs,如下所示:
[Application]
public class MainApplication : MvxAppCompatApplication<Setup,transfer)
    {
    }
}

如果没有这个,你将被困在SplashScreen上.其次,您需要知道您显示的第一个viewmodel中的Initialize必须是同步的.正如@Ale_lipa所提到的,一位MvvmCross作者撰写了一篇博客文章,解释了为什么会这样做以及如何处理它.

https://nicksnettravels.builttoroam.com/post/2018/04/19/MvvmCross-Initialize-method-on-the-first-view-model.aspx

简而言之,如果您使用的是SplashScreen,并且您确实需要将第一个viewmodel的Initialize作为Async,则可以按如下方式添加CustomAppStart:

public class CustomMvxAppStart<Tviewmodel> : MvxAppStart<Tviewmodel>
     where Tviewmodel : IMvxviewmodel
{
     public CustomMvxAppStart(IMvxApplication application,IMvxNavigationservice navigationservicE) : base(application,navigationservicE)
     {
     }

    protected override void NavigateToFirstviewmodel(object hint)
     {
         Navigationservice.Navigate<Tviewmodel>();
     }
}

你的App.cs中,替换你的

RegisterAppStart<Firstviewmodel>();

有:

RegisterCustomAppStart<CustomMvxAppStart<Firstviewmodel>>();

这将允许您的第一个Initialize异步.我只确定它适用于Android,并且只有在使用SplashScreen时才有效.

大佬总结

以上是大佬教程为你收集整理的xamarin.android – 升级到6.0版后,MvvmCross App无法启动全部内容,希望文章能够帮你解决xamarin.android – 升级到6.0版后,MvvmCross App无法启动所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。