Xamarin.窗体MVVM Light IDialogService ShowMessage引发本机异常



i最近开始使用MVVM Light,并且只是第一次使用IDialogService。

我的ViewModelLocator

ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
// Register the dialog service provided by mvvm light
SimpleIoc.Default.Register<IDialogService, DialogService>();

我的ViewModel

private IRestService _restService;
public OrderViewModel (IDialogService dialogService )
{ 
   _dialogService = dialogService;
}

最后使用ShowMessage:

await _dialogService.ShowMessage("test", "test", "Ok", "Nop", (result) => { 
   if (result)
   {
      //...
   }
   else
   {
      //...
   }
});

导致此异常的原因:

Java.Lang.NullPointerException: Attempt to invoke virtual method 
'android.content.res.Resources$Theme android.content.Context.getTheme()'
on a null object reference 

有人能告诉我发生了什么事吗?

我认为问题是,当前活动尚未设置。问题是MvvmLight需要CurrentActivityActivityBase类型。所以我认为DialogService实际上与Forms不兼容,因为Forms要求Activity从FormsAppCompatActivity继承。这将构成一个很好的菱形继承,这是不可能的,因为C#不支持多重继承。

因此,您可以使用ACR用户日志:https://www.nuget.org/packages/Acr.UserDialogs/

请参阅https://github.com/aritchie/userdialogs#android-在安装程序的主要活动中初始化。

你可以注册

SimpleIoc.Default.Register<IUserDialogs>(UserDialogs.Instance);

最新更新