輕量級MVVM框架 Stylet

這兩天試了下Stylet框架,這個框架雖然很小,可是功能齊全,簡化了不少MVVM的代碼,好比Command,對Dialog,MessageBox都有很好的支持。html

開源地址 https://github.com/canton7/Styletgit

新建一個WPF項目,添加NuGet引用github

安裝完成後會自動添加一個BootStrapper文件,這個文件是項目啓動文件express

  public class Bootstrapper : Bootstrapper<ShellViewModel>

BootStrapper<ShellViewModel>,這個是對應的啓動窗體,Stylet是根據ViewModel去找對應的View去顯示,ViewModel與View的名稱要一致。app

咱們新添加一個窗體,界面上放一個文本和三個按鈕,來體驗下Stylet的綁定,命令,顯示消息框和子窗體。框架

<Window x:Class="StyletTestNew.Pages.Window1View"
        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:local="clr-namespace:StyletTestNew.Pages"
        mc:Ignorable="d"
          xmlns:s="https://github.com/canton7/Stylet"
        Title="Window1" Height="450" Width="800" d:DataContext="{d:DesignInstance local:Window1ViewModel}">
    
    <Grid>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="106,100,0,0" TextWrapping="Wrap" 
               Text="{Binding FName,UpdateSourceTrigger=PropertyChanged}"   VerticalAlignment="Top" Width="120"/>
        <Button Content="ChangeTxt" HorizontalAlignment="Left" Margin="365,179,0,0" 
                VerticalAlignment="Top" Width="75" Command="{s:Action BtnCommand}"/>
        <Button Content="ShowMessage" HorizontalAlignment="Left"  Command="{s:Action ShowMessage}"
                Margin="533,179,0,0" VerticalAlignment="Top" Width="75"/>
        <Button Content="ShowDialog" HorizontalAlignment="Left"  Command="{s:Action ShowDialog}"
            Margin="649,179,0,0" VerticalAlignment="Top" Width="75"/>

    </Grid>
</Window>

 添加ViewModel,添加對應的屬性和方法spa

public class Window1ViewModel : Screen
    {
        private IWindowManager _windowManger;
        private ShellViewModel _ChildDialog;

        public Window1ViewModel(IWindowManager windowManager,ShellViewModel ChildDialog)
        {
            _windowManger = windowManager;
            _ChildDialog = ChildDialog;
        }

        public string FName { get; set; } = "ly";

        public void BtnCommand()
        {
            FName = DateTime.Now.ToString();
        }

        public bool CanBtnCommand
        {
            get
            {
                 return  !string.IsNullOrWhiteSpace(FName);
            }
        }

        public void ShowMessage() => _windowManger.ShowMessageBox(FName);
        public void ShowDialog() => _windowManger.ShowDialog(_ChildDialog);

    }

咱們能夠看到Button綁定的是方法而不是命令對象3d

Stylet也能夠像Command同樣作CanCommandExecute的功能。code

好比,文本框的Text爲空,則不能夠點按鈕orm

  public bool CanBtnCommand
        {
            get
            {
                 return  !string.IsNullOrWhiteSpace(FName);
            }
        }

        public void BtnCommand()
        {
            FName = DateTime.Now.ToString();
        }

在按鈕綁定的方法名稱前加個Can屬性,實現的功能和CanCommandExecute功能同樣。

你們發現個人ViewModel中沒有實現INotifyPropertyChanged接口,這時,咱們須要nuget一個類庫PropertyChanged.Fody。Stylet完美支持PropertyChanged.Fody,不須要咱們在去實現下INotifyPropertyChanged接口了。

關於PropertyChanged.Fody,你們能夠看這篇文章,介紹的蠻詳細的。

https://www.cnblogs.com/cqgis/p/6360231.html

Stylet也提供了ShowMessage和ShowDialog功能。IWindowManager接口內包含這些方法。

Stylet經過IOC依賴注入,將實例化的WindowManager對象傳入ViewModel中。這樣咱們就能夠在ViewModel中使用這些功能。

        private IWindowManager _windowManger;
        private ShellViewModel _ChildDialog;

        public Window1ViewModel(IWindowManager windowManager,ShellViewModel ChildDialog)
        {
            _windowManger = windowManager;
            _ChildDialog = ChildDialog;
        }
  public void ShowMessage() => _windowManger.ShowMessageBox(FName);
  public void ShowDialog() => _windowManger.ShowDialog(_ChildDialog);

其中,ShowDialog時,傳入的是ShellViewModel,Stylet經過ViewModel去找到了對應的View去顯示。

先寫到這!

相關文章
相關標籤/搜索