我有一个 ZXing 扫描仪页面,我想从 ScanPageModel 获取扫描数据到 MyPageModel
我正在从 MyPageModel 打开扫描页面
public Command ScanCode
{
get
{
return new Command(async () =>
{
MessagingCenter.Subscribe<MyPageModel>(this, "ScanText", (value) =>
{
});
await CoreMethods.PushPageModel<ScanPageModel>();
});
}
}
从 ScanPageModel 获取扫描结果后发送消息中心数据
private void ScanResultAction(object obj)
{
// Prevent multiple event triggers from triggering the navigation multiple times
if (NavigatingAway)
return;
NavigatingAway = true;
var result = obj as ZXing.Result;
var format = result?.BarcodeFormat.ToString() ?? string.Empty;
var value = result?.Text ?? string.Empty;
// Navigate to a page based on value
SampleListData sa = new SampleListData();
sa.Name = value;
Device.BeginInvokeOnMainThread(async () =>
{
MessagingCenter.Send<ScanPageModel, string>(this, "ScanText", result.Text);
await CoreMethods.PopPageModel();
});
}
但是从 ScanPageModel 发送数据后我的消息中心没有调用这是接收扫描代码的订阅方法
public MyPageModel()
{
MessagingCenter.Subscribe<SampleListData>(this, "ScanText", (value) =>
{
});
}
我也试着把这个订阅放在里面
public override void Init(object initData)
{
MessagingCenter.Subscribe<SampleListData>(this, "ScanText", (value) =>
{
});
}
但它也不起作用那么如何解决这个问题?