gpt4 book ai didi

mvvm - Xamarin.Forms 和 MvvmCross Messenger 插件上的 NRE

转载 作者:行者123 更新时间:2023-12-03 11:03:29 25 4
gpt4 key购买 nike

我正在尝试使用 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 上的一个按钮绑定(bind)到“ShowBasePageCommand”,而在 BasePage 上还有另一个按钮绑定(bind)到“TestMessageCommand”。

完全异常(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)

堆:
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

最佳答案

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

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

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

关于mvvm - Xamarin.Forms 和 MvvmCross Messenger 插件上的 NRE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43008097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com