WPF Stylet框架的基础进阶V1.0
Stylet简介
本次程序的源码是基于上个stylet程序进行编写的
ViewModel中关闭View窗体
1.ViewModel要继承Screen接口
/// <summary>
/// 关闭函数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void CloseButton(object sender, EventArgs e)
{
this.RequestClose();
}
在点击按钮的时候就会直接关闭当前窗体,若窗体为最后一个窗体则关闭程序.
ViewModel中打开其他的View窗体
在传统的View-frist方式中,如果想要显示了一个新的Window或Dialog,需要创建一个View的实例,并且调用.Show()或.ShowDialog()方法。stylet框架中WindowManager解决了这个问题,只需要调用IWindowManager.ShowWindow(someViewModel)就可以调用ViewModel,发现其View,实例化并显示
private IWindowManager windowManager;
public MainViewModel(IWindowManager windowManager)
{
this.windowManager = windowManager;
}
/// <summary>
/// 打开新的界面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void OpenButton(object sender, EventArgs e)
{
FirstViewModel firstviewmodel = new FirstViewModel();
this.windowManager.ShowWindow(firstviewmodel);
}
可以通过Button的CommandParameter属性传递
<Button
Content="刷新"
HorizontalAlignment="Left"
Margin="171,193,0,0"
VerticalAlignment="Top"
Height="39"
Width="109"
CommandParameter="刷新按钮"
Tag="tag刷新"
Click="{s:Action btnClick}" />
也可以通过Tag来传递数据 或者自己创建一个自定义控件 多增加几个传值的属性也是可以的
/// <summary>
/// 刷新按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void btnClick(object sender, EventArgs e)
{
Button button = (Button)sender;
string str = button.CommandParameter.ToString();
string str1 = button.Tag.ToString();
}
事件绑定可绑定到任何位置
<Button
Content="按钮"
s:View.ActionTarget="{Binding CommandButton}"
Click="{s:Action DoSomething}"/>
可以提高代码的复用性而且更灵活
重写了MessageBox
this.windowManager.ShowMessageBox("是否确认",
buttons: MessageBoxButton.YesNo,
buttonLabels: new Dictionary<MessageBoxResult, string>()
{
{ MessageBoxResult.Yes, "Yes please!" },
{MessageBoxResult.No,"No, thanks" }
});
MessageBoxViewModel.ButtonToResults,字典变量,定制对话框同时显示按钮的类型;MessageBoxViewModel.IconMapping,字典变量,定制对话框同时显示图标的类型;为null时表示不显示任何图标;
MessageBoxViewModel.SoundMapping,字典变量,定制对话框播放声音的类型
IWindowManager.ShowMessageBox()有一些参数可用于指定FlowDirection和 TextAlignment。如果未指定这些,则使用默认值和。如果您愿意,也可以更改这些默认值。
目前就学习了这个多 如果有什么新的知识 也希望大家指点
Box()有一些参数可用于指定FlowDirection和 TextAlignment。如果未指定这些,则使用默认值和。如果您愿意,也可以更改这些默认值。
目前就学习了这些 如果有什么新的知识 也希望大家指点