WINUI——Trigger(触发器)使用小结

背景

WINUI不提供原生的Trigger支持,推荐使用VisualStateManager进行操作;然对于从WPF转WINUI的开发人员而言,经常会想用Trigger解决问题,鉴于此社区推出了CommunityToolkit.WinUI.Triggers以支持Trigger的使用。

使用方法

1.项目中引入CommunityToolkit.WinUI.Triggers Nuget包。

当前最新稳定版本为8.0.240109,具体见下图:

2. 在Page中引入CommunityToolkit.WinUI

xmlns:triggers="using:CommunityToolkit.WinUI"

3. 在需要的地方编写相应的Trigger

如编写一个校验是否为邮箱的校验,以下为示例截图:

xml中代码如下:

<Page x:Class="TriggersExperiment.Samples.RegexStateTriggerSample"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:local="using:TriggersExperiment.Samples"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:triggers="using:CommunityToolkit.WinUI"
      mc:Ignorable="d">

    <Grid>
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup>
                <VisualState x:Name="validEmail">
                    <VisualState.StateTriggers>
                        <!--  Note: Simple example RegEx, see our IsEmail string extension using emailregex.com for official RFC 5322 support  -->
                        <triggers:RegexStateTrigger Expression="^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$"
                                                    Options="IgnoreCase"
                                                    Value="{Binding Text, ElementName=emailTextBox, Mode=OneWay}" />
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="emailStatus.Text" Value="This is a valid email" />
                        <Setter Target="emailStatus.Foreground" Value="{ThemeResource SystemFillColorSuccessBrush}" />
                        <Setter Target="submitButton.IsEnabled" Value="true" />
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>

        <StackPanel MaxWidth="400">
            <TextBox x:Name="emailTextBox"
                     HorizontalAlignment="Stretch"
                     Header="Enter an email" />
            <TextBlock x:Name="emailStatus"
                       Margin="0,4,0,24"
                       Foreground="{ThemeResource SystemFillColorCriticalBrush}"
                       Style="{StaticResource CaptionTextBlockStyle}"
                       Text="Not a valid email" />
            <Button x:Name="submitButton"
                    Content="Submit"
                    IsEnabled="False" />
        </StackPanel>
    </Grid>
</Page>

后台代码如下:

using CommunityToolkit.WinUI;

namespace TriggersExperiment.Samples;

[ToolkitSample(id: nameof(RegexStateTriggerSample), "RegexStateTrigger", description: $"A sample for showing how to create and use a {nameof(RegexStateTrigger)}.")]
public sealed partial class RegexStateTriggerSample : Page
{
    public RegexStateTriggerSample()
    {
        this.InitializeComponent();
    }
}

注:以上示例来源于Github。

实现原理

可以看到Trigger的实现还是借助于VisualState实现,只是自定义了一些相应的校验规则而已。以后再做相应介绍。

参考链接:

GitHub - CommunityToolkit/Windows: Collection of controls for WinUI 2, WinUI 3, and Uno Platform developers. Simplifies and demonstrates common developer tasks building experiences for Windows with .NET.

WinUI 3 的 `SettingCard` 是一种用于显示设置信息的控件,通常用于Windows UI应用程序中展示配置选项。它提供了一个简洁、清晰的方式来呈现用户可以调整的设置项。以下是创建和使用 `SettingCard` 的基本步骤: 1. **引入依赖**: 首先,在你的 XAML 文件中添加 `<MahApps.Metro.Controls.Settings>` 库作为依赖项,例如通过 NuGet 包管理器安装 MahApps.Metro。 ```xml <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 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" xmlns:mh="using:MahApps.Metro.Controls.Settings" mc:Ignorable="d" Title="MyApp"> ``` 2. **创建 SettingCard 控件**: 在 XAML 中,你可以创建一个 `SettingCard` 并定义其标题、内容和其他属性。 ```xml <mh:SettingCard Header="General Settings" Background="{ThemeResource AccentColorBrush}"> <mh:SettingItem> <TextBlock Text="Application Name" /> <TextBlock Text="{Binding AppName}" /> </mh:SettingItem> </mh:SettingCard> ``` 这里,`Header` 属性设置了卡片标题,`SettingItem` 是包含单独设置的容器,`AppName` 是一个数据绑定到实际应用名称的文本框。 3. **绑定和数据模型**: 你需要有一个数据模型(通常是 ViewModel),其中包含 `AppName` 或其他设置属性,并在视图中关联数据上下文。 ```csharp private SettingViewModel viewModel; public MainWindow() { InitializeComponent(); viewModel = new SettingViewModel(); DataContext = viewModel; } ``` 4. **处理数据改变**: 如果需要,可以在 `SettingViewModel` 类里实现数据更改,如属性变化监听并更新UI。 ```csharp public class SettingViewModel : ObservableObject { public string AppName { get; set; } // 添加数据改变通知逻辑 private string _appNameBackingField; public string AppName { get => _appNameBackingField; set => SetProperty(ref _appNameBackingField, value); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值