【问题标题】:Xamarin.Forms MVVM Light IDialogService ShowMessage throwing native exceptionXamarin.Forms MVVM Light IDialogService ShowMessage 引发本机异常
【发布时间】:2016-08-31 07:31:30
【问题描述】:

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

我的 ViewModelLocator

ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

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

我的视图模型

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 

谁能告诉我这是怎么回事?

【问题讨论】:

标签: c# mvvm xamarin xamarin.forms mvvm-light


【解决方案1】:

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

所以您可以使用 ACR 用户对话:https://www.nuget.org/packages/Acr.UserDialogs/

有关设置,请参阅 https://github.com/aritchie/userdialogs#android-initialization-in-your-main-activity

你可以注册它

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

【讨论】:

  • 好吧,我以前使用过 acr userdialogs,我只是想我可以使用 mvvm light 中已经实现的用户对话框。
猜你喜欢
  • 1970-01-01
  • 2019-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-09
  • 1970-01-01
  • 2015-09-01
相关资源
最近更新 更多