WPF常备
Font Awesome字体使用
Step1: 下载地址为:https://fontawesome.dashgame.com/
Step2:复制 fontawesome-webfont.ttf 字体到项目中,并在字典中引用该字体
字典中引用字体
<FontFamily x:Key="FontAwesome">pack://application:,,,/MafengWo;component/Fonts/#FontAwesome</FontFamily>
如取f110
PropertyChanged.Fody
Nuget安装
安装 PropertyChanged.Fody NuGet 软件包并更新Fody NuGet包:
Install-Package Fody
Install-Package PropertyChanged.Fody
这个Install-Package Fody这是必需的,因为NuGet总是默认为任何依赖项的最古老和最错误的版本。
添加到FodyWeavers.xml
加到FodyWeavers.xml
<Weavers>
<PropertyChanged/>
</Weavers>
特别提醒:VS2017 安装2.61 版本
Costura.Fody
方法同 PropertyChanged.Fody
<?xml version="1.0" encoding="utf-8"?>
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<Costura />
<PropertyChanged/>
</Weavers>
BaseViewModel
[AddINotifyPropertyChangedInterface]
public class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = (sender, e) => {
};
public void OnPropertyChanged(string name)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
所有 ViewModel 全部继承自 BaseViewModel
该基类实现了 [AddINotifyPropertyChangedInterface] 接口,这样在编译的时候将PropertyChanged方法植入到public属性的Set方法中,这样就不必为每个Set都写PropertyChanged了
BasePage
public class BasePage<VM> : Page
where VM :BaseViewModel,new ()
{
#region private Member
/// <summary>
/// The view Model associated with this page
/// </summary>
private VM mViewModel;
#endregion
#region Properties
/// <summary>
/// The animition the play when the page is first loaded
/// </summary>
public PageAnimation PageLoadAnimation {
get; set; } = PageAnimation.SlideAndFadeInFromRight;
/// <summary>
/// The animition the play when the page is first unloaded
/// </summary>
public PageAnimation PageUnLoadAnimation {
get; set; } = PageAnimation.SlideAndFadeInFromLeft;
/// <summary>
/// The time to Animation takes complete
/// </summary>
public float SlideSeconds {
get; set; } = 0.8f;
/// <summary>
/// The view Model associated with this page
/// </summary>
public VM ViewModel
{
get
{
return mViewModel;
}
set
{
//if nothing has changed ,return
if (mViewModel == value)
return;
//Update the value
mViewModel = value;
//Set data contenxt for this page
this.DataContext = mViewModel;
}
}
#endregion
#region Constructor
public BasePage()
{
if (PageLoadAnimation != PageAnimation.None)
Visibility = Visibility.Collapsed;
//Listen out for page loading
Loaded += BasePage_Loaded;
//Create a default view model
this.ViewModel = new VM();
}
#endregion
#region Animation Load / Unload
/// <summary>
/// Once the page is loaded ,perform any requied animation
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void BasePage_Loaded(object sender, System.Windows.RoutedEventArgs e)// use void
{
await AmimationIn();
}
public async Task AmimationIn()
{
if (PageLoadAnimation == PageAnimation.None)
return;
switch (PageLoadAnimation)
{
case PageAnimation.SlideAndFadeInFromRight:
var sb = new Storyboard();
var slideAnimation = new ThicknessAnimation
{
Duration = new Duration