WPF学习日志.-01

学习WPF

WPF和Winform属于两套界面渲染方式。一个是对传统windows界面元素的封装,通过gdi绘制。另一个是全新的dx渲染绘制的界面,也脱离了对传统windows控件的依赖,没有历史包袱,理论上可以展现更炫酷的界面。对初级开发人员来说,没太大区别,类似的基本设计器是他们设计界面的主要手段,一样给事件编写代码。对初级以上开发人员来说,wpf需要学习xaml,有全新的ui描述语言,特别是可以通过模板的嵌套实现复杂的元素,通过style实现类似css的功能,通过比winform完善的多的数据绑定机制实现业务逻辑可以专心对数据而不是界面进行开发。前途来说,两者必定还会长期共存。深入理解winform对理解windows一些内部机制有好处。好好学习wpf可以更方便过度到其他xaml相关技术,这是微软目前广泛使用的技术,比如接下来的windows通用应用。

学习内容如下:
1. 数据绑定
2. 命令绑定
3. 自定立MVVM框架
4. 经典MVVM框架库
一、数据绑定:需要使用道德命名空间:Using System.ComponentModel
代码如下:

using System.ComponentModel
//数据绑定的基类
class ModelBase:INotifyPropertyChanged
{
	public event PropertyChangedEventHandler PropertyChanged;
	public void RaizePropertyChanged(String porpertyName)
	{
		if(PropertyChanged!=null)
		{
		this.PropertyChanged.Invoke(this,new PropertyChangedArges(propertyName));
		}
	}
}
class MainViewModel:ModelBase
{
	private string name;
	public string Name{get return name;
	set{
		if(value!=this.name)
			{
			this.name=value;
			RaizePropertyChanged("Name");
			}
		}
	}
}
//XAML页面部分代码
<Window.Resource>
	<Style>
	<Setter Property="Control.FontSize" value="12"/>
	</Style>
</Window.Resource>


<Button x:Name="bt1" text="{Binding Path=Name,Mode=Two Way}" Margin="100,100,100,100" />
//代码的cs代码加载
DataContent=new MainViewModel();

二、命令绑定:需要使用道德命名空间:Using System.Window.Input
代码如下:

//自定义类
class RelayCommand:ICommand
{
	private event Action<object> _Execute;
	private event Func<object,bool>_CanExecute;
	public event EventHandler CanExecuteChanged;
	public RelayCommand(Action<Object> action,Func<Object,bool> func)
	{
	this._Execute=action;
	this._CanExecute=func;
	}
	public void Execute(Object parameter)
	{
		if(this._Execute==null) return;
		this._Execute(parameter);
	}
	public bool CanExecute(Object parameter)
	{
		if(this._CanExecute==null) return true;
		this._CanExecute(parameter);
	}
} 
//viewModel.cs代码
using System.Window.Input;
class MainViewModel
{
	private ICommand textCommand;
	public ICommand TextCommand{
	set{
		if(this.textCommand==null)
			{
			this.textCommand=new RelayCommand(Text,new Func<Object,bool> (o=>true));
			}
		}
	}
	public void Text()
	{
	Messagebox.Show("Hello world");
	}
}

///xaml代码
<Button x:Name="bt1" Command={Binding  TextCommand} CommandParameter="text">
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

工控匠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值