WPF Stylet框架的基础进阶V1.0

WPF Stylet框架的基础进阶V1.0

Stylet简介

Stylet开源地址:GitHub - canton7/Stylet: A very lightweight but powerful ViewModel-First MVVM framework for WPF for .NET Framework and .NET Core, inspired by Caliburn.Micro.

本次程序的源码是基于上个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。如果未指定这些,则使用默认值和。如果您愿意,也可以更改这些默认值。

目前就学习了这些 如果有什么新的知识 也希望大家指点

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值