WPF mvvm framework Stylet usage tutorial - form interaction usage

window operation

open form

In the stylet framework, to open a window or dialog, just use the window manager directly

Inject in the ViewModel to be used IWindowManager, and then use his method to manipulate the window.

  • ShowDialog(object viewModel)Modal display
  • ShowWindow(object viewModel)Non-modal display
  • ShowMessageBox()Message Box

Example:

Add three buttons to View
insert image description here

        <Button
            Width="140"
            Height="57"
            Margin="781,188,0,0"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            Command="{s:Action ShowDialogAbout}"
            Content="打开弹窗"
            FontSize="20" />
        <Button
            Width="140"
            Height="57"
            Margin="781,269,0,0"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            Command="{s:Action ShowAbout}"
            Content="打开窗口"
            FontSize="20" />
        <Button
            Width="140"
            Height="57"
            Margin="781,356,0,0"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            Command="{s:Action ShowMsg}"
            Content="打开消息框"
            FontSize="20" />

In the corresponding ViewModel, an IWindowManager is injected into the construction method, and the IWindowManager object is obtained in the ViewModel

  private IWindowManager _windowManager;
  
 public ShellViewModel(IWindowManager windowManager)
{
    
    

    _windowManager= windowManager;
}

Then implement three methods, use the method of IWindowManager ShowDialog()、ShowWindow()、ShowMessageBox()to open the window and the message box respectively

/// <summary>
/// 打开一个About窗口,模态显示
/// </summary>
public void ShowDialogAbout()
{
    
    
    _windowManager.ShowDialog(new AboutViewModel());
}

/// <summary>
/// 打开一个About窗口,非模态显示
/// </summary>
public void ShowAbout()
{
    
    
    _windowManager.ShowWindow(new AboutViewModel());
}

/// <summary>
/// 打开一个消息对话框,输入参数跟windows消息框一样
/// </summary>
public void ShowMsg()
{
    
    
    _windowManager.ShowMessageBox("这是一个重要消息", "重要", MessageBoxButton.OK, MessageBoxImage.Information);

}

The complete ShellViewModel.cs code is as follows

  public class ShellViewModel : Screen
    {
    
    



        public void ChangeTitle()
        {
    
    

        }

        private IWindowManager _windowManager;

        public ShellViewModel(IWindowManager windowManager)
        {
    
    
            _windowManager= windowManager;
        }

        /// <summary>
        /// 打开一个About窗口,模态显示
        /// </summary>
        public void ShowDialogAbout()
        {
    
    
            _windowManager.ShowDialog(new AboutViewModel());
        }

        /// <summary>
        /// 打开一个About窗口,非模态显示
        /// </summary>
        public void ShowAbout()
        {
    
    
            _windowManager.ShowWindow(new AboutViewModel());
        }

        /// <summary>
        /// 打开一个消息对话框,输入参数跟windows消息框一样
        /// </summary>
        public void ShowMsg()
        {
    
    
            _windowManager.ShowMessageBox("这是一个重要消息", "重要", MessageBoxButton.OK, MessageBoxImage.Information);

        }
    }

After running, the operation window will open

insert image description here

close form

To close the form, you need to use the method in the base class Screen RequestClose(),

Add a close button to the form and bind the method Close

insert image description here

 <Button
            Width="39"
            Height="26"
            Margin="21,21,0,0"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            Command="{s:Action Close}"
            Content="关闭" />
            

Then implement the close method in ViewModel

  public void Close()
{
    
    
    this.RequestClose();
}

to close the window

Guess you like

Origin blog.csdn.net/qq_39427511/article/details/130184627