视图代码后端:
namespace DialogosPruebas
{
/// <summary>
/// Lógica de interacción para MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Messenger.Default.Register<DialogMessage>(
this,
msg =>
{
var result = MessageBox.Show(
msg.Content,
msg.Caption,
msg.Button);
// Send callback
msg.ProcessCallback(result);
});
}
}
}
而 ViewModel
是:
using System;
using System.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
namespace DialogosPruebas.ViewModel
{
/// <summary>
/// This class contains properties that the main View can data bind to.
/// <para>
/// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
/// </para>
/// <para>
/// You can also use Blend to data bind with the tool's support.
/// </para>
/// <para>
/// See http://www.galasoft.ch/mvvm
/// </para>
/// </summary>
public class MainViewModel : ViewModelBase
{
private const string Login = "abcd1234";
public RelayCommand<string> CheckLoginCommand
{
get;
private set;
}
/// <summary>
/// The <see cref="Message" /> property's name.
/// </summary>
public const string MessagePropertyName = "Message";
private string _message = "Login";
/// <summary>
/// Gets the Message property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public string Message
{
get
{
return _message;
}
set
{
if (_message == value)
{
return;
}
_message = value;
RaisePropertyChanged(MessagePropertyName);
}
}
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel()
{
CheckLoginCommand = new RelayCommand<string>(CheckLogin);
}
private void CheckLogin(string text)
{
if (text == Login)
{
var message = new DialogMessage("Login confirmed, do you want to continue", DialogMessageCallback)
{
Button = MessageBoxButton.OKCancel,
Caption = "Continue?"
};
Messenger.Default.Send(message);
}
}
private void DialogMessageCallback(MessageBoxResult result)
{
if (result == MessageBoxResult.OK)
{
Message = "Continue";
}
else
{
Message = "Stop";
}
}
}
}
AXML文件:
<Window x:Class="DialogosPruebas.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<Binding Path="Main" Source="{StaticResource Locator}"/>
</Window.DataContext>
<Grid>
<StackPanel x:Name="LayoutRoot" Background="Black">
<TextBlock FontSize="36"
FontWeight="Bold"
Foreground="Purple"
Text="{Binding Message}"
VerticalAlignment="Center"
HorizontalAlignment="Center"
TextWrapping="Wrap" Margin="0,10" />
<TextBox x:Name="LoginTextBox" TextWrapping="Wrap" Margin="10,0" FontSize="21.333" Text="Enter login">
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyUp">
<cmd:EventToCommand Command="{Binding CheckLoginCommand, Mode=OneWay}" CommandParameter="{Binding Text, ElementName=LoginTextBox}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
<TextBlock TextWrapping="Wrap" Text="(Enter abcd1234 to trigger the message)" HorizontalAlignment="Center" Margin="0,10,0,0" FontSize="16" Foreground="White"/>
</StackPanel>
</Grid>
</Window>
我的疑问是,我们在视图的代码后面使用MessageBox
,而ViewModel
有以下代码:
var message = new DialogMessage("Login confirmed, do you want to continue", DialogMessageCallback)
{
Button = MessageBoxButton.OKCancel,
Caption = "Continue?"
};
Messenger.Default.Send(message);
这将向视图发出请求,到代码后台,然后使用MessageBox
。
为什么这比直接在ViewModel
中使用MessageBox
的解决方案更好呢:
private void CheckLogin(string text)
{
if (text == Login)
{
MessageBox.Show("Login correct");
}
}
有什么不同吗?在这两种情况下,我都使用了MessageBox
,并且必须等待用户的响应。
我已经阅读过,在viewModel
中使用MessageBox
不是一个好主意,但我不明白在这种情况下有什么区别。