[英]Uno Platform UI not updating Loader when Observable property is updated ViewModel
I am developing a client using Uno Platform and WINUI3 .我正在使用Uno Platform和WINUI3开发客户端。 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.