简体   繁体   English

具有 CommunityToolkit ObservableProperty 的毛伊岛在处理时未更新

[英]Maui with CommunityToolkit ObservableProperty not updating while processing

I have a Maui app only running on Android (Zebra Scan-gun).我有一个仅在 Android(斑马扫描枪)上运行的毛伊岛应用程序。

When I tap a button on the screen the process that runs may take 30 seconds to complete.当我点击屏幕上的按钮时,运行的过程可能需要 30 秒才能完成。 During those 30 seconds I update the ObservableProperty's value to give updates to the user as processing continues.在这 30 秒内,我更新 ObservableProperty 的值,以便在处理继续时向用户提供更新。 Currently, the result is that the UI does not refresh until it returns control to the screen.目前,结果是 UI 在将控制权返回给屏幕之前不会刷新。

As I am using CommunityToolkit.Mvvm the SetProperty is called by internal code which I would expect to refresh the UI.当我使用CommunityToolkit.Mvvm时, SetProperty由我希望刷新 UI 的内部代码调用。

Here's a bits of relevant code:这是一些相关代码:

    using CommunityToolkit.Mvvm.ComponentModel;
    using CommunityToolkit.Mvvm.Input;
    namespace myNamespace 
    {
       public partical class MyClassVM: ObservableObject
       {
            [ObservableProperty]
            string status;
    
            [RelayCommand]
            void DoStuff()
            {
              var myUseCase = new DoStuffUseCase();
              myUseCase.Execute(UpdateStatus)
            }
    
            public void UpdateStatus(string text, bool isThreadRunning)
            {
                Status = text;
            }
       }
    }

Here's the definition of my Execute use case:这是我的执行用例的定义:

    public void Execute(Action<string, bool> callback)
    {
        callback("Begin", true);
        // do stuff which sometimes takes a few seconds
        callback("End", false);
    }

and here's my XAML:这是我的 XAML:

        <Label x:Name="lblStatus" 
               Text="{Binding Status}" 
               HorizontalTextAlignment="Center" 
               FontSize="20" 
               TextColor="DarkRed" 
               FontAttributes="Bold" 
               Margin="0,20,0,0"/>

The result is the user never sees the "Begin" message.结果是用户永远不会看到“开始”消息。 Only the End message after the process is complete.只有进程完成后的结束消息。

You can try to add async-await for function Execute .您可以尝试为 function Execute添加async-await

Based on your code, I did a test, and it works properly on my side.根据你的代码,我做了一个测试,它在我这边工作正常。

Please refer to the following code:请参考以下代码:

  public partial class MyClassVM: ObservableObject
{
    [ObservableProperty]
    string status;

    //[RelayCommand]
    public MyClassVM()
    {
        var myUseCase = new DoStuffUseCase();
        myUseCase.Execute(UpdateStatus);
      }

    public void UpdateStatus(string text, bool isThreadRunning)
    {
        Status = text;
    }
}

internal class DoStuffUseCase
{
    public DoStuffUseCase()
    {
    }

// add async-await  here

    public async void Execute(Action<string, bool> callback)
    {
        callback("Begin", true);
        // do stuff which sometimes takes a few seconds
        await Task.Delay(3000);

        callback("End", false);
    }
}

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

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