简体   繁体   English

Xamarin.Forms中View和ViewModel之间的通信

[英]Communication between View and ViewModel in Xamarin.Forms

i have a problem with ZXing that correctly scan qr codes in my xamarin app, but it does multiple times and it sends to other viewmodels more than one time. 我在ZXing中遇到问题,该问题可以正确扫描我的xamarin应用程序中的二维码,但是它执行了多次,并且多次发送给其他视图模型。 To resolve this problem i use a counter and it works good. 为了解决这个问题,我使用了一个计数器,它很好用。 So i thought to use messenger provided by MvvmLight to send, from view, a message to viewmodel to reset the counter every time my view start the OnAppearing method. 因此,我认为每次我的视图启动OnAppearing方法时,都使用MvvmLight提供的Messenger从视图向视图模型发送消息以重置计数器。 Is it a good way? 这是个好方法吗? Can i do it better in another way? 我可以用其他方式做得更好吗?

This is my OnAppearing method in my view: 在我看来,这是我的OnAppearing方法:

protected override void OnAppearing()
        {
        base.OnAppearing();
        var messageScanActivation = new MessageResetScan();
        Messenger.Default.Send(messageScanActivation);
        Console.WriteLine("i sent reset message");
    }

This is my viewmodel: 这是我的视图模型:

   public ScannerViewModel(INavigationService navigationService, 
  IScanCreatorService scanCreatorService) : base(navigationService)
    {
        _scanCreatorService = scanCreatorService;
        title = "Scan";
        _countScan = 0;
        OnBarcodeScannedCommand = new Command(OnBarcodeScanned);
        _isScanning = true;
        Messenger.Default.Register<MessageResetScan>(this, ResetScan);
    }



    private void OnBarcodeScanned()
    {
        _countScan++;
        _isAnalyzing = false;
        Device.BeginInvokeOnMainThread(() =>
            {
                if (_countScan == 1)
                {
                    Debug.WriteLine("RisultatoScansione: " + Result.Text);
                    UpdateHistoryScans(Result.Text);
                    Debug.WriteLine("currentKey: " + _navigationService.CurrentPageKey);
                    DisplayScanAcquiredPrompt();
                    _navigationService.GoBack();
                }           

            });
        _isAnalyzing = true;

    }

    private void ResetScan(MessageResetScan message)
    {
        Console.WriteLine("I received reset message! CountScan: " + _countScan);
        _countScan = 0;
    }

Messaging is one way, another MVVM friendly way is to listen for viewmodel data changes in the view and then trigger code behind. 消息传递是一种方式,另一种对MVVM友好的方式是侦听视图中的视图模型数据更改,然后触发背后的代码。

Using David's excellent behaviors library and when the data comes in from the rest service the view triggers the code behind when the data changes. 使用David出色的行为库,当数据来自其余服务时,视图将在数据更改时触发代码。

<ContentPage.Behaviors>
<b:DataChangedBehavior Binding="{Binding Position}" ComparisonCondition="NotEqual" Value="{x:Null}">
  <b:InvokeMethodAction TargetObject="{Binding Source={x:Reference GeoposView}}"
                        MethodName="MapRefresh" />
</b:DataChangedBehavior>

Here is a similar discusion about this question. 这是关于这个问题的类似讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM