如何将数据传递到 Caliburn.Micro 中的视图模型

发布于 2024-11-29 02:33:57 字数 673 浏览 1 评论 0原文

这可能是一个非常简单的问题,但此时我自己很困惑,看不到答案。简而言之,我有一个包含内容控件的窗口。我正在使用 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 技术交流群。

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

发布评论

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

评论(1

生来就爱笑 2024-12-06 02:33:57

就像你说的,你可以做很多事情:

  1. 你可以在 MyViewModel 上公开一个属性并在其中设置它
    MainWindowView

  2. 您可以使用EventAgregator,从
    MainWindowView 并从 MyViewModel 订阅该事件。

  3. 使用 MEF,您可以在两者之间注入共享资源
    ViewModels,在MainWindowViewModel中设置,并能够访问它
    来自 MyViewModel

Like you said there are a number of things you can do:

  1. You could expose a property on MyViewModel and set it within
    MainWindowView.

  2. You could use the EventAgregator, publish an event from the
    MainWindowView and subscribe to that event from MyViewModel.

  3. Using MEF you could inject a shared resource between the two
    ViewModels, set it in MainWindowViewModel, and be able to access it
    from MyViewModel.

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