本文记录学习WPF的过程
WPF参见视频 https://www.bilibili.com/video/BV1ht411e7Fe?p=5&vd_source=d0d8cc5a57b8088c1b1856a3e9087bb3
Prism参见视频
https://www.bilibili.com/video/BV1Ei4y1F7du/?p=2&spm_id_from=pageDriver
一、 WPF介绍
1. XAML介绍
-
在XAML中为对象属性赋值,用 Atribute=value的形式
xaml 里添加 xmlns:local=“clr-namespace:HappyWPF”
在resource里 <local:Human x:key=“human” Name=“Tim” Child=“LittleTim”/>
.cs 里 定义Human类
[TypeConverter(typeof(NameToHumanTypeConverter)]
public classHuman
{ public string Name {get; set;}
public Human Child {get; set;} }
定义 派生类
public class NameToHumanTypeConverter:TypeConverter
{
public override object ConvertFrom(ITypeDescriptorContext context,)
{ string name=value.Tostring(); Human child=new Human();
child.Name=name; return child; }
}
获取 Human h=this.FindResource(“human”) as Human; -
在XAML中为对象属性赋值,用属性标签的形式 .下例子中 <Button.Content>就是属性标签
xaml 里添加
//对象的内容 <Button.Content> <Rectangle Width=“20”,Height=“20” Fill=“LawnGreen” />
</Button.Content>
-
在XAML中为对象属性赋值,用 属性扩展的形式,这种不是很多
Binding ,DynamicResource, MultiBinding, PriorityBinding,StaticResource
xaml 里添加 资源里 <sys:String x:Key=“stringHello”> Hello WPF! </sys:String>
使用 <TextBlock Text={StaticResource:ResouceKey=stringHello} /> -
事件
2 x命名空间及其常用元素
- xmlns=“http://schemas.microsoft.com/winfx/2006/xaml/presentation”
里面存放了 wpf 默认的好多控件 - xmlns:x=“http://schemas.microsoft.com/winfx/2006/xaml”
xaml的解析 常用的:
x:Array 标签扩展
x:Class Attribute 声明类 <Window x.Class=“xxxx” 表示继承Window的xxxx类。
x:ClassModifier 类的修饰符 public private
x:Code XAML指令元素
x:FiledModifier Attribute 指的是某个空间的 是否可以修改。
x:Key Attribute
x:Name Attribute 就是控件实例名。
x:Null 扩展标签
x:Shared Attribute
x:Static 扩展标签
x:SubClass Attribute
x:Type 扩展标签
x:TypeArguments Attribute
x:Uid Attribute
x:XData Attribute
3 MV传输到VIEW
定义
使用
设置DataContext this.DataContext=对应的VM。
3MV接受命令
定义
使用
二、 Prism介绍
1. Prism 初始化过程
2. wpf包含两种容器 Dryloc 与 Unity. 本文使用Dryloc 。 说是快
3. 可以使用Prism Template Pack 创建项目
1. 安装 Prism Template Pack 项目模板。 在管理扩展里安装。
4 Region
1. 定义Region (RegionManager)
两种方式 :
在XAML里, RegionNameManager.RegionName
XAML里 ContentControl rism:RegionManager.RegionName=“contentRegion” />
在代码里 `` <gionNameManager.SetRegionName
XAML 里
CS 里 RegionManager.SetRegionName( Ctr,“ContentRegion”);
2. RegionManager 除了定义区域,还有以下功能。
维护区域集合0
提供对区域的访问: RegionNameManager.Region
合成视图 : 给区域指定试图
区域导航定义区域
自定义 区域适配器
1 创建 适配器; 2 注册 3 使用
4 模块 Module
加载方式。
自己创建模块
或 视图注入的模式
5 视图注入
6 MVVM
VM :
V 设置自动绑定
在这里插入图片描述
复合命令:
7 消息队列 事件集合器 IEventAggregator
8 导航
9 对话服务
10 ViewModule 的接口
INavigationAware : 导航接口,设计到传参, 是否已经存在,是否重新初始化。
IConfirmNavigationRequest: 继承于INavigationAware。 用于切换时,弹对话框
BindableBase : 事件, 变化通知。 如:RaisePropertyChange();
4. a
三、 操作步骤
1. 创建Prism项目
1 创建一个基于.NetFramework 或NetCore的WPF应用程序
2 NuGet管理器搜索添加Prism.Dryloc包。版本: 8.0.0.1909
安装后, 就自动添加了Prism的依赖了。
3. 修改Application基类 PrismApplication