绑定未更新 WinUI 3

3

我正在使用WinUI和Microsoft MVVM工具包进行开发。但是,我在绑定方面遇到了一些问题,无法确定问题出在哪里。

ViewModel和ViewModel中使用的模型都是observableObject类型。命令已经触发并获取了数据。但是,除非我更改XAML并热重载更改,否则绑定不会在UI中显示结果。

我的页面:

<Page
x:Class="ThrustmasterGuide.Pages.WheelBasePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ThrustmasterGuide.Pages"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:model="using:ThrustmasterGuide.DataAccess.Context.Model"
xmlns:wheelbase="using:ThrustmasterGuide.ViewModel.Wheelbase"
xmlns:xaml="using:ABI.Microsoft.UI.Xaml"
xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:converters="using:ThrustmasterGuide.Converters"
xmlns:wheelBase="using:ThrustmasterGuide.Model.WheelBase"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance wheelbase:WheelBaseViewModel, IsDesignTimeCreatable=True}">
<Page.Resources>
    <converters:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
    <converters:InvertBoolToVisibilityConverter x:Key="InvertBoolToVisibilityConverter" />
</Page.Resources>
<interactivity:Interaction.Behaviors>
    <core:EventTriggerBehavior EventName="Loaded">
        <core:EventTriggerBehavior.Actions>
            <core:InvokeCommandAction Command="{x:Bind ViewModel.LoadWheelBaseCommand}" />
        </core:EventTriggerBehavior.Actions>
    </core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>

<StackPanel Padding="16 16 16 16" Orientation="Vertical">
    <StackPanel>
        <TextBlock FontSize="18" Text="{x:Bind ViewModel.WheelBase.Name}" />
        <TextBlock FontSize="18" Text="Symptomen:" />
        <TextBlock Text="Kies hieronder een symptoom uit om te starten." />
        <ProgressRing IsActive="true"
                      Visibility="{x:Bind ViewModel.LoadWheelBaseCommand.IsRunning, Converter={StaticResource BoolToVisibilityConverter}}" />
        <TreeView ItemsSource="{x:Bind ViewModel.WheelBase.Symptoms, Mode=OneWay}">
            <TreeView.ItemTemplate>
                <DataTemplate x:DataType="wheelBase:SymptomModel">
                    <TreeViewItem ItemsSource="{x:Bind Children}" Content="{x:Bind Description}" />
                </DataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
    </StackPanel>
    <Button VerticalAlignment="Bottom" Command="{x:Bind ViewModel.LoadWheelBaseCommand}"
            Content="Refresh">
    </Button>
</StackPanel>

我的 ViewModel:

    public class WheelBaseViewModel : ObservableRecipient
{
    public WheelBaseModel WheelBase { get; set; }

    public string WheelBaseName { get; set; }

    private readonly WheelBaseService _wheelBaseService;

    public IAsyncRelayCommand LoadWheelBaseCommand { get; }

    public WheelBaseViewModel(WheelBaseService wheelBaseService)
    {
        _wheelBaseService = wheelBaseService;
        LoadWheelBaseCommand = new AsyncRelayCommand(FetchWheelBase);
    }

    public async Task FetchWheelBase()
    {
        WheelBase = await _wheelBaseService.GetWheelBase(WheelBaseName);
    }
}

我的模型:

namespace ThrustmasterGuide.Model.WheelBase
{
public class WheelBaseModel : ObservableObject
{
    public string Name { get; set; }

    public ObservableCollection<SymptomModel> Symptoms { get; set; }
}

我的代码:

代码后台:

    public sealed partial class WheelBasePage : Page
{
    public WheelBasePage()
    {
        this.InitializeComponent();
        this.DataContext = App.Current.Services.GetService<WheelBaseViewModel>();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        this.ViewModel.WheelBaseName = e.Parameter as string;
    }

    public WheelBaseViewModel ViewModel => (WheelBaseViewModel)DataContext;
}

我错过了什么让UI与WheelBaseModel的值绑定吗?

更新我添加了mode=OneWay,但仍未更新。

需要注意的是,我在导航后在内容框架中显示页面吗?


1
你尝试在你的TreeViewItem上添加“Mode=OneWay”了吗? - Andrew KeepCoding
是的,我在发完这篇帖子后立即添加了那个,但仍然没有运气。 - Tristan Boland
3个回答

7
的默认模式是 OneTime,而 {Binding} 的默认模式是 OneWay。

我尝试添加单向模式,但是没有成功,UI界面仍然没有更新。 - Tristan Boland

6
我认为您需要使用SetProperty,以便知道何时引发此类事件?

https://learn.microsoft.com/en-us/windows/communitytoolkit/mvvm/observableobject

namespace ThrustmasterGuide.Model.WheelBase
{
public class WheelBaseModel : ObservableObject
{
    public string Name
    {
        get => name;
        set => SetProperty(ref name, value);
    }

    public ObservableCollection<SymptomModel> Symptoms { get; set; }
}

并且绑定模式 = OneWay

<TextBlock FontSize="18" Text="{x:Bind ViewModel.WheelBase.Name, Mode=OneWay}" /> 

1
使用 setproperty 并手动调用 OnPropertyChanged 可以触发事件。不确定是否打算手动调用,但这样做可以使其正常工作。 - Tristan Boland

0

我正在使用微软的MVVM工具包,ObservableObject实现了INotifyPropertyChanged接口,并且在属性更改时应调用OnPropertyChanged。 - Tristan Boland
实现接口的基类不会改变属性的行为。 - Kevin Gallahan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接