ToolBar就用Github开源的组件,请大家务必尊重开源精神,如果使用了,请贴出出处。

https://github.com/Tulesha/Avalonia.Controls.ToolBar

稍微整理了下代码,把组件整合到了项目里,其中我的App.axaml配置如下,改成了FluentTheme主题。

<Application xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             x:Class="AvaloniaUI.App"
             RequestedThemeVariant="Light">
             <!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->

    <Application.Styles>
        <FluentTheme/>
    </Application.Styles>

    <Application.Resources>
        <ResourceDictionary>
            <FontFamily x:Key="IconFont">avares://AvaloniaUI/Resources/Fonts/IconFont.ttf#IconFont</FontFamily>
            <FontFamily x:Key="Bayern">avares://AvaloniaUI/Resources/Fonts/Bayern.ttf#Bayern</FontFamily>
            <ResourceDictionary.MergedDictionaries>
                <!--这里添加其他资源-->
                <ResourceInclude Source="/Resources/Themes/ToolBar.axaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

SimpleDocument.axaml代码

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Height="300" Width="300"
         xmlns:local ="using:AvaloniaUI"
         x:DataType="local:ViewModel"
        x:Class="AvaloniaUI.SimpleDocument"
        Title="SimpleDocument">
    <!--https://github.com/Tulesha/Avalonia.Controls.ToolBar-->
    <Grid RowDefinitions="auto,auto,*">
        <Menu Grid.Row="0">
            <MenuItem Header="_File (Alt + F)">
                <MenuItem Header="_New" Command="{Binding NewCommand}"></MenuItem>
                <MenuItem Header="_Open" Command="{Binding OpenCommand}"></MenuItem>
                <MenuItem Header="_Save" Command="{Binding SaveCommand}"></MenuItem>
                <MenuItem Header="Save_As" Command="{Binding SaveAsCommand}"></MenuItem>
                <Separator></Separator>
                <MenuItem Header="Clo_se" Command="{Binding CloseCommand}"></MenuItem>
            </MenuItem>
        </Menu>
        
        <ToolBarTray Grid.Row="1" Background="LightSalmon">
            <ToolBar>
                <Button Command="{Binding NewCommand}">New</Button>
                <Button Command="{Binding OpenCommand}">Open</Button>
                <Button Command="{Binding SaveCommand}">Save</Button>
            </ToolBar>
            <ToolBar>
                <Button Command="{Binding CutCommand}">Cut</Button>
                <Button Command="{Binding CopyCommand}">Copy</Button>
                <Button Command="{Binding PasteCommand}">Paste</Button>
            </ToolBar>
        </ToolBarTray>

        <TextBox Name="txt" Margin="5" Grid.Row="2"
               TextWrapping="Wrap" AcceptsReturn="True"
               TextChanged="txt_TextChanged"></TextBox>
    </Grid>
</Window>

SimpleDocument.axaml.cs代码,用了方便测试,我把ViewModel放在同一个cs中,正常不应该这样做,应该是单独存在的cs文件。

using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace AvaloniaUI;

public partial class ViewModel : ObservableObject
{
    [ObservableProperty]
    [NotifyCanExecuteChangedFor(nameof(SaveCommand))]
    [NotifyCanExecuteChangedFor(nameof(SaveAsCommand))]
    private bool _isDirty;

    [RelayCommand]
    private void New()
    {
        IsDirty = false;
    }
    [RelayCommand]
    private void Open()
    {
        IsDirty = false;
    }
    [RelayCommand(CanExecute = nameof(CanSaveAs))]
    private void Save()
    {
        IsDirty = false;
    }
    [RelayCommand(CanExecute = nameof(CanSaveAs))]
    private void SaveAs()
    {
        IsDirty = false;
    }
    private bool CanSaveAs()
    {
        return IsDirty;
    }
    [RelayCommand]
    private void Close()
    {

    }
    [RelayCommand]
    private void Cut()
    {

    }
    [RelayCommand]
    private void Copy()
    {

    }

    [RelayCommand]
    private void Paste()
    {

    }
}
public partial class SimpleDocument : Window
{
    public SimpleDocument()
    {
        InitializeComponent();
        this.DataContext = new ViewModel();
    }

    private void txt_TextChanged(object? sender, TextChangedEventArgs e)
    {
        if (this.DataContext is ViewModel vm)
        {
            vm.IsDirty = true;
        }
    }
}

运行效果

image

 

posted on 2025-08-08 09:55  dalgleish  阅读(11)  评论(0)    收藏  举报