简体   繁体   English

如何在Caliburn.Micro中使用View的结果调用ViewModel方法

[英]How to invoke a ViewModel method with result from View in Caliburn.Micro

In my Caliburn.Micro project I have a Window view. 在我的Caliburn.Micro项目中,我有一个Window视图。

I use some Invoke in the View to call methods of the ViewModel: 我在视图中使用一些Invoke来调用ViewModel的方法:

private void ViewEvent1(object sender, RoutedEventArgs e)
{
  Caliburn.Micro.Action.Invoke(DataContext, "Event1Occured");
}

private void ViewEvent2(object sender, RoutedEventArgs e)
{
  Caliburn.Micro.Action.Invoke(DataContext, "Event2Occured");
}

They work just fine. 他们工作得很好。

In my View, I have a method that closes the Window: 在我的视图中,我有一个关闭窗口的方法:

private void EventClose(object sender, RoutedEventArgs e)
{
  this.DialogResult = false;      
  Close();
}

Unfortunately I have to check some conditions in the ViewModel before the closing takes effect. 不幸的是,在关闭生效之前,我必须检查ViewModel中的某些条件。

The problem is that Invoke return void , so I cannot call a method that returns a value. 问题是Invoke返回void ,所以我不能调用返回值的方法。

Is there an alternative for Invoke to get the result from the ViewModel's method? Invoke是否可以从ViewModel的方法获取结果?

You don't want to be doing your method calls to viewmodels from the view in that nature.. kinda breaks the ideas behind CMs abilities even for the items that you claim work. 您本质上不希望从视图中进行对视图模型的方法调用。有点儿破坏了CM功能背后的想法,即使您主张工作的项目也是如此。

as eran otzap pointed out you need use convention or Message.Attach, to call a method on the viewmodel from the view is breaking a few things, might as well not use CM in this case. 正如eran otzap指出的那样,您需要使用Convention或Message.Attach,以便从视图中调用viewmodel上的方法破坏了一些事情,在这种情况下可能不使用CM。 Some will go as far as deleting code behind files altogether, because they don't use them at all. 有些人甚至会完全删除文件后面的代码,因为它们根本不使用它们。

Secondly, if it's view related then it doesn't affect the viewmodel that is ok, but since you are calling the viewmodel the premise of SoC and MVVM are thrown out and testing becomes very difficult. 其次,如果它与视图相关,那么它不会影响正常的视图模型,但是由于您正在调用该视图模型,因此SoC和MVVM的前提被抛弃了,并且测试变得非常困难。

Invoking is bad, event passage might be slightly better but still requires some very smelly code to do it. 调用是不好的,事件传递可能会稍微好一些,但仍然需要一些非常臭的代码才能执行。

The documentation at caliburnmicro.com are pretty clear and the examples on the github repository are pretty thorough for ideals that are in place for CMs usage. caliburnmicro.com上的文档非常清楚,而github存储库上的示例对于针对CM使用的理想情况而言非常详尽。

Caliburn.Micro - 如何在 WPF 视图中显示多个项目,来自 ViewModel 继承:导体<object> .Collection.AllActive<div id="text_translate"><p> 我在 WPF 应用程序中使用 Caliburn.Micro 作为我的 MVVM 框架。 我试图在一个页面中包含几个自定义用户控件,这些控件会根据用户交互动态变化。 我了解 CM 约定是 View 将绑定到 ViewModel 中的Items属性。</p><p> 我的问题是:</p><ol><li><p> 如何在我的视图中访问 ViewModel 的Items集合属性中的每个项目?</p></li><li><p> 我可以像通常使用普通 WPF 控件那样将每个项目单独放置在父视图页面上吗?</p></li></ol><p> 视图页面的基本外观如下所示。 <a href="https://i.stack.imgur.com/5vlyX.jpg" rel="nofollow noreferrer">查看页面布局</a></p><p>基本的 ViewModel 代码:</p><pre> public class TestViewModel: Conductor&lt;object&gt;.Collection.AllActive { public TestViewModel() { Items.Add(new CustomUC1ViewModel()); Items.Add(new CustomUC2ViewModel()); Items.Add(new CustomUC3ViewModel()); } //following logic would reassign the Items collection as required }</pre><p> 附加到Items的 ViewModel 将根据用户与父页面和嵌入式用户控件的交互而动态变化。</p><p> 我希望以上内容有意义(这是我的第一篇文章)。</p></div></object> - Caliburn.Micro - How to display multiple items in WPF View from ViewModel that inherits: Conductor<object>.Collection.AllActive

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Caliburn.Micro将视图模型中的图像加载到视图中 - Load image from viewmodel into view with Caliburn.Micro 使用Caliburn.Micro从ViewModel调用UserControl的方法 - Calling method of UserControl from ViewModel with Caliburn.Micro 在Caliburn.Micro中如何将动作绑定到嵌套的ViewModel方法? - In Caliburn.Micro how to bind action to nested ViewModel method? Caliburn.View模型的微视图分辨率<T> - Caliburn.Micro view resolution for ViewModel<T> 如何使Caliburn.Micro ViewModel的文本框为ReadOnly? - How to make textbox ReadOnly from Caliburn.Micro ViewModel? Caliburn.Micro从UserControl调用视图模型 - Caliburn.Micro Calling a viewmodel from UserControl Caliburn.Micro - 如何在 WPF 视图中显示多个项目,来自 ViewModel 继承:导体<object> .Collection.AllActive<div id="text_translate"><p> 我在 WPF 应用程序中使用 Caliburn.Micro 作为我的 MVVM 框架。 我试图在一个页面中包含几个自定义用户控件,这些控件会根据用户交互动态变化。 我了解 CM 约定是 View 将绑定到 ViewModel 中的Items属性。</p><p> 我的问题是:</p><ol><li><p> 如何在我的视图中访问 ViewModel 的Items集合属性中的每个项目?</p></li><li><p> 我可以像通常使用普通 WPF 控件那样将每个项目单独放置在父视图页面上吗?</p></li></ol><p> 视图页面的基本外观如下所示。 <a href="https://i.stack.imgur.com/5vlyX.jpg" rel="nofollow noreferrer">查看页面布局</a></p><p>基本的 ViewModel 代码:</p><pre> public class TestViewModel: Conductor&lt;object&gt;.Collection.AllActive { public TestViewModel() { Items.Add(new CustomUC1ViewModel()); Items.Add(new CustomUC2ViewModel()); Items.Add(new CustomUC3ViewModel()); } //following logic would reassign the Items collection as required }</pre><p> 附加到Items的 ViewModel 将根据用户与父页面和嵌入式用户控件的交互而动态变化。</p><p> 我希望以上内容有意义(这是我的第一篇文章)。</p></div></object> - Caliburn.Micro - How to display multiple items in WPF View from ViewModel that inherits: Conductor<object>.Collection.AllActive Caliburn.Micro-将侧边栏中的按钮绑定到ViewModel中的方法 - Caliburn.Micro - Binding a button in a sidebar to a method in a ViewModel Caliburn.Micro无法匹配来自不同程序集的View和ViewModel - Caliburn.Micro can't match View and ViewModel from different assemblies 从另一个ViewModel设置Caliburn.Micro ControlContent - Setting Caliburn.Micro ControlContent from another ViewModel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM