将 MVVM light Messenger 与 Silverlight 4 ChildWindow 对话框类一起使用
问候!我很享受使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这与我所做的非常接近,除了有几件事我做的不同。
以下是我根据建议 2 和 2 对您的视图模型所做的更改。 3.
That's pretty darn close to what I'd do, except there are a couple of things I do differently.
Here's the changes I'd make to your view model following suggestions 2 & 3.