升级到6.0版后,MvvmCross应用程序将无法启动
我将我的应用程序升级到MvvmCross 6.0版.现在它进入了启动屏幕,什么也没做.通过查看控制台,我可以看到服务正在启动.这是我的app.cs:
I upgraded my app to MvvmCross version 6.0. Now it get's to the splash screen and does nothing else. I can see the the Services are starting up by looking at the console. Here is my 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>();
}
}
这是非常基本的.我已经转换到新的导航系统,因此注册了NavigationNavigationServiceAppStart.那将不再解决,所以我回到了直接的RegisterAppStart.启动屏幕出现,然后停止.万一重要,splashscreen.cs如下:
It is pretty basic. I had converted to the new navigation system hence the RegisterNavigationServiceAppStart. That would no longer resolve so I went back to a straight RegisterAppStart. The splash screen comes up and then it stops. In case it matters, splashscreen.cs is as follows:
[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如下:
It's pretty vanilla, but I know things have changed along the way. My setup.cs is as follows:
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的文档,构造函数不再具有参数,因此我认为没有理由调用它.有人可以帮忙吗?
The only change I made to it was to remove the constructor. Per the documentation for version 6.0, the constructor no longer has parameters so I saw no reason to call it. Can anyone help?
**更新**
我添加了MainApplication.cs,如下所示:
I added a MainApplication.cs as follows:
[Application]
public class MainApplication : MvxAppCompatApplication<Setup, App>
{
public MainApplication(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
}
这使我越过了初始屏幕,但挂断了LoginViewModel的初始化.
This got me past the splash screen, but hung up on the Initialize of the LoginViewModel.
*日志*
也许这会有所帮助.这是事件日志条目:
Maybe this will help. Here are the event log entries:
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()上.我创建了一个测试来演示:
I traced it a little further. It is hanging up on the Initialize() in the view model. I created a test to demonstrate:
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是:
If I set a break point on the two var m = l, It will get to the one in the constructor but will never get to the one in the Initialize. GetLocations is:
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, DateTimeOffset.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调用,它将到达那里.
If you set a break point on Client.Close(), it will get there if called from the constructor but not if its called from Initialize.
我的问题是双重的.首先,当您使用Android升级到6.0版时,现在必须包含MainApplication.cs,如下所示:
My problem was twofold. First, when you upgrade to version 6.0 using Android, you now have to include a MainApplication.cs as follows:
[Application]
public class MainApplication : MvxAppCompatApplication<Setup, App>
{
public MainApplication(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
}
否则,您将被卡在SplashScreen上.其次,您需要知道显示的第一个ViewModel中的Initialize必须是同步的.正如@Ale_lipa提到的那样,MvvmCross作者写了一篇博客文章,解释了为什么这样做以及如何处理.
Without this, you will get stuck on the SplashScreen. Secondly, you need to know that the Initialize in the first ViewModel that you display has to be synchronous. As @Ale_lipa mentioned, an MvvmCross Author wrote a blog post explaining why this is and what to do about it.
简而言之,如果您使用的是SplashScreen,并且确实需要将第一个ViewModel初始化为异步,则可以按如下所示添加CustomAppStart:
In a nutshell, if you are using a SplashScreen and you really need the Initialize for your first ViewModel to be Async, you can add a CustomAppStart as follows:
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中,替换您的:
In you App.cs, replace your:
RegisterAppStart<FirstViewModel>();
具有:
RegisterCustomAppStart<CustomMvxAppStart<FirstViewModel>>();
这将使您的第一个Initialize异步.我只能确定它仅适用于Android,并且仅当您使用SplashScreen时.
This will allow your first Initialize to be async. I am only sure it works for Android and only if you are using a SplashScreen.