c# - 使用 MVVM 模式时如何处理 MessageBox 对话框(MVVM Light ToolKit)

标签 c# windows-phone-7 mvvm mvvm-light

我正在制作 Windows Phone 7 并尝试使用 MVVM 来完成它。我想保持我的 View 模型尽可能干净,但我不确定如何制作对话框。我正在使用 MVVM light,我知道他们有消息系统或其他东西,但不太确定如何使用它。

我想使用Guide.BeginShowMessageBox,因为这似乎比标准对话框提供更多功能。

如何在不破坏 MVVM 模式的情况下做到这一点。当我加载 View 时,我希望触发加载的触发器,然后检查一些条件。如果满足条件,则显示对话框。

//虚拟机

public RelayCommand MainPageLoaded
    {
        get
        {
            if (!NetworkInterface.GetIsNetworkAvailable())
            {
                // breaks MVVM now as have view code in viewmodel. Need to take out somehow
                Guide.BeginShowMessageBox("Test", "Test network", new List<string>() { "Yes", "No" }, 0, MessageBoxIcon.Warning, asyncResult =>
                    {
                        int? returned = Guide.EndShowMessageBox(asyncResult);
                        // if yes then work offline mode? Maybe another property in ViewModel will get set to say offline mode?
                    }, null);
            }
            return null;
        }
        set
        {
            // Not sure what to put here.
        }
    }

//查看

<i:Interaction.Triggers>
    <i:EventTrigger>
        <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding MainPageLoaded}"/>
    </i:EventTrigger>
</i:Interaction.Triggers>

编辑 我遇到的另一个问题是。我有一个列表,它绑定(bind)到存储在该属性中的一些数据

   public ObservableCollection<ContactGroup> ContactGroups { get; set; }

然后点击我有一个应该触发的中继命令

 public ICommand GroupContactTapped
        {
            get
            {
                return new RelayCommand<GestureEventArgs>(e =>
                {
                    var selectedTextBlock = e.OriginalSource as TextBlock;

                    MessageBox.Show(selectedTextBlock.Tag.ToString());
                });
            }
        }

但是我不知道如何在不将源转换为文本 block 的情况下找到哪个对象被“点击”。

最佳答案

假设您有一个主页/ View 托管所有其他 View ,例如主窗口: 我从 View 模型发送消息事件,并且对话框在主窗口后面的代码中处理。这是我的项目中唯一的代码隐藏,因此我发现项目的其余部分可以严格是 MVVM,除了这一个异常(exception),这是可以接受的。

我使用以下内容发送消息(从 VB 转换而来,因此可能需要处理):

object message = new DialogMessage("YourMessage", YourFunctionThatHandlesCallback) {
    Button = MessageBoxButton.YesNo,
    Caption = "Caption Goes Here"
};
Messenger.Default.Send(message);

我在主页代码后面注册了以下对话框:

Partial Public Class MainWindow
  Inherits Window

  Public Sub New()
    InitializeComponent()

    ''single initialization of messanger for catching message box
    Messenger.[Default].Register(Of DialogMessage)(Me, Sub(msg)
                                                           Dim result = MessageBox.Show(msg.Content, msg.Caption, msg.Button, MessageBoxImage.Warning)
                                                           ''Send callback
                                                           msg.ProcessCallback(result)
                                                       End Sub)
  End Sub
End Class

我无法成功转换 C# lambda,因此不得不将其保留在 VB 中。希望这有帮助

关于c# - 使用 MVVM 模式时如何处理 MessageBox 对话框(MVVM Light ToolKit),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15420793/

相关文章:

c# - Windows Phone 应用程序中 Dispatcher.BeginInvoke 方法的用途是什么

c# - 如何在 Xamarin Forms 中使用 pdf.js 查看 PDF

javascript - 无法从一个 View 模型更新另一个 View 模型的可观察值

c# - WP7 ListBox如何允许用户订购项目

c# - XDocument.Save 方法中的问题

wpf - 绑定(bind)到 ViewModel.SubClass.Property(子属性)

c# - 从不明确的新声明中获取可能的泛型类型列表

c# - WP7 BarcodeManager - 无效的跨线程访问

c# - 在高频交易中尝试并行处理订单是否有意义?

c# - 如何获取 DataTable 一行中的最后一个单元格?