如下图所示的“
如下图所示的“
菜单与工具条

MainWindowViewModel.cs
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
namespace _12_MenuToolbar.ViewModels;
public partial class MainWindowViewModel : ObservableObject
{
[ObservableProperty]
private string _statusMessage = "就绪";
[ObservableProperty]
private string _currentFile = "未保存";
[ObservableProperty]
private bool _isBold;
[ObservableProperty]
private bool _isItalic;
[ObservableProperty]
private bool _isUnderline;
[RelayCommand]
private void NewFile()
{
CurrentFile = "未命名.txt";
StatusMessage = "新建文件";
}
[RelayCommand]
private void OpenFile()
{
StatusMessage = "打开文件对话框";
}
[RelayCommand]
private void SaveFile()
{
StatusMessage = $"保存文件: {CurrentFile}";
}
[RelayCommand]
private void Exit()
{
StatusMessage = "退出应用程序";
}
[RelayCommand]
private void Undo()
{
StatusMessage = "撤销";
}
[RelayCommand]
private void Redo()
{
StatusMessage = "重做";
}
[RelayCommand]
private void Cut()
{
StatusMessage = "剪切";
}
[RelayCommand]
private void Copy()
{
StatusMessage = "复制";
}
[RelayCommand]
private void Paste()
{
StatusMessage = "粘贴";
}
[RelayCommand]
private void About()
{
StatusMessage = "关于 - 菜单和工具栏演示 v1.0";
}
}SVG图形显示

SVG资源:
heart.svg
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <path d="M20.84 4.61a5.5 5.5 0 0 0-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 0 0-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 0 0 0-7.78z"></path> </svg>
多语言

LocalizationService.cs
using System.Collections.Generic;
using System.Globalization;
namespace _10_Localization.Services;
public class LocalizationService
{
private static LocalizationService? _instance;
public static LocalizationService Instance => _instance ??= new LocalizationService();
private string _currentLanguage = "zh-CN";
public string CurrentLanguage => _currentLanguage;
public event System.Action? LanguageChanged;
private readonly Dictionary<string, Dictionary<string, string>> _translations = new()
{
["zh-CN"] = new Dictionary<string, string>
{
["WelcomeText"] = "欢迎",
["WelcomeMessage"] = "欢迎使用多语言演示应用",
["UsernameLabel"] = "用户名",
["PasswordLabel"] = "密码",
["LoginButtonText"] = "登录",
["RegisterButtonText"] = "注册",
["LanguageLabel"] = "语言",
["SelectLanguageText"] = "选择语言",
["CurrentLanguageText"] = "当前语言:简体中文"
},
["en-US"] = new Dictionary<string, string>
{
["WelcomeText"] = "Welcome",
["WelcomeMessage"] = "Welcome to Localization Demo App",
["UsernameLabel"] = "Username",
["PasswordLabel"] = "Password",
["LoginButtonText"] = "Login",
["RegisterButtonText"] = "Register",
["LanguageLabel"] = "Language",
["SelectLanguageText"] = "Select Language",
["CurrentLanguageText"] = "Current Language: English"
},
["ja-JP"] = new Dictionary<string, string>
{
["WelcomeText"] = "ようこそ",
["WelcomeMessage"] = "多言語デモアプリへようこそ",
["UsernameLabel"] = "ユーザー名",
["PasswordLabel"] = "パスワード",
["LoginButtonText"] = "ログイン",
["RegisterButtonText"] = "登録",
["LanguageLabel"] = "言語",
["SelectLanguageText"] = "言語を選択",
["CurrentLanguageText"] = "現在の言語:日本語"
}
};
public string[] AvailableLanguages => new[] { "zh-CN", "en-US", "ja-JP" };
public string GetLanguageDisplayName(string code) => code switch
{
"zh-CN" => "简体中文",
"en-US" => "English",
"ja-JP" => "日本語",
_ => code
};
public void SetLanguage(string languageCode)
{
if (_translations.ContainsKey(languageCode) && _currentLanguage != languageCode)
{
_currentLanguage = languageCode;
LanguageChanged?.Invoke();
}
}
public string this[string key]
{
get
{
if (_translations.TryGetValue(_currentLanguage, out var dict) && dict.TryGetValue(key, out var value))
return value;
return key;
}
}
}页面导航

HomeViewModel.cs
namespace _09_Navigation.ViewModels;
public partial class HomeViewModel : ViewModelBase
{
public override void OnNavigatedTo()
{
System.Console.WriteLine("Home page activated");
}
}样式

App.axaml
<Application xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="_08_Styles.App"> <Application.Styles> <FluentTheme /> </Application.Styles> <Application.Resources> <ResourceDictionary> <SolidColorBrush x:Key="PrimaryBrush" Color="#6200EE"/> <SolidColorBrush x:Key="SecondaryBrush" Color="#03DAC6"/> <SolidColorBrush x:Key="DangerBrush" Color="#CF6679"/> <SolidColorBrush x:Key="SuccessBrush" Color="#4CAF50"/> </ResourceDictionary> </Application.Resources> </Application>
MVVM演示

TodoItem.cs
namespace _07_MVVM.Models;
public class TodoItem
{
public required string Title { get; set; }
public bool IsCompleted { get; set; }
public string Priority { get; set; } = "Normal";
}数据绑定

App.axaml
注意如果没有写<FluentTheme/>,则这些Fluent控件将没有效果,比如滑块控件成了一个Lable控件,只显示文字了。
ListBox演示

MainWindow.axaml

MainWindow.axaml
<Window xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="_04_TextInput.MainWindow" Title="Text Input - 文本输入演示" Width="450" Height="400"> <StackPanel Margin="30" Spacing="20"> <TextBlock Text="TextBox(文本框)" FontSize="16" FontWeight="Bold"/> <StackPanel Spacing="5"> <TextBlock Text="请输入您的名字:"/> <TextBox x:Name="NameTextBox" Watermark="在这里输入..." MaxLength="50"/> </StackPanel> <StackPanel Spacing="5"> <TextBlock Text="多行文本框:"/> <TextBox x:Name="MultiLineTextBox" AcceptsReturn="True" AcceptsTab="True" TextWrapping="Wrap" Height="80" Watermark="支持多行输入..."/> </StackPanel> <Button x:Name="SubmitButton" Content="提交" HorizontalAlignment="Center" Padding="30,10"/> <TextBlock x:Name="ResultText" Text="" FontSize="14" Foreground="#2196F3" TextWrapping="Wrap"/> </StackPanel> </Window>
Powered By Z-BlogPHP 1.7.3
Copyright www.skcircle.com Rights Reserved.