C#值得永久收藏的WPF项⽬实战(经典)
01
—
简介
之前也写过好多篇CM框架相关的项⽬实战⽂章,⽐如:
但是可能这些都是把⼀个综合性的项⽬分多个部分分别讲述的,有些初学者没有从前往后连续起来阅读的话,后⾯的部分可能就不容易理解了。其次,因为没有整体的构思,随着内容的增加,项⽬越来越显得笨重和凌乱,所以今天我重新搞了⼀个项⽬,希望以尽量少的代码,涵盖尽量多的知识点,帮助⼤家更好的理解和使⽤CM框架。
02
—
重要知识点
CM框架的配置和启动;
CM框架⾃带ioc容器SimpleContainer⽤法;
MVVM开发模式;
事件聚合器EventAggregator进⾏事件发布和订阅;
属性通知:
① CM框架下基类PropertyChangedBase⽤法;
②PropertyChanged.Fody.3.1.3下 [AddINotifyPropertyChangedInterface]⽤法;
⽇志框架:Common.Logging和log4net⽤法;
CM框架下命名约定以及事件关联;
⽤第三⽅库Xceed.Wpf.Toolkit.dll做等待窗体;
CM框架下Conductor⽤法;
CM框架下IWindowManager;
双检锁单例模式⽤法;
ListBox数据绑定以及窗体切换;
如果以上知识点你都不敢兴趣或者已经熟知,那这篇⽂章不适合你,看到这⾥你就可以关闭⽹页了!
03
—
运⾏结果
04
—
项⽬说明container容器用法
项⽬主要包括的类⽂件如下图:
① MainWindowViewModel.cs类:
using PropertyChanged;
using System.Collections.ObjectModel;
namespace Caliburn.Micro.Hello
{
[AddINotifyPropertyChangedInterface]
public class MainWindowViewModel : PropertyChangedBase, IViewModel
{
private static readonly Common.Logging.ILog logger = Common.Logging.LogManager.GetLogger<MainWindowViewModel>(); public IViewModel ActiveWindowView { get; set; }
public ObservableCollection<string> ListBoxItems { get; set; }
public string SelectedItem { get; set; }
public MainWindowViewModel()
{
logger.Info($"Enter [MainWindowViewModel].");
ListBoxItems = new ObservableCollection<string>() { };
ListBoxItems.Add("ShellView");
ListBoxItems.Add("EventAggregatorView");
ListBoxItems.Add("ConductorView");
logger.Info($"Leave [MainWindowViewModel].");
}
public void ListBoxItems_SelectionChanged()
{
switch(SelectedItem)
{
case "ShellView":
ActiveWindowView = new ShellViewModel();break;
case "EventAggregatorView":
ActiveWindowView = EventAggregatorViewModel.Instance; break;
case "ConductorView":
ActiveWindowView = new ConductorViewModel(); break;
default:break;
}
}
}
}
[AddINotifyPropertyChangedInterface]:需要引⽤第三⽅库PropertyChanged.Fody,加上之后类⾥⾯的属性变更后会⾃动通知界⾯,⽆需再⼀个个单独触发;
logger是定义的common logging的⼀个通⽤⽇志,需要在nuget上引⽤:Common.Logging.Log4Net1213;
ListBoxItems_SelectionChanged()是listbox的选择改变事件:
需要在前台附加事件:
<ListBox Name="ListBoxItems" Grid.Column="0" SelectedItem="{Binding SelectedItem}" Margin="2"
cal:Message.Attach="[Event SelectionChanged] = [Action ListBoxItems_SelectionChanged]"/>
ActiveWindowView 是我的主窗体,选择那个就激活那个
<ContentControl Name="ActiveWindowView"/>
②ShellViewModel.cs类:
从ioc容器获取⼀个IWindowManager并显⽰:
SimpleContainerViewModel simpleContainerViewModel = new SimpleContainerViewModel();
IWindowManager windowManager = IoC.Get<IWindowManager>();
windowManager.ShowDialog(simpleContainerViewModel);
CanSayHello和SayHello,只有CanSayHello条件满⾜才会执⾏SayHello
public bool CanSayHello
{
get { return !string.IsNullOrWhiteSpace(Name); }
}
public void SayHello()
{
}
③EventAggregatorViewModel.cs
双检锁线程安全的单例模式:
private static EventAggregatorViewModel instance;
private static object locker = new object();
/// <summary>
/// 双检锁
/// </summary>
public static EventAggregatorViewModel Instance
{
get
{
if (instance == null)
{
lock(locker)
{
if (instance == null)
{
instance = new EventAggregatorViewModel();
}
}
}
return instance;
}
}
事件订阅:需要继承事件接⼝IHandle<PersonInfoEven>
private readonly IEventAggregator eventAggregator;
this.eventAggregator = IoC.Get<IEventAggregator>();
this.eventAggregator.Subscribe(this);
事件处理⽅法:
public void Handle(PersonInfoEven message)
{
PersonInfo = message.ToString();
}
④SimpleContainerViewModel.cs
⽅法1 同步ui发布事件
this.eventAggregator.PublishOnUIThread("i am a chinese");
⽅法2 开线程去发布
this.eventAggregator.Publish(new PersonInfoEven() {
Name = "ZYR", Age = 18, Sex = "man" },
action =>
{
/
/⽅式①
//Task.Factory.StartNew(action);
//⽅式②
Task.Run(action);
});
等待窗体BusyIndicator ,需要引⽤第三⽅库Xceed.Wpf.Toolkit,
并添加名称空间:
xmlns:xctk="d/wpf/xaml/toolkit"
<xctk:BusyIndicator Name="busyIndicator" BusyContent="Publish Event ..." IsBusy="{Binding IsShow}" Style="{x:Null}">
⑤ConductorViewModel.cs:此类需要继承Conductor<object>
将需要激活的实例放到ActivateItem,就可以从界⾯绑定激活对应实例窗体
ActivateItem(new ShellViewModel());
05
—
源码下载
项⽬有点长,不可能每个细节都说得到位,能看到这⾥的都是真爱,那么下⾯给出源码百度⽹盘链接:⽹盘链接:
pan.baidu/s/18LOVAIxy7WCWJisiPcOuAQ
提取码:添加⼩编获取
⼩编:mm1552923
:dotNet编程⼤全
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论