全网网站建设设计,网站文章排版工具,如何建微信微网站,网站设计步骤及流程概述上期我们概述了CMFodyHC#xff0c;如果之前没有阅读#xff0c;可以先了解下#xff1a;C# 为什么说CMFodyHC是WPF开发的最强组合#xff1f;今天基于最新的VS版本、最新的CM框架版本#xff0c;.NET基于6.0#xff0c;搭建了一个WPF入门学习项目实例#xff0c;关…概述上期我们概述了CMFodyHC如果之前没有阅读可以先了解下C# 为什么说CMFodyHC是WPF开发的最强组合今天基于最新的VS版本、最新的CM框架版本.NET基于6.0搭建了一个WPF入门学习项目实例关于库的nuget引用部分上节已经讲过了不懂的看上期.开发环境VS2022 WPF框架Caliburn.Micro 版本4.0.212HandyControl3.3.0.NET版本.NET6.0HC相关的用法网上资料很少我们直接下载源项目看示例是最快捷的方式地址https://github.com/AFei19911012/HandyControl项目创建步骤详述① 在NUGET上引用相关的库②接下来是搭建CM框架这里可以参考文章C# WPF MVVM开发框架Caliburn.Micro快速搭建③C# WPF框架Caliburn.Micro快速搭建以上文章对框架大家有详细描述这里不再详细介绍③接下里需要在App.xaml中添加HandyControl的资源最终代码如下Application x:ClassWpfApp5.Appxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:localclr-namespace:WpfApp5Application.ResourcesResourceDictionaryResourceDictionary.MergedDictionariesResourceDictionarylocal:MyBootstrapper x:Keybootstrapper//ResourceDictionaryResourceDictionary Sourcepack://application:,,,/HandyControl;component/Themes/SkinDefault.xaml/ResourceDictionary Sourcepack://application:,,,/HandyControl;component/Themes/Theme.xaml//ResourceDictionary.MergedDictionaries/ResourceDictionary/Application.Resources
/Application然后在对应的XAML页面引用xmlns:hchttps://handyorg.github.io/handycontrol④ 在需要属性变更的类名称前标注[AddINotifyPropertyChangedInterface]然后就可以愉快的玩转CMFodyHC.项目实例代码前台XAML:Window x:ClassWpfApp5.StartViewxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006 xmlns:dhttp://schemas.microsoft.com/expression/blend/2008 xmlns:calhttp://www.caliburnproject.org xmlns:hchttps://handyorg.github.io/handycontrolxmlns:localclr-namespace:WpfApp5mc:IgnorabledTitleStartView Height500 Width600 WindowStartupLocationCenterScreenhc:TransitioningContentControlStackPanelTextBox Margin0,0,0,32 hc:InfoElement.TitleWidth70 hc:InfoElement.PlaceholderNecessary hc:InfoElement.TitlePlacementLeft hc:InfoElement.TitleLeft title hc:InfoElement.NecessaryTrue Style{StaticResource TextBoxExtend} NameTextContent/Button Height48 Width160 Margin3 NametestBtnBackground{DynamicResource PrimaryBrush}hc:BackgroundSwitchElement.MouseHoverBackground{DynamicResource MouseHoverBrush} hc:BackgroundSwitchElement.MouseDownBackground{DynamicResource MouseDownBrush}Button.ContentGridGrid.ColumnDefinitionsColumnDefinition Widthauto/ColumnDefinition//Grid.ColumnDefinitionsImage Sourcepack://application:,,,/Images/icon.ico/Label Grid.Column1 ContentCustom Button BorderThickness0 BackgroundTransparent Foreground{DynamicResource TextIconBrush}//Grid/Button.Content/ButtonListBox NameListBoxItems MinHeight230 BackgroundLightGraycal:Message.Attach[Event SelectionChanged] [Action ListBoxItems_SelectionChanged($source,$eventArgs)];[Event MouseUp][ListBoxItems_MouseUp($source,$eventArgs)] /RepeatButton Height48 Width160 ContentRepeat Button Margin3 Delay500 Style{StaticResource RepeatButtonPrimary} Background{DynamicResource PrimaryBrush} Foreground{DynamicResource TextIconBrush} hc:BorderElement.CornerRadius0 NameRepeatButton_Click//StackPanel/hc:TransitioningContentControl
/Window后台ViewModel:using Caliburn.Micro;
using PropertyChanged;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;namespace WpfApp5
{[AddINotifyPropertyChangedInterface]class StartViewModel : Screen{public StartViewModel(){ListBoxItems new ObservableCollectionstring() { };ListBoxItems.Add(dotNet编程大全);ListBoxItems.Add(Csharp编程大全);ListBoxItems.Add(dotnet工控上位机编程);}public ObservableCollectionstring 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);}}
}CM框架引导程序using Caliburn.Micro;
using System.Windows;namespace WpfApp5
{class MyBootstrapper : BootstrapperBase{public MyBootstrapper(){Initialize();//初始化框架}protected override void OnStartup(object sender, StartupEventArgs e){DisplayRootViewForAsyncStartViewModel();//显示界面}}
}运行演示源代码链接https://pan.baidu.com/s/1zuFpe-gv6h2L0pCJ_u8JoA提取码6666