WPF - 最强框架组合 CM+Fody+HC
WPF - 最强框架组合 CM+Fody+HC
环境:net6 wpf
框架:
CM:Caliburn.Micro
Fody
HC:HandyControl
一. 添加项目依赖
安装这三个插件
二. 修改 App.xaml
需要在App.xaml中添加HandyControl的资源
<Application x:Class="BenchMarkMaster.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:BenchMarkMaster" xmlns:hc="https://handyorg.github.io/handycontrol"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary> <local:Bootstrapper x:Key="bootstrapper"/> </ResourceDictionary> <ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/SkinDefault.xaml"/> <ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/Theme.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application>
三. 标注
在需要属性变更的类名称前标注
[AddINotifyPropertyChangedInterface]
四. 删除MainWindow.xaml 文件
五. 创建 Bootstrapper.cs 文件
在项目根目录下创建 Bootstrapper.cs 文件
using BenchMarkMaster.Views; using Caliburn.Micro; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace BenchMarkMaster { public class Bootstrapper : BootstrapperBase { public Bootstrapper() { Initialize(); } protected override async void OnStartup(object obj, StartupEventArgs e) { await DisplayRootViewForAsync<MessageBoxWindow>(); } } }
六. 创建 窗口 BenchMarkView.xaml
在项目中创建 Views 文件夹,在里面创建 BenchMarkView.xaml 窗口类型文件
<Window x:Class="BenchMarkMaster.Views.BenchMarkView" 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:local="clr-namespace:BenchMarkMaster.Views" xmlns:hc="https://handyorg.github.io/handycontrol" xmlns:cal="http://www.caliburnproject.org" mc:Ignorable="d" Title="BenchMarkView" Height="450" Width="800" WindowStartupLocation="CenterScreen"> <hc:TransitioningContentControl> <StackPanel> <TextBox Margin="0,0,0,32" hc:InfoElement.TitleWidth="70" hc:InfoElement.Placeholder="Necessary" hc:InfoElement.TitlePlacement="Left" hc:InfoElement.Title="Left title" hc:InfoElement.Necessary="True" Style="{StaticResource TextBoxExtend}" Name="TextContent"/> <Button Height="48" Width="160" Margin="3" Name="testBtn" Background="{DynamicResource PrimaryBrush}" hc:BackgroundSwitchElement.MouseHoverBackground="{DynamicResource MouseHoverBrush}" hc:BackgroundSwitchElement.MouseDownBackground="{DynamicResource MouseDownBrush}"> <Button.Content> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="auto"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Image Source="pack://application:,,,/Images/icon.ico"/> <Label Grid.Column="1" Content="Custom Button" BorderThickness="0" Background="Transparent" Foreground="{DynamicResource TextIconBrush}"/> </Grid> </Button.Content> </Button> <ListBox Name="ListBoxItems" MinHeight="230" Background="LightGray" cal:Message.Attach="[Event SelectionChanged] = [Action ListBoxItems_SelectionChanged($source,$eventArgs)]; [Event MouseUp]=[ListBoxItems_MouseUp($source,$eventArgs)]" /> <RepeatButton Height="48" Width="160" Content="Repeat Button" Margin="3" Delay="500" Style="{StaticResource RepeatButtonPrimary}" Background="{DynamicResource PrimaryBrush}" Foreground="{DynamicResource TextIconBrush}" hc:BorderElement.CornerRadius="0" Name="RepeatButton_Click"/> </StackPanel> </hc:TransitioningContentControl> </Window>
七. 创建 BenchMarkViewModel.cs
在项目中创建 ViewModels 文件夹,在里面创建 BenchMarkViewModel.cs 文件
using Caliburn.Micro; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Controls; using System.Windows.Input; using System.Windows; using PropertyChanged; namespace BenchMarkMaster.ViewModels { [AddINotifyPropertyChangedInterface] public class BenchMarkViewModel : Screen { public BenchMarkViewModel() { ListBoxItems = new ObservableCollection<string>() { }; ListBoxItems.Add("dotNet编程大全"); ListBoxItems.Add("Csharp编程大全"); ListBoxItems.Add("dotnet工控上位机编程"); } public ObservableCollection<string> ListBoxItems { get; set; } public string TextContent { get; set; } public void testBtn() { TextContent = "hello world!"; NotifyOfPropertyChange(() => TextContent); } public void ListBoxItems_MouseUp(object sender, MouseButtonEventArgs e) { ListBox listbox = sender as ListBox; MessageBox.Show("当前操作的控件名称是:" + listbox.Name); } public void ListBoxItems_SelectionChanged(object sender, SelectionChangedEventArgs e) { TextContent = (sender as ListBox).SelectedItem.ToString(); NotifyOfPropertyChange("TextContent"); } public void RepeatButton_Click(object sender, RoutedEventArgs e) { MessageBox.Show("你点击了按钮RepeatButton"); } } }
运行
引用:https://blog.csdn.net/sD7O95O/article/details/126945117