Xamarin.Forms 和 MvvmCross Messenger 插件上的 NRE
NRE on Xamarin.Forms and MvvmCross Messenger Plugin
我正在尝试使用 MvvmCross Messenger 插件,但我简单地让它工作了..它总是 return "Null Reference Exception".
这是我创建的用于测试它的 BaseViewModel:
namespace TestProject.Core.ViewModels
{
public class BaseViewModel : MvxViewModel
{
private readonly IMvxMessenger _messenger;
public BaseViewModel(IMvxMessenger messenger)
{
messenger = _messenger;
}
public IMvxCommand TestMessageCommand
{
get { return new MvxCommand(DoTestMessage); }
}
private void DoTestMessage()
{
var message = new TestMessage(this, "Potato");
_messenger.Publish(message);
}
}
}
这是应该接收消息的另一个 ViewModel:
namespace TestProject.Core.ViewModels
{
public class HomeViewModel : MvxViewModel
{
private string _testMessage = string.Empty;
private readonly MvxSubscriptionToken _token;
public HomeViewModel(IMvxMessenger messenger)
{
_token = messenger.Subscribe<TestMessage>(OnTestMessage);
}
private void OnTestMessage(OnTestMessage testMessage)
{
_testMessage = testMessage.Result;
}
public ICommand ShowBasePageCommand
{
get { return new MvxCommand(() => ShowViewModel<BaseViewModel>()); }
}
}
}
最后,这是消息:
namespace TestProject.Core.Messages
{
public class TestMessage
: MvxMessage
{
public QRCodeResultMessage(object sender, string result) : base(sender)
{
Result = result;
}
public string Result { get; private set; }
}
}
我将主页上的一个按钮绑定到 "ShowBasePageCommand",而在 BasePage 上还有另一个按钮绑定到 "TestMessageCommand"。
完全异常:
System.NullReferenceException: Object reference not set to an nstance of an object.
at TestProject.Core.ViewModels.BaseViewModel.DoTestMessage () [0x00014] in /Users/diegopatrocinio/Projects/Xamarin/TestProject/TestProject.Core/ViewModels/BaseViewModel.cs:49
at MvvmCross.Core.ViewModels.MvxCommand.Execute (System.Object parameter) [0x00009] in <69bce0378e8e413982d3b552d7e387a8>:0
at Xamarin.Forms.Button.Xamarin.Forms.IButtonController.SendClicked () [0x0000a] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\Button.cs:121
at Xamarin.Forms.Platform.Android.ButtonRenderer+ButtonClickListener.OnClick (Android.Views.View v) [0x0000f] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\Renderers\ButtonRenderer.cs:303
at Android.Views.View+IOnClickListenerInvoker.n_OnClick_Landroid_view_View_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_v) [0x00011] in /Users/builder/data/lanes/4009/3a62f1ea/source/monodroid/src/Mono.Android/platforms/android-25/src/generated/Android.Views.View.cs:1857
at at (wrapper dynamic-method) System.Object:1b16fb3a-f768-4a9f-8e2e-60f0085ed7fb (intptr,intptr,intptr)
堆栈:
System.Diagnostics.Debugger.Mono_UnhandledException_internal() in
System.Diagnostics.Debugger.Mono_UnhandledException(System.NullReferenceException ex) in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/corlib/System.Diagnostics/Debugger.cs:122
object.1b16fb3a-f768-4a9f-8e2e-60f0085ed7fb( arg0, arg1, arg2) in
TestProject.Core.ViewModels.BaseViewModel.DoTestMessage() in /Users/diegopatrocinio/Projects/Xamarin/TestProject/TestProject.Core/ViewModels/BaseViewModel.cs:49
请注意,您的构造函数已反转参数和 class 成员:
public BaseViewModel(IMvxMessenger messenger)
{
messenger = _messenger;
}
应该是
public BaseViewModel(IMvxMessenger messenger)
{
_messenger = messenger;
}
我正在尝试使用 MvvmCross Messenger 插件,但我简单地让它工作了..它总是 return "Null Reference Exception".
这是我创建的用于测试它的 BaseViewModel:
namespace TestProject.Core.ViewModels
{
public class BaseViewModel : MvxViewModel
{
private readonly IMvxMessenger _messenger;
public BaseViewModel(IMvxMessenger messenger)
{
messenger = _messenger;
}
public IMvxCommand TestMessageCommand
{
get { return new MvxCommand(DoTestMessage); }
}
private void DoTestMessage()
{
var message = new TestMessage(this, "Potato");
_messenger.Publish(message);
}
}
}
这是应该接收消息的另一个 ViewModel:
namespace TestProject.Core.ViewModels
{
public class HomeViewModel : MvxViewModel
{
private string _testMessage = string.Empty;
private readonly MvxSubscriptionToken _token;
public HomeViewModel(IMvxMessenger messenger)
{
_token = messenger.Subscribe<TestMessage>(OnTestMessage);
}
private void OnTestMessage(OnTestMessage testMessage)
{
_testMessage = testMessage.Result;
}
public ICommand ShowBasePageCommand
{
get { return new MvxCommand(() => ShowViewModel<BaseViewModel>()); }
}
}
}
最后,这是消息:
namespace TestProject.Core.Messages
{
public class TestMessage
: MvxMessage
{
public QRCodeResultMessage(object sender, string result) : base(sender)
{
Result = result;
}
public string Result { get; private set; }
}
}
我将主页上的一个按钮绑定到 "ShowBasePageCommand",而在 BasePage 上还有另一个按钮绑定到 "TestMessageCommand"。
完全异常:
System.NullReferenceException: Object reference not set to an nstance of an object.
at TestProject.Core.ViewModels.BaseViewModel.DoTestMessage () [0x00014] in /Users/diegopatrocinio/Projects/Xamarin/TestProject/TestProject.Core/ViewModels/BaseViewModel.cs:49
at MvvmCross.Core.ViewModels.MvxCommand.Execute (System.Object parameter) [0x00009] in <69bce0378e8e413982d3b552d7e387a8>:0
at Xamarin.Forms.Button.Xamarin.Forms.IButtonController.SendClicked () [0x0000a] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\Button.cs:121
at Xamarin.Forms.Platform.Android.ButtonRenderer+ButtonClickListener.OnClick (Android.Views.View v) [0x0000f] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\Renderers\ButtonRenderer.cs:303
at Android.Views.View+IOnClickListenerInvoker.n_OnClick_Landroid_view_View_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_v) [0x00011] in /Users/builder/data/lanes/4009/3a62f1ea/source/monodroid/src/Mono.Android/platforms/android-25/src/generated/Android.Views.View.cs:1857
at at (wrapper dynamic-method) System.Object:1b16fb3a-f768-4a9f-8e2e-60f0085ed7fb (intptr,intptr,intptr)
堆栈:
System.Diagnostics.Debugger.Mono_UnhandledException_internal() in
System.Diagnostics.Debugger.Mono_UnhandledException(System.NullReferenceException ex) in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/corlib/System.Diagnostics/Debugger.cs:122
object.1b16fb3a-f768-4a9f-8e2e-60f0085ed7fb( arg0, arg1, arg2) in
TestProject.Core.ViewModels.BaseViewModel.DoTestMessage() in /Users/diegopatrocinio/Projects/Xamarin/TestProject/TestProject.Core/ViewModels/BaseViewModel.cs:49
请注意,您的构造函数已反转参数和 class 成员:
public BaseViewModel(IMvxMessenger messenger)
{
messenger = _messenger;
}
应该是
public BaseViewModel(IMvxMessenger messenger)
{
_messenger = messenger;
}