NRE在Xamarin.Forms和MvvmCross Messenger插件上

时间:2017-03-24 19:55:46

标签: mvvm xamarin.android xamarin.forms mvvmcross nullreferenceexception

我试图使用MvvmCross Messenger插件,但我简单地让它工作..它总是返回一个"空引用异常"。

这是我为测试而创建的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; }
 }
}

我将HomePage上的一个按钮绑定到&#34; ShowBasePageCommand&#34;,并且在BasePage上有另一个按钮绑定到&#34; TestMessageCommand&#34;。

完全例外:

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

1 个答案:

答案 0 :(得分:2)

请注意,您的构造函数已反转参数和类成员:

public BaseViewModel(IMvxMessenger messenger)
{
    messenger = _messenger;
}

应该是

public BaseViewModel(IMvxMessenger messenger)
{
    _messenger = messenger;
}