简体   繁体   English

当 Observable 属性更新 ViewModel 时,Uno Platform UI 不更新 Loader

[英]Uno Platform UI not updating Loader when Observable property is updated ViewModel

I am developing a client using Uno Platform and WINUI3 .我正在使用Uno PlatformWINUI3开发客户端。 I have small requirement to show the loader when some external API call is being made asynchronously for getting data.当一些外部 API 调用异步进行以获取数据时,我有一个小的要求来显示加载程序。 In fact i need this functionality across all pages.事实上,我在所有页面上都需要这个功能。

i am using x:Bind to bind 'IsBusy' Observable Property of viewmodel (using CommunityToolkit.MVVM 8.0 ).我正在使用 x:Bind 绑定 viewmodel 的“IsBusy”可观察属性(使用CommunityToolkit.MVVM 8.0 )。 when 'IsBusy' property set to 'true' in async InvokeCommand raised by button click, i am expecting it to start showing the loader in UI.当按钮单击引发的异步 InvokeCommand 中的“IsBusy”属性设置为“true”时,我希望它开始在 UI 中显示加载程序。 and once my external API call is complete i set it back to 'false' so that loader will stop showing.一旦我的外部 API 调用完成,我将其设置回“假”,以便加载程序将停止显示。

Unfortunately, this does not work as expected.不幸的是,这并没有按预期工作。 what am i missing here?我在这里想念什么? complete code is below.完整的代码如下。

MainPage.Xaml:主页.Xaml:

<Page
    x:Class="TestMVVM.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TestMVVM"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"    
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Page.Resources>
        <local:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
    </Page.Resources>
    
    <Grid>
      <!--<TextBlock Text="Hello, world!" Margin="20" FontSize="30" />-->
        <ProgressRing  Name="myLoader" Margin="0,0,5,0"  Height="50" Width="50" IsActive="{x:Bind ViewModel.IsBusy}"/>
        <TextBlock  Name="txtLoader" FontWeight="SemiBold" FontSize="25" Text="Loading..." Visibility="{x:Bind ViewModel.IsBusy, Converter={StaticResource BoolToVisibilityConverter}}"/>
        
                
        <Button Content="CLick me" Command="{x:Bind ViewModel.ItemInvokedCommand}"></Button>
    </Grid>
</Page>

MainPage.Xaml.cs:主页.Xaml.cs:

public sealed partial class MainPage : Page
    {
        public MainPageViewModel ViewModel { get; } = new MainPageViewModel();       
        public MainPage()
        {
            this.InitializeComponent();
            //this.DataContext = ViewModel; // i tried to set like this as well but no luck
        }
    }

ViewModel:视图模型:

public partial class MainPageViewModel : ObservableObject
    {
        private IAsyncRelayCommand _itemInvokedCommand;
        public IAsyncRelayCommand ItemInvokedCommand => _itemInvokedCommand ?? (_itemInvokedCommand = new AsyncRelayCommand<TypedEventHandler<object, object>>(OnItemInvoked));

        [ObservableProperty]
        private bool isBusy;

        private async Task OnItemInvoked(TypedEventHandler<object, object> args)
        {
            
            IsBusy = true;

            //in real time i make some external API call here            
            await Task.Delay(TimeSpan.FromSeconds(10));

            IsBusy = false;
        }
    }

You're missing Mode=OneWay .您缺少Mode=OneWay

The default mode of x:Bind is OneTime (as opposed to the default mode of Binding which is OneWay ). x:Bind的默认模式是OneTime (与Binding的默认模式OneWay相对)。 Reference 参考

<ProgressRing (...) IsActive="{x:Bind ViewModel.IsBusy, Mode=OneWay}"/>
<TextBlock (...) Visibility="{x:Bind ViewModel.IsBusy, Converter={StaticResource BoolToVisibilityConverter}, Mode=OneWay}"/>

暂无
暂无

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

相关问题 尝试更新 ViewModel 中的 Ui 属性时 DispatcherQueue null - DispatcherQueue null when trying to update Ui property in ViewModel Winui3 桌面; ObservableCollection,在属性更改时更新 UI? 从不同线程更新 - Winui3 Desktop; ObservableCollection, updating UI when property changes? Updating from different thread Uno Platform 从 NavigationView 中移除右间距 - Uno Platform Remove Right Spacing from NavigationView WinUI 3 使用 DataTemplate 时如何将命令绑定到 ViewModel 属性? - WinUI 3 How to Bind Command to ViewModel property when using a DataTemplate? MVVM,当 ItemsSource 为 ObservableCollection 时,您能否将 ViewModel 中的命令绑定到 ItemsRepeater 中的 Button<model> 和 ViewModel 的属性?</model> - MVVM, Can you bind command in ViewModel to Button in ItemsRepeater when ItemsSource is ObservableCollection<Model> and a property of the ViewModel? 绑定到可观察的属性,但显示不可观察的子属性 - Bind to observable property, but show a non-observable sub-property 在 ViewModel 中调用时如何在不提供参数的情况下执行带参数的按钮命令? - How to execute Button Command with Parameter without providing the Parameter when called in the ViewModel? 如何从 DataTemplate 绑定到 ViewModel 属性 - How can you bind to a ViewModel property from a DataTemplate WinUI3-MVVM:从 ViewModel 设置 UI 控制焦点 - WinUI3-MVVM: Setting UI control focus from ViewModel 将 SizeChanged 事件绑定到 ViewModel - Binding SizeChanged event to ViewModel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM