[英]C#/WPF/Avalonia - On button click change text
我正在使用 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!
但什么也沒有發生。
為什么,我的錯誤在哪里?
看看這個頁面:
using ReactiveUI;
public class MyViewModel : ReactiveObject
{
private string caption;
public string Caption
{
get => caption;
set => this.RaiseAndSetIfChanged(ref caption, value);
}
}
這意味着要在 UI 中反映更改,屬性需要實現INotifyPropertyChanged
接口。 仔細閱讀。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.