你是如何成功执行消息框.显示()功能。?
-
11-09-2019 - |
题
我已经有了一个WPF应用程序 它呼吁消息框.显示()方式在视图模型 (检查,如果用户真正想要删除)。 这实际工作, 但 是对粮食的。 由于模型应该没有明确确定发生什么的看法。
所以现在我在想 如何才能最好地落实消息框.显示()功能 在我的。申请,选择:
我可以有一个消息的文字"你确定...?"随着两个按钮是并没有在所有边境在我的摘要,并创建一个触发的模板,以便于它是倒塌/可见的基于ViewModelProperty叫 AreYourSureDialogueBoxIsVisible, ,然后当我需要这个对话框中,分配AreYourSureDialogueBoxIsVisible到"真实的",并且还处理两个按钮通过DelegateCommand回到我的视图模型.
我还可以以某种方式试图处理这个触发器在XAML以便删除按钮实际上只是使某些边境元出现的与信息和按钮在它,并且是按钮有没有在实际上删除。
这两个解决方案似乎太复杂 为什么用的是一对夫妇的代码行有消息框.显示().
在什么方法你有没有成功地实施对话框在你的。应用程序?
解决方案
这两个你所说的,我喜欢选择#2.删除按钮在网页上只是使"确认"删除"对话"的出现。"确认"删除"对话"实际上踢掉删除。
你有没有检查出卡尔Shifflett的 WPF行的业务幻灯片和演示?我知道他做这样的事情。我会努力记住在哪里。
编辑:查看演示第11"数据验证。"(EditContactItemsControlSelectionviewmodel.DeleteCommand).卡尔调用的弹出窗口ViewModal(什么!?:-).实际上我喜欢你的想法更好。似乎更容易单元的测试。
其他提示
服务到救援。使用 缟玛瑙 (免责声明,我的提交人),这是作为容易,因为:
public void Foo()
{
IDisplayMessage dm = this.View.GetService<IDisplayMessage>();
dm.Show("Hello, world!");
}
在运行的应用程序,这将间接地呼吁消息框.显示("hello world!").测试时,IDisplayMessage服务可以嘲笑和提供的模型做任何你想要完成的期间的试验。
扩大在院长粉笔的回答现在,他的链路是过时了:
在应用程序。停靠。cs文件,我们钩上确认对话的模型.
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var confirm = (Func<string, string, bool>)((msg, capt) => MessageBox.Show(msg, capt, MessageBoxButton.YesNo) == MessageBoxResult.Yes);
var window = new MainWindowView();
var viewModel = new MainWindowViewModel(confirm);
window.DataContext = viewModel;
...
}
在图(MainWindowView.xaml)我们有一个按钮,要求命令中的模型
<Button Command="{Binding Path=DeleteCommand}" />
视图模型(MainWindowViewModel.cs)使用的委托令,以显示"你确定?"对话和执行行动。在这个例子就是一个 SimpleCommand
类似于 此, 但任何执行ICommand应该做的。
private readonly Func<string, string, bool> _confirm;
//constructor
public MainWindowViewModel(Func<string, string, bool> confirm)
{
_confirm = confirm;
...
}
#region Delete Command
private SimpleCommand _deleteCommand;
public ICommand DeleteCommand
{
get { return _deleteCommand ?? (_deleteCommand = new SimpleCommand(ExecuteDeleteCommand, CanExecuteDeleteCommand)); }
}
public bool CanExecuteDeleteCommand()
{
//put your logic here whether to allow deletes
return true;
}
public void ExecuteDeleteCommand()
{
bool doDelete =_confirm("Are you sure?", "Confirm Delete");
if (doDelete)
{
//delete from database
...
}
}
#endregion
我只是创建一个接口(IMessageDisplay或类似的)获得注入虚拟机,并且它的方法就像一个消息框(用()等等)。你可以实现,使用标准的消息框,或者更多的东西WPF具体(I使用 这一更 一些人称为Prajeesh).
这样一切都是分离和测试.
什么样的关于提高样的事件 "MessageBoxRequested"
处理的代码隐藏认(无论如何它认为只码所以我没有看到任何问题与具有这种代码代码隐藏).
我做了一个简单的消息框的包装控制对我们使用在纯粹的。解决方案并且仍然允许单元的测试能力。细节是在我的博客 http://geekswithblogs.net/mukapu/archive/2010/03/12/user-prompts-messagebox-with-mvvm.aspx
mukapu
我已经实施一行为,听到消息的模型.这是基于洛朗bugnion是使解决方案,但由于它不使用代码后面的和更可重复使用,我认为这是更优雅。
只是在情况下任何其他人仍然是阅读和不满意:
我只想处理"通知"类型的消息框中(即我不关心 DialogResult
),但是问题,我与大多数的解决方案,我读的是,他们似乎间接地强迫你来选择你的查看执行情况(即,目前我有一个 MessageBox.Show
, 但是如果我以后决定刚刚摆弄的可见度的一个隐藏的小组直接在我看来,这不会网非常漂亮的一个 INotification
接口传递给模型).
所以我去了快速和肮脏的:
该模型有一个 string NotificationMessage
财产,改变通知 PropertyChanged
.
认赞同 PropertyChanged
, ,如果它看到的 NotificationMessage
酒店来通过,做什么,它想要的东西。
好的,所以这意味着有代码隐藏,名称 PropertyChanged
是硬编码,但将很难编码在XAML无论如何。它意味着我避开所有的东西,如转换器,为可见性和性能来说是否通知仍然可见或不可见。
(应当承认这仅仅是有限的使用情况(消防和忘记),我还没有想一想我怎么可能想到延长。)
我只想把它从虚拟机。我不想要必须用别人的服务或写我自己只是扔一个消息框.
我最近遇到了这个问题,我不得不更换的消息框.显示在这个例子的一些全。申诉的消息框的机构。
实现这个我用 InteractionRequest<Notification>
和 InteractionRequest<Confirmation>
随着相互作用的触发器和写我自己的风景的消息框中。
我已经实施的发布 在这里,
有这么多的答案在这一主题的不同建立一个自定义类使用第三方图书馆。我会说使用第三方的图书馆如果你想酷的弹出窗口有很好的视觉效果。
但如果你只是想要使用常规邮箱从microsoft WPF应用程序,这是一个。/单元的测试友好的执行情况:
起初我以为我会只是继承的消息框和包裹它有接口但我不能因为信息框中不具有公共构造的,因此这里是"简单"的解决方案:
反编译的消息框中visual studio你可以看到所有的方法,重载,我检查哪些我想要的然后创造了一类新的和加入的方法,包裹它的接口和ta-da!现在你可以使用而绑定的接口和类、注射和使用最低采购量为单位的测试e。t.c.
创建一个接口(只有加入一些重载,因为我不需要他们所有的):
public interface IMessageBox
{
/// <summary>Displays a message box that has a message, title bar caption, and button; and that returns a result.</summary>
MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button);
/// <summary>Displays a message box that has a message, title bar caption, button, and icon; and that returns a result.</summary>
MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon);
/// <summary>Displays a message box that has a message and title bar caption; and that returns a result.</summary>
MessageBoxResult Show(string messageBoxText, string caption);
}
然后我们有类继承:
public class MessageBoxHelper : IMessageBox
{
/// <summary>Displays a message box that has a message, title bar caption, button, and icon; and that returns a result.</summary>
public MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button,
MessageBoxImage icon)
{
return MessageBox.Show(messageBoxText, caption, button, icon, MessageBoxResult.None,
MessageBoxOptions.None);
}
/// <summary>Displays a message box that has a message, title bar caption, and button; and that returns a result.</summary>
public MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button)
{
return MessageBox.Show(messageBoxText, caption, button, MessageBoxImage.None, MessageBoxResult.None,
MessageBoxOptions.None);
}
/// <summary>Displays a message box that has a message and title bar caption; and that returns a result.</summary>
public MessageBoxResult Show(string messageBoxText, string caption)
{
return MessageBox.Show(messageBoxText, caption, MessageBoxButton.OK, MessageBoxImage.None,
MessageBoxResult.None, MessageBoxOptions.None);
}
/// <summary>Displays a message box that has a message and that returns a result.</summary>
public MessageBoxResult Show(string messageBoxText)
{
return MessageBox.Show(messageBoxText, string.Empty, MessageBoxButton.OK, MessageBoxImage.None,
MessageBoxResult.None, MessageBoxOptions.None);
}
}
现在只是用这种时注入e。t.c和繁荣都是站不住脚的抽象,将会做到...这是很好的,这取决于你将使用它。我的情况是一个简单的应用程序只是为做一些事情,因此没有一点在工程解决方案。希望这可以帮助别人。