![](/img/trans.png)
[英]How to Handle MessageBox Dialogs When Using the MVVM Pattern(MVVM Light ToolKit)
[英]How to Show Dialogs Using MVVM Light for WPF?
我需要你的幫助。 我已經花了很多時間研究如何使用 MVVM Light for WPF 顯示對話框的正確方法。 但是,我不走運。
我閱讀了有關如何實現/使用 DialogService 的內容: https ://marcominerva.wordpress.com/2014/10/14/dialogservice-in-mvvm-light-v5/ 卻發現它沒有 DialogService。 我必須為 WPF 實現 DialogService。
有人可以幫助我如何為 WPF 實現 DialogService 嗎? 非常感謝您的幫助。
我不確定這是否是一個完美的解決方案,但我創建了一個服務來顯示對話框。
public interface IDialogService {
void ShowError(Exception Error, string Title);
void ShowError(string Message, string Title);
void ShowInfo(string Message, string Title);
void ShowMessage(string Message, string Title);
bool ShowQuestion(string Message, string Title);
void ShowWarning(string Message, string Title);
}
public class DialogService : IDialogService {
public void ShowError(Exception Error, string Title) {
MessageBox.Show(Error.ToString(), Title, MessageBoxButton.OK, MessageBoxImage.Error);
}
public void ShowError(string Message, string Title) {
MessageBox.Show(Message, Title, MessageBoxButton.OK, MessageBoxImage.Error);
}
public void ShowInfo(string Message, string Title) {
MessageBox.Show(Message, Title, MessageBoxButton.OK, MessageBoxImage.Information);
}
public void ShowMessage(string Message, string Title) {
MessageBox.Show(Message, Title, MessageBoxButton.OK);
}
public bool ShowQuestion(string Message, string Title) {
return MessageBox.Show(Message, Title, MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes;
}
public void ShowWarning(string Message, string Title) {
MessageBox.Show(Message, Title, MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
這對我來說很好用。 如果平台需要改動,直接修改DialogService類即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.