C#/ WPF / Avalonia-在按钮上单击更改文本

问题描述 投票:-1回答: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时,什么都没发生?这是为什么?我想将Message的文本更新为Hello world!,但是什么也没有发生。

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

c# wpf avaloniaui
1个回答
2
投票

看看这个page

© www.soinside.com 2019 - 2024. All rights reserved.