1

我正在使用 Avalonia 0.9.2。

我对它很陌生,按下按钮时我无法更改文本。

这是我的MainWindow.xaml

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:ReadyForWar_Launcher.ViewModels;assembly=ReadyForWar_Launcher"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
        x:Class="ReadyForWar_Launcher.Views.MainWindow"
        Icon="/Assets/avalonia-logo.ico"
        Title="ReadyForWar_Launcher">

    <Design.DataContext>
        <vm:MainWindowViewModel/>
    </Design.DataContext>

  <StackPanel>
    <Button Content="Update the Text" Command="{Binding UpdateText}" CommandParameter="Hello World!"></Button>
    <TextBlock Text="{Binding Message}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
  </StackPanel>


</Window>

这是我的MainWindowViewModel

public class MainWindowViewModel : ViewModelBase
{
    public string Message = "Welcome to Avalonia!";
    public void UpdateText(object text)
    {
        Message = (string)text;
    }
}

当我单击按钮Update the Text时没有任何反应?这是为什么?我想更新 to 的文本,MessageHello world!没有任何反应。

为什么会这样,我的错误在哪里?

4

1 回答 1

2

看看这个页面

using ReactiveUI;

public class MyViewModel : ReactiveObject
{
    private string caption;

    public string Caption
    {
        get => caption;
        set => this.RaiseAndSetIfChanged(ref caption, value);
    }
}

这意味着要在 UI 中反映更改,该属性需要实现INotifyPropertyChanged接口。仔细阅读。

于 2020-01-27T19:23:37.800 回答