如何调用方法来更新UI?

问题描述 投票:0回答:1

WaveForm.cs

public class WaveForm
{
    public double Amplitude
    {
        get;set;
    }

    public double Frequency
    {
        get;set;
    }

    public string Wave()
    {
        return (Amplitude * Frequency).ToString();
    } 

    public WaveForm()
    {
        Amplitude = 1;
        Frequency = 2;
    }
}

频道.cs

public class Channel
{

    public string ChannelId
    {
        get;set;
    }

    public WaveForm WaveForm
    {
        get;set;
    }

    public Channel(string channelId)
    {
        ChannelId = channelId;
        WaveForm = new WaveForm();
    }
}

MainViewModel.cs

[ObservableProperty]
private ObservableCollection<Channel> _channelSource = new();
<TextBox
    Text="{x:Bind WaveForm.Amplitude, Mode=TwoWay}"
    TextWrapping="Wrap" />
<TextBox
    Text="{x:Bind WaveForm.Frequency, Mode=TwoWay}"
    TextWrapping="Wrap" />
<TextBlock
    Style="{StaticResource BodyTextBlockStyle}"
    Text="{x:Bind WaveForm.Wave(), Mode=OneWay}"
    TextWrapping="Wrap" />

我有多个频道,我想显示。每个通道都应该是可编辑的。如果我更改通道的频率或幅度,“Wave()”不会更新。我该如何解决这个问题?

c# .net winui-3 winui community-toolkit-mvvm
1个回答
0
投票

您可以使用您已经在使用的

CommunityToolkit.Mvvm
包附带的 NotifyPropertyChangedFor 属性。

using CommunityToolkit.Mvvm.ComponentModel;
using System.Collections.ObjectModel;

namespace WinUI3App1;

public partial class WaveForm : ObservableObject
{
    [ObservableProperty]
    [NotifyPropertyChangedFor(nameof(Wave))]
    private double _amplitude = 1;

    [ObservableProperty]
    [NotifyPropertyChangedFor(nameof(Wave))]
    private double _frequency = 2;

    public string Wave => (Amplitude * Frequency).ToString();
}

public partial class Channel : ObservableObject
{
    [ObservableProperty]
    private string _channelId = string.Empty;

    [ObservableProperty]
    private WaveForm _waveForm = new();

    public Channel(string channelId)
    {
        ChannelId = channelId;
    }
}

public partial class MainViewModel : ObservableObject
{
    [ObservableProperty]
    private ObservableCollection<Channel> _channelSource = new();

    public MainViewModel()
    {
        ChannelSource.Add(new Channel("1"));
        ChannelSource.Add(new Channel("2"));
        ChannelSource.Add(new Channel("3"));
    }
}

然后在 XAML 中:

<Window
    x:Class="WinUI3App1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="using:WinUI3App1"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid>
        <ItemsRepeater ItemsSource="{x:Bind ViewModel.ChannelSource}">
            <ItemsRepeater.ItemTemplate>
                <DataTemplate x:DataType="local:Channel">
                    <StackPanel>
                        <TextBox
                            Text="{x:Bind WaveForm.Amplitude, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                            TextWrapping="Wrap" />
                        <TextBox
                            Text="{x:Bind WaveForm.Frequency, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                            TextWrapping="Wrap" />
                        <TextBlock
                            Style="{StaticResource BodyTextBlockStyle}"
                            Text="{x:Bind WaveForm.Wave, Mode=OneWay}"
                            TextWrapping="Wrap" />
                    </StackPanel>
                </DataTemplate>
            </ItemsRepeater.ItemTemplate>
        </ItemsRepeater>
    </Grid>
</Window>
© www.soinside.com 2019 - 2024. All rights reserved.