Xamarin.Forms MVVM Light IDialogService ShowMessage抛出本机异常 [英] Xamarin.Forms MVVM Light IDialogService ShowMessage throwing native exception
问题描述
我最近开始使用MVVM Light,并且第一次使用IDialogService.
i recently started working with MVVM Light and just used the IDialogService for the first time.
我的ViewModelLocator
My ViewModelLocator
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
// Register the dialog service provided by mvvm light
SimpleIoc.Default.Register<IDialogService, DialogService>();
我的ViewModel
My ViewModel
private IRestService _restService;
public OrderViewModel (IDialogService dialogService )
{
_dialogService = dialogService;
}
最后使用ShowMessage:
And finally using 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
有人可以告诉我怎么回事吗?
Can someone tell me whats going on ?
推荐答案
我认为问题是尚未设置当前活动.问题是,MvvmLight需要CurrentActivity
属于ActivityBase
类型.因此,我认为DialogService
不能真正与Forms兼容,因为Forms要求Activity从FormsAppCompatActivity
继承.那将构成一个很好的菱形继承,这是不可能的,因为C#不支持多重继承.
I think the problem is, that current activity has not been set. The problem is, that MvvmLight needs CurrentActivity
to be of type ActivityBase
. So i think the DialogService
not really compatible with Forms, because Forms requires the Activity to inherit from FormsAppCompatActivity
. That would make up a nice diamond shaped inheritance, which is not possible, because C# doesn't support multiple inheritance.
因此您可以使用ACR用户Daialog: https://www.nuget.org/packages /Acr.UserDialogs/
So you could use ACR User Daialogs: https://www.nuget.org/packages/Acr.UserDialogs/
请参见 https://github.com/aritchie/userdialogs #android-initialization-in-your-main-activity 进行设置.
您可以注册
SimpleIoc.Default.Register<IUserDialogs>(UserDialogs.Instance);
这篇关于Xamarin.Forms MVVM Light IDialogService ShowMessage抛出本机异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!