WPF 最强框架组合 CM+Fody+HC

134 阅读2分钟

WPF - 最强框架组合 CM+Fody+HC

前言

环境:.NET 6.0+ 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");
        }
    }
}

运行

最后

如果你觉得这篇文章对你有帮助,不妨点个赞支持一下!你的支持是我继续分享知识的动力。如果有任何疑问或需要进一步的帮助,欢迎随时留言。

也可以加入微信公众号 [DotNet技术匠] 社区,与其他热爱技术的同行一起交流心得,共同成长!

优秀是一种习惯,欢迎大家留言学习!

作者:无心々菜

出处:cnblogs.com/1285026182YUAN/p/18490260

声明:网络内容,仅供学习,尊重版权,侵权速删,歉意致谢!