如何将数据传递到 Caliburn.Micro 中的视图模型
这可能是一个非常简单的问题,但此时我自己很困惑,看不到答案。简而言之,我有一个包含内容控件的窗口。我正在使用 Caliburn.Micro 的约定来“定位”视图。
窗口如下所示:
<Window x:Class="Views.MainWindowView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox/>
<ContentControl x:Name="MyViewModel" Height="Auto" Background="Blue"/>
</Grid>
</Window>
已成功找到视图本身,并且屏幕按我的预期显示。但是,MyViewModel 需要进行服务调用才能根据文本框中输入的内容获取信息。
因此,我似乎无法弄清楚如何将该信息从文本框传递到视图模型。我想过几种选择,但它们似乎都需要太多工作,这让我觉得我错过了一些简单的东西。
多谢
This is probably a very simple question, but at this time I have myself so confused I can't see the answer. Simply put, I have a window that contains a content control. I'm using Caliburn.Micro's conventions to "locate" the view.
The window looks like this:
<Window x:Class="Views.MainWindowView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox/>
<ContentControl x:Name="MyViewModel" Height="Auto" Background="Blue"/>
</Grid>
</Window>
The view itself is successfully found, and the screen displays as I expected. However, MyViewModel needs to make a service call to get information based on what is typed into the text box.
So, what I can't seem to figure out is how one would pass that information from the text box to the view model. I've thought of several options, but they all seem to be too much work which makes me think that I'm missing something simple.
Thanks a lot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就像你说的,你可以做很多事情:
你可以在
MyViewModel
上公开一个属性并在其中设置它MainWindowView
。您可以使用
EventAgregator
,从MainWindowView
并从MyViewModel
订阅该事件。使用 MEF,您可以在两者之间注入共享资源
ViewModels,在
MainWindowViewModel
中设置,并能够访问它来自
MyViewModel
。Like you said there are a number of things you can do:
You could expose a property on
MyViewModel
and set it withinMainWindowView
.You could use the
EventAgregator
, publish an event from theMainWindowView
and subscribe to that event fromMyViewModel
.Using MEF you could inject a shared resource between the two
ViewModels, set it in
MainWindowViewModel
, and be able to access itfrom
MyViewModel
.