Knowledge Point Record
Incidental
- 窗体置顶属性 Topmost = true;
- 控件层次划分 Panel.ZIndex=“2”;(数值越大,越位于顶层 Canvas)
- MVVM模式指定数据上下文新方法:
Resources Study
ResourcesDictionary Cite Create
<!--Example:-->
<Window.Resources>
<SolidColorBrush Color="AliceBlue" x:Key="solidAilcBlue"/>
</Window.Resources>
可以新建一个资源字典,将控件样式编写在其中。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush Color="AliceBlue"
x:Key="solidAilcBlue" />
<SolidColorBrush Color="red"
x:Key="solidRed" />
<SolidColorBrush Color="blue"
x:Key="Blue" />
<SolidColorBrush Color="Green"
x:Key="solidGreen" />
</ResourceDictionary>
在项目中引用上述字典资源文件:
<Application x:Class="WpfApp099.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp099"
StartupUri="View/LoginView.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/Dictionary1.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
静态常量值的资源申明:
xmlns:system="clr-namespace:System;assembly=netstandard"
<Window.Resources>
<system:Double x:Key="BtnWidth">60</system:Double>
</Window.Resources>
动态资源绑定与引用修改:
<Border Grid.Row="0" Grid.Column="0" Background="{DynamicResource Blue}"/>
<Border Grid.Column="1" Grid.Row="0" Background="{DynamicResource solidAilcBlue}" />
<Border Grid.Column="0" Grid.Row="1" Background="{DynamicResource solidGreen}" />
<Border Grid.Column="1" Grid.Row="1" Background="{DynamicResource solidRed}" />
//方式一
this.Resources["solidAilcBlue"] = new SolidColorBrush(Colors.Black);
//方式二
var solid = App.Current.FindResource("solidAilcBlue");
只有在动态资源绑定时(DynamicResource),才能够通过后台代码索引资源的方式修改样式。静态资源则无法在程序运行时修改样式。
ResourcesDictionary Cite Way
资源引用具有两种方式,相对引用 or 绝对引用。
-
相对引用:
<Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/Resources/Dictionary1.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources>
-
绝对引用:
<Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/SkinDefault.xaml"/> <ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/Theme.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources>
绝对引用路径格式说明:
<ResourceDictionary Source="pack://application:,,,/[HandyControl];[版本号];[公钥];component/Themes/SkinDefault.xaml"/>
Style Study
样式的声明可以在窗体的Window.Resource根节点下面就行编写,也可以在项目的App.xaml文件下编写,还可以在新建的资源字典里面编写。总结Style声明位置
- Window窗体资源根节点
- App.xaml文件
- Dictionary.xaml资源字典文件
<Window.Resources>
<Style x:Key="textblock" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="White" />
<Setter Property="FontSize" Value="35" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="TextAlignment" Value="Center" />
</Style>
</Window.Resources>
样式继承:
- 方式一
<Style x:Key="textbockSon" TargetType="{x:Type TextBlock}" BasedOn="{StaticResource textblock}">
<Setter Property="Foreground" Value="black" />
</Style>
- 方式二
<Style x:Key="textBlockNokey" TargetType="{x:Type TextBlock}" BasedOn="{StaticResource {x:Type TextBlock}}">
<Setter Property="Foreground" Value="black" />
</Style>
方式二中如过继承的父样式没有键值Key,则会在资源树中向上查找,原生的控件样式。(使用较少)
样式绑定也有两种方式:静态绑定 or 动态绑定。
Control Template Study
- TemplateBinding
- Triggers
<ControlTemplate.Triggers>
<Trigger Property="IsDefaulted" Value="true">
<Setter TargetName="border" Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="border" Property="Background" Value="{StaticResource Button.MouseOver.Background}" />
<Setter TargetName="border" Property="BorderBrush" Value="{StaticResource Button.MouseOver.Border}" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="border" Property="Background" Value="{StaticResource Button.Pressed.Background}" />
<Setter TargetName="border" Property="BorderBrush" Value="{StaticResource Button.Pressed.Border}" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="border" Property="Background" Value="{StaticResource Button.Disabled.Background}" />
<Setter TargetName="border" Property="BorderBrush" Value="{StaticResource Button.Disabled.Border}" />
<Setter TargetName="contentPresenter" Property="TextElement.Foreground" Value="{StaticResource Button.Disabled.Foreground}" />
</Trigger>
</ControlTemplate.Triggers>
Font Resource Use
Font Resource Create
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<FontFamily x:Key="songti">
/WpfApp099;component/Resources/Fonts/AiDianGanFengXingShuttf-2.ttf#爱点乾峰行书
</FontFamily>
</ResourceDictionary>
格式说明:/[项目名称];comonent/[文件路径]#[字体名称]
上述字体名称需要双击字体文件查看,并非字体文件名称。
最后将字体文件属性更改为资源类型生成。
ResourceDictionaryMerged
<Application x:Class="WpfApp099.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp099"
StartupUri="View/ResourcesStudy.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/Style/Dictionary1.xaml" />
<ResourceDictionary Source="/Resources/Style/BtnTempalate.xaml" />
<ResourceDictionary Source="/Resources/Fonts/Font.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Binding Study
绑定的Base:
private Emloyee emloyee = new Emloyee() { Name = "nihao" };
private void InitBinding()
{
var binding = new Binding();
binding.Source = emloyee;
binding.Path = new PropertyPath("Name");
binding.Mode = BindingMode.TwoWay;
binding.UpdateSourceTrigger = UpdateSourceTrigger.LostFocus;
//Mode、UpdateSourceTrigger属性均为枚举类型。
this.text.SetBinding(TextBlock.TextProperty, binding);
}
public class Emloyee
{
public string Name { get; set; }
}
绑定四要素:
- Source
- Path
- Mode
- UpdateSourceTrigger
绑定方式:
-
将控件作为绑定源
<TextBlock Width="200" Height="auto" Margin="0,20,0,0" Background="AliceBlue" FontSize="20" Text="{Binding ElementName=slider, Path=Value}" TextAlignment="Center" /> <Slider x:Name="slider" Width="400" Height="20" Margin="0,20,0,0" Maximum="100" Minimum="0" IsSnapToTickEnabled="True"/>
-
将DataContext作为数据源
<Window x:Class="WpfApp099.View.ResourcesStudy" 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:local="clr-namespace:WpfApp099.View" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:system="clr-namespace:System;assembly=netstandard" xmlns:viewmodel="clr-namespace:WpfApp099.ViewModel" Title="ResourcesStudy" Width="800" Height="450" mc:Ignorable="d"> <Window.DataContext> <viewmodel:datacontext Name="clik me" /> </Window.DataContext> <Grid> <Button Width="200" Height="35" Content="{Binding Path=Name}" /> </Grid> </Window>
DataContext声明是,相当于后台代码new一个实例,其中Name相当于初始化赋值。
Item Binding
Singleton Pattern(单例模式)
class Test
{
//引入标志位 避免外界使用反射的方式创建实例 防君子不防小人。
private static bool isReflection = true;
//构造函数私有化
private Test()
{
lock(o)
{
if(isReflection)
{
isReflection = false;
}
else
{
throw new Exception("无法从外界创建对象");
}
}
}
//写一个私有的字段
private static volatile Test test;
//写一个线程锁对象
private static object o = new object();
//写一个方法暴露给外界这个实例
public static Test GetTest()
{
if(test == null)
{
lock(o)
{
if(test == null)
{
test = new Test();
}
}
}
return test;
}
}
上述代码展示了单例设计模式。从“单例”字面意思上理解为——一个类只有一个实例,所以单例模式也就是保证一个类只有一个实例的一种实现方法
单例模式具有线程安全的问题,所以需要加上线程锁,避免多个线程访问时,创建多个实例。**lock()**是一个互斥锁。