目录
- 概述
- 具体方法
- 完整代码
- 使用示例
概述
绑定机制是wpf的核心,也是界面独立的根本,尤其是使用了mvvm模式,没有业务逻辑的干扰,使得界面得以专注于绚丽效果的实现。在xaml中编写绑定语句后,在业务逻辑层需要定义相应的属性与其绑定,需要继承INotifyPropertyChanged,并通过PropertyChanged通知属性改变。但是每个地方都去继承实现INotifyPropertyChanged还是有点麻烦。
具体方法
1、继承INotifyPropertyChanged
继承INotifyPropertyChanged后需要定义一个事件。
public event PropertyChangedEventHandler PropertyChanged;
2、定义通知方法
定义一个动作属性改变的方法。这一步骤将2个参数,简化成了一个字符串参数。
protected void RaisePropertyChangedEvent( string propertyName )
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
3、自动获取属性名
调用的时候要写属性名还是有点麻烦,进一步简化,将属性名参数省去。
在.net 4.5以下需要通过栈帧和反射获取属性名,在.net4.5或core1.0以上使用System.Runtime.CompilerServices.CallerMemberName属性即可。
string GetCallerMemberName()
{
StackTrace trace = new StackTrace();
StackFrame frame = trace.GetFrame(2);//1代表上级,2代表上上级,以此类推
var propertyName = frame.GetMethod().Name;
if (propertyName.StartsWith("get_") || propertyName.StartsWith("set_") || propertyName.StartsWith("put_"))
{
propertyName = propertyName.Substring("get_".Length);
}
return propertyName;
}
通知方法作相应的修改如下。
protected void RaisePropertyChangedEvent(/*此属性在.net 4.5以下不支持将其注释即可*/[System.Runtime.CompilerServices.CallerMemberName] string propertyName = "")
{
if (propertyName == "")
{
propertyName = GetCallerMemberName();
}
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
注:在.net4.5以下只能调用GetCallerMemberName,使用者觉得影响性能则直接使用参数赋值即可。
完整代码
public abstract class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChangedEvent([System.Runtime.CompilerServices.CallerMemberName] string propertyName = "")
{
if (propertyName == "")
{
propertyName = GetCallerMemberName();
}
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
string GetCallerMemberName()
{
StackTrace trace = new StackTrace();
StackFrame frame = trace.GetFrame(2);//1代表上级,2代表上上级,以此类推
var propertyName = frame.GetMethod().Name;
if (propertyName.StartsWith("get_") || propertyName.StartsWith("set_") || propertyName.StartsWith("put_"))
{
propertyName = propertyName.Substring("get_".Length);
}
return propertyName;
}
}
使用示例
TimeTick .cs
public class TimeTick : ViewModelBase
{
public double Seconds
{
set { _seconds = value; RaisePropertyChangedEvent(); }
get { return _seconds; }
}
double _seconds;
public TimeTick()
{
DispatcherTimer time = new DispatcherTimer();
time.Tick += Time_Tick;
time.Interval = TimeSpan.FromMilliseconds(1000);
time.Start();
}
private void Time_Tick(object sender, EventArgs e)
{
//修改属性的值
Seconds++;
}
}
MainWindow.xaml.cs
public MainWindow()
{
InitializeComponent();
this.DataContext = new TimeTick();
}
MainWindow.xaml
<Window x:Class="WpfApp1.MainWindow"
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="360" Width="640">
<Grid >
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding Seconds}" FontSize="128" Foreground="#999999" Loaded="TextBlock_Loaded" ></TextBlock>
</Grid>
</Window>
显示效果

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

评论(0)