MVVM:处理对话的简单方法? [英] MVVM: simple way to handle dialog?

查看:67
本文介绍了MVVM:处理对话的简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿那里,

我已经使用MVVM模式玩弄WPF几周了。我想我理解它背后的概念,我现在很容易接受它,但是当涉及到像对话框一样简单的事情时,我真的很困惑。

I've been toying around with WPF using the MVVM pattern for a few weeks now. I think I understand to concept behind it and I'm now confortable with it, but when it comes to a thing as simple as a dialog box, I'm really confused.

要成为清除我对对话框的期望:

To be clear of what I expect of a dialog box:


  • 用户点击按钮(例如,行上的"删除"按钮);
  • 出现一个模态窗口,带有确认文本和"是"。和"否"按钮(例如);
  • 用户点击"是"按钮:执行操作;
  • 用户点击"否" :弹出窗口关闭,一切都恢复正常,好像什么也没发生。

我一直在搜索堆栈溢出,channel9和msdn上的几个小时,但每次似乎没有一个干净的答案,或者至少不是一个简单的答案(我已经看到了b $ b 很多关于它的线索。但是,我仍然觉得有必要开始另一个)。

I've been searching for hours on stack overflow, channel9 and here on msdn, but every time it seems that there is not a clean answer, or at least not a simple one (and I've seen lots of threads about it. Yet, I still feel the need to start another one).

最令我惊讶的是,显然没有"参考书"。为了这件事。有一些像MVVM Light和Prism这样的东西,我在这里和现在看到弹出,但就是这样。

What surprises me most is that apparently there is no "reference book" for this thing. There are thing like MVVM Light and Prism that I see popping here and now, but that's all.

所以,基本上,我有两个问题:

So, basically, I've got two questions:


  • 是否有一些"参考资源"?就此主题而言?书籍,博客文章......
  • 是否可以处理没有框架(如MVVM Light)的对话框?你能用"开箱即用的WPF"吗?
  • are there some "reference resources" on the subject? Books, blog articles...
  • is it possible to handle dialog box without a framework (like MVVM Light)? Can you do that with "just out of the box WPF"?

欢迎提供代码示例/下载。

Code examples/download are welcome.

对不起,如果这听起来像是关于这个主题的第二步,但此刻我觉得我只是不知道我看到哪个答案是最好的。

Sorry if it sounds like a nth tread about this subject, but at the moment I think I just don't know which answer I saw is best.

谢谢!

推荐答案

>>是否有一些"参考资源"就此主题而言?书籍,博客文章......

>>are there some "reference resources" on the subject? Books, blog articles...

怀疑
你可以找到它,
这是一个老话题。

suspect you can find it, this is an old topic.

>>是否可以处理对话框 没有
一个框架(如MVVM Light)?你可以用"开箱即用的WPF"吗?

我认为你只需要  Window ShowDialog方法,您可以将窗口显示为对话框窗口。

I think you just need Window.ShowDialog Method ,you can show a window as dialog window.

https://msdn.microsoft.com/en-us/library/system.windows.window.showdialog(v = vs.110)的.aspx

您可以自定义消息框,您可以参考以下演示。

And you can custom a messagebox, you can refer following demo.

 public enum CustomMessageBoxButton
    {
        OK = 0,
        OKCancel = 1,
        YesNo = 2,
        YesNoCancel = 3
    }
    public enum CustomMessageBoxResult
    {
        None = 0,
        OK = 1,
        Cancel = 2,
        Yes = 3,
        No = 4
    }
    public enum CustomMessageBoxIcon
    {
        None = 0,
        Error = 1,
        Question = 2,
        Warning = 3
    }
    /// <summary>
    /// Interaction logic for MessageBox.xaml
    /// </summary>
    public partial class CustomMessageBox : Window
    {
        #region Filed
   
        public string MessageBoxText { get; set; }
   
        public string ImagePath { get; set; }
     
        public Visibility OkButtonVisibility { get; set; }
      
        public Visibility CancelButtonVisibility { get; set; }
   
        public Visibility YesButtonVisibility { get; set; }
  
        public Visibility NoButtonVisibility { get; set; }

        public CustomMessageBoxResult Result { get; set; }

        #endregion
        public CustomMessageBox()
        {
            InitializeComponent();
            this.DataContext = this;

            OkButtonVisibility = Visibility.Collapsed;
            CancelButtonVisibility = Visibility.Collapsed;
            YesButtonVisibility = Visibility.Collapsed;
            NoButtonVisibility = Visibility.Collapsed;

            Result = CustomMessageBoxResult.None;
        }
        private void OkButton_Click(object sender, RoutedEventArgs e)
        {
            Result = CustomMessageBoxResult.OK;
            this.Close();
        }

        private void YesButton_Click(object sender, RoutedEventArgs e)
        {
            Result = CustomMessageBoxResult.Yes;
            this.Close();
        }

        private void NoButton_Click(object sender, RoutedEventArgs e)
        {
            Result = CustomMessageBoxResult.No;
            this.Close();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            Result = CustomMessageBoxResult.Cancel;
            this.Close();
        }
        public static CustomMessageBoxResult Show(string messageBoxText, CustomMessageBoxButton messageBoxButton, CustomMessageBoxIcon messageBoxImage)
        {
            CustomMessageBox window = new CustomMessageBox();
            window.Owner = Application.Current.MainWindow;
            window.Topmost = true;
            window.MessageBoxText = messageBoxText;
            switch (messageBoxImage)
            {
                case CustomMessageBoxIcon.Question:
                    window.ImagePath = @"/Images/question.png";
                    break;
                case CustomMessageBoxIcon.Error:
                case CustomMessageBoxIcon.Warning:
                    window.ImagePath = @"/Images/alert.png";
                    break;
            }
            switch (messageBoxButton)
            {
                case CustomMessageBoxButton.OK:
                    window.OkButtonVisibility = Visibility.Visible;
                    break;
                case CustomMessageBoxButton.OKCancel:
                    window.OkButtonVisibility = Visibility.Visible;
                    window.CancelButtonVisibility = Visibility.Visible;
                    break;
                case CustomMessageBoxButton.YesNo:
                    window.YesButtonVisibility = Visibility.Visible;
                    window.NoButtonVisibility = Visibility.Visible;
                    break;
                case CustomMessageBoxButton.YesNoCancel:
                    window.YesButtonVisibility = Visibility.Visible;
                    window.NoButtonVisibility = Visibility.Visible;
                    window.CancelButtonVisibility = Visibility.Visible;
                    break;
                default:
                    window.OkButtonVisibility = Visibility.Visible;
                    break;
            }

            window.ShowDialog();
            return window.Result;
        }

        private void Window_Closed(object sender, EventArgs e)
        {

        }
    }


<Window x:Class="wpfAppDemo.wpfDataGrid.CustomMessageBox"
        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:local="clr-namespace:wpfAppDemo.wpfDataGrid"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Title="MessageBox"
        Width="300"
        Height="300"
        BorderThickness="1,0,1,1"
        Closed="Window_Closed"
        ResizeMode="NoResize"
        ShowInTaskbar="False"
        WindowStartupLocation="CenterScreen"
        mc:Ignorable="d">
    <Grid x:Name="grid">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="50"/>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" VerticalAlignment="Center" Orientation="Horizontal">
            <Image Source="{Binding ImagePath}" Width="62" Height="62" Margin="40,20,20,20"/>
            <TextBlock VerticalAlignment="Center" HorizontalAlignment="Left" TextWrapping="WrapWithOverflow" Width="280" TextAlignment="Left"
                       Text="{Binding MessageBoxText}" FontSize="12"/>
        </StackPanel>
        <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right">
            <Button Content="?" x:Name="OkButton" Width="80" Height="25" Click="OkButton_Click" Margin="10,0,15,0" IsDefault="True"
                    Visibility="{Binding OkButtonVisibility,Mode=OneWay}"/>
            <Button Content="Yes" x:Name="YesButton" Width="80" Height="25" Click="YesButton_Click" Margin="10,0,15,0" 
                    Visibility="{Binding YesButtonVisibility,Mode=OneWay}"/>
            <Button Content="No" x:Name="NoButton" Width="80" Height="25" Click="NoButton_Click" Margin="10,0,15,0"
                    Visibility="{Binding NoButtonVisibility,Mode=OneWay}"/>
            <Button Content="Cancel" x:Name="CancelButton" Width="80" Height="25" Click="CancelButton_Click" Margin="10,0,15,0"
                    Visibility="{Binding CancelButtonVisibility}"/>
        </StackPanel>
    </Grid>
</Window>

最好的问候,

Bob


这篇关于MVVM:处理对话的简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆