本文主要是介绍WPF常备,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
WPF常备
- Font Awesome字体使用
- PropertyChanged.Fody
- Nuget安装
- 添加到FodyWeavers.xml
- Costura.Fody
- BaseViewModel
- BasePage
- BaseAttachedProperty
- BaseValueConverter
- BooleanToVisiblityConverter
- Storyboard
- AttachedProperty实现逆时针顺时针旋转功能
- 几种动画注意事项
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> : Pagewhere 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 ,returnif (mViewModel == value)return;//Update the valuemViewModel = value;//Set data contenxt for this pagethis.DataContext = mViewModel;}}#endregion#region Constructorpublic BasePage(){if (PageLoadAnimation != PageAnimation.None)Visibility = Visibility.Collapsed;//Listen out for page loadingLoaded += BasePage_Loaded;//Create a default view modelthis.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
这篇关于WPF常备的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!