将 MVVM light Messenger 与 Silverlight 4 ChildWindow 对话框类一起使用

发布于 2024-09-11 16:45:58 字数 2038 浏览 9 评论 0原文

问候!我很享受使用 MVVM 轻量级框架 - 让我的生活变得更加轻松,并且消除了许多事实证明难以克服的障碍......

问题:

我正在尝试设置一个自定义对话框来编辑用户发送到的消息彼此。我正在尝试使用 MVVM 框架使用 ChildWindow 对象构建 silverlight 自定义对话框。

想知道是否有任何关于如何完成此操作的建议按照

我在这里找到的对话框 MVVM 示例代码: http://mvvmlight.codeplex.com/Thread/View.aspx?ThreadId=209338 我陷入困境,因为 Silverlight 中的 ChildWindow 对话框对象是异步的,并且具有不同的 Result 类。

所以 - 我现在的基本想法是使用类的视图模型(在本例中为 Matrix.MessageViewModel)创建自定义对话框的实例,通过 Messenger.Send<> 发送它,处理注册的消息在视图中显示对话框,然后让 ChildWindow 对话框的 Save 按钮处理程序触发 Messenger.Send ,其中包含修改后的内容,然后使用 viewmodel 上的 Save 方法存储这些内容...

似乎有点迂回 - 所以想要确保没有更干净的方法....

相关代码位:

视图模型:

messageDialogBox = new MessageEditorDialog(
    selectedMessage, this.SelectedSiteId,  this.LoggedOnEmployee.Id, this.Projects);

DialogMessage editMessage = new DialogMessage(
    this, messageDialogBox,"Edit Message", DialogMessageCallback);
Messenger.Default.Send(editMessage);

视图:

public ViewHost()
{
    InitializeComponent();

    Loaded += new RoutedEventHandler(ViewHost_Loaded);

    if (!ViewModelBase.IsInDesignModeStatic)
    {
        // Use MEF To load the View Model
        CompositionInitializer.SatisfyImports(this);
    }

    ApplicationMessages.IsBusyMessage.Register(this, OnIsBusyChange);

    Messenger.Default.Register<DialogMessage>(this, msg => ShowDialog(msg));
}



private void ShowDialog(DialogMessage msg)
{
    MessageEditorDialog myDialog = (MessageEditorDialog) msg.Target;
    myDialog.Show();
}

对话框保存:

private void ButtonSave_Click(object sender, RoutedEventArgs e)
{
    Messenger.Default.Send<Message>(
        this.MessageItem, CommandMessages.MessageTypes.MessageSave);
}

这与ViewModel,具有 Messenger.Default.Register<>监视 CommandTypes.MessageSave,它将生成的 MessageItem 路由到模型进行存储......

Greetings! Am enjoying using MVVM light -great framework - has made my life much easier, and has removed a number of barriers that were proving difficult to overcome....

Question:

I am attempting to setup a custom dialog box for editing messages users send to each other. I am attempting to construct a silverlight custom dialog box using the ChildWindow object using the MVVM framework.

Was wondering if there were any suggestions as to how this might be accomplished

Following the dialog MVVM sample code I found here: http://mvvmlight.codeplex.com/Thread/View.aspx?ThreadId=209338 I got stuck because the ChildWindow dialog object in Silverlight is async, and has a different Result class.

So - the Basic idea I have now is using the view model of the class (in this case the Matrix.MessageViewModel) to create an instance of the custom dialog box, send it through the Messenger.Send<>, process the registered message in the view to display the dialog, then have the ChildWindow dialog box's Save button handler fire a Messenger.Send with the modified contents that is then stored using the Save method on the viewmodel...

Seems a bit round-about - so wanted to make sure there wasn't a cleaner way....

Relevant code bits:

view model:

messageDialogBox = new MessageEditorDialog(
    selectedMessage, this.SelectedSiteId,  this.LoggedOnEmployee.Id, this.Projects);

DialogMessage editMessage = new DialogMessage(
    this, messageDialogBox,"Edit Message", DialogMessageCallback);
Messenger.Default.Send(editMessage);

View:

public ViewHost()
{
    InitializeComponent();

    Loaded += new RoutedEventHandler(ViewHost_Loaded);

    if (!ViewModelBase.IsInDesignModeStatic)
    {
        // Use MEF To load the View Model
        CompositionInitializer.SatisfyImports(this);
    }

    ApplicationMessages.IsBusyMessage.Register(this, OnIsBusyChange);

    Messenger.Default.Register<DialogMessage>(this, msg => ShowDialog(msg));
}



private void ShowDialog(DialogMessage msg)
{
    MessageEditorDialog myDialog = (MessageEditorDialog) msg.Target;
    myDialog.Show();
}

Dialog Save:

private void ButtonSave_Click(object sender, RoutedEventArgs e)
{
    Messenger.Default.Send<Message>(
        this.MessageItem, CommandMessages.MessageTypes.MessageSave);
}

This ties back into the ViewModel, that has a Messenger.Default.Register<> watching for the CommandTypes.MessageSave which routes the resulting MessageItem to the model for storage.....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如若梦似彩虹 2024-09-18 16:45:58

这与我所做的非常接近,除了有几件事我做的不同。

  1. 我的对话框视图有一个视图模型,并将消息传递逻辑移至其中,而不是视图后面的代码。
  2. 我将在视图模型中使用 Save 命令,并将 ButtonSave 绑定到该命令。这会将保存逻辑移至视图模型,而不是视图背后的代码。
  3. 单击保存按钮时,您将使用不同的消息。另外,您没有使用 DialogMessage 的回调。假设您更改为使用“保存”命令,则可以将消息保存在视图模型中的私有成员中,然后在用户保存时使用消息的回调。
  4. 您可能需要考虑重新使用对话框视图,或者确保正确清理视图,这样就不会导致内存泄漏。

以下是我根据建议 2 和 2 对您的视图模型所做的更改。 3.

public class MessageEditorDialogViewModel : ViewModelBase
{
    private DialogMessage _dialogMessage;

    public RelayCommand SaveCommand { get; private set; }

    public DialogMessage Message { get; set; }

    public MessageEditorDialogViewModel()
    {
        SaveCommand = new RelayCommand(SaveCommandExecute);
    }

    private SaveCommandExecute()
    {
        Message.Execute();
    }
}

That's pretty darn close to what I'd do, except there are a couple of things I do differently.

  1. I'd have a view model for my dialog view, and move the messaging logic to it rather than the view's code behind.
  2. I'd use a Save command in my view model, and bind the ButtonSave to that command. That moves the save logic to the view model instead of the code behind of your view.
  3. You're using a different message when the save button is clicked. Also, you're not using the DialogMessage's callback. Assuming you change to using a Save command, you could save the message in a private member in the view model, then use message's callback when the user saves.
  4. You may want to think about re-using the dialog view, or ensuring that the view is being cleaned up correctly so you don't end up with a memory leak.

Here's the changes I'd make to your view model following suggestions 2 & 3.

public class MessageEditorDialogViewModel : ViewModelBase
{
    private DialogMessage _dialogMessage;

    public RelayCommand SaveCommand { get; private set; }

    public DialogMessage Message { get; set; }

    public MessageEditorDialogViewModel()
    {
        SaveCommand = new RelayCommand(SaveCommandExecute);
    }

    private SaveCommandExecute()
    {
        Message.Execute();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文