0

I'm trying to use the MvvmCross Messenger plugin, but I simple get it to work.. it always return a "Null Reference Exception".

Here is the BaseViewModel I created to test it:

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);
    }

   }
 }

Here is the other ViewModel that should receive the message:

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>()); }
    }

   }
 }

And finally, here is the message:

namespace TestProject.Core.Messages
{
public class TestMessage 
    : MvxMessage
{
    public QRCodeResultMessage(object sender, string result) : base(sender)
    {
        Result = result;
    }
    public string Result { get; private set; }
 }
}

I bound a button on the HomePage to the "ShowBasePageCommand", and on the BasePage there is another button bound to the "TestMessageCommand".

Full Exception:

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)

Stack:

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
Diego Patrocinio
  • 100
  • 1
  • 4
  • 13

1 Answers1

2

Please note that your constructor has inverted the parameter and the class member:

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

Should be

public BaseViewModel(IMvxMessenger messenger)
{
    _messenger = messenger;
}
nmilcoff
  • 1,084
  • 5
  • 20
  • That was really it.. I think I've been coding for too long and didn't pay attention on that detail, I checked everything but this lol.. Now I miss ReSharper even more, hope they release it for Visual Studio on Mac. Thanks man! – Diego Patrocinio Mar 24 '17 at 20:15
  • You can always use Rider on mac if you want the R# feeling – Cheesebaron Mar 24 '17 at 20:16
  • @Cheesebaron the problem with Rider is the lack of the live preview for editing XAML, which helps me a lot of, adding it would be perfect, or just releasing ReSharper for VisualStudio.. – Diego Patrocinio Mar 24 '17 at 20:24