简体   繁体   English

如何在Caliburn.Micro应用程序中访问ShellViewModel?

[英]How to access ShellViewModel in an Caliburn.Micro application?

I have a WPF application with Caliburn.Micro. 我在Caliburn.Micro上有一个WPF应用程序。 The main ViewModel is ShellViewModel. 主要的ViewModel是ShellViewModel。 It contains a tab control, and each tab contains a user control. 它包含一个选项卡控件,并且每个选项卡都包含一个用户控件。 I need to access a property of ShellViewModel from that internal user control. 我需要从该内部用户控件访问ShellViewModel的属性。

var par = ((MyApp.ShellViewModel)((Screen)Parent).MyProperty; var par =(((MyApp.ShellViewModel)((Screen)Parent).MyProperty;

ShellViewModel is not known though. ShellViewModel是未知的。 Could you please tell how I can access it? 您能告诉我如何使用吗?

Thanks. 谢谢。

var parent = IoC.Get<ShellViewModel>();

我目前无法验证该语法,但我认为这是正确的。

Ok from your comments it sounds like you have a combobox on the shell which needs to affect what is displayed on one of the tabs. 好的,从您的评论看来,您的外壳上似乎有一个组合框,需要影响这些选项卡之一上显示的内容。

To communicate between your ViewModels, you can always use the EventAggregator which is part of CM and implements a subscriber pattern which you can take advantage of 要在您的ViewModel之间进行通信,您始终可以使用EventAggregator它是CM的一部分)并实现一个订户模式,您可以利用该模式

eg 例如

On your shell VM you can create a static instance of the aggregator or create a separate static class which will provide the aggregator to the application 在您的Shell VM上,您可以创建聚合器的静态实例或创建单独的静态类,该类将向应用程序提供聚合器

static class AggregatorProvider 
{
    // The event aggregator
    public static EventAggregator Aggregator = new EventAggregator();
}

class ShellViewModel : Conductor<IScreen>
{
    // When the combo box selection changes...
    public void SelectionChanged(object SomeValue) 
    {
        // Publish an event to all subscribers
        AggregatorProvider.Aggregator.Publish(new SelectionChangedMessage(SomeValue));
    }
}

You handle the SelectionChanged of the combobox by using a standard action message or convention (I'm not sure what conventions CM applies by default to the combo so I'll show the explicit bindings in my example) 您可以使用标准操作消息或约定来处理组合框的SelectionChanged (我不确定CM默认将哪些约定应用于该组合,因此在示例中将显示显式绑定)

<ComboBox x:Name="MyCombo" cal:Message.Attach="[Event SelectionChanged] = [Action SelectionChanged(MyCombo)" />

Hopefully if the correct conventions are applied, you should get the selected item being passed to the method 希望如果应用了正确的约定,则应该将所选项目传递给该方法

Your child VM just needs to subscribe to the aggregator and implement IHandle where T is the type of message it should handle 您的子VM仅需要订阅聚合器并实现IHandle,其中T是它应处理的消息类型

class ChildViewModel : Screen, IHandle<SelectionChangedMessage>
{
    public ChildViewModel() 
    {
        // Subscribe to the aggregator so we receive messages from it
        AggregatorProvider.Aggregator.Subscribe(this);
    }

    // When we receive a SelectionChangedMessage...
    public void Handle(SelectionChangedMessage message) 
    {
        // Do something with the new selection
    }
}

The SelectionChangedMessage can just be: SelectionChangedMessage可以只是:

class SelectionChangedMessage 
{
    public object NewValue { get; private set; }

    public SelectionChangedMessage(object newValue) 
    {
        NewValue = newValue;
    }
}

Obviously the above could be a generic type so that you strongly type the NewValue parameter - then again the message you publish can be anything, so it's up to you 显然,以上内容可能是通用类型,因此您可以强烈键入NewValue参数-然后,您发布的消息可以是任何内容,因此由您自己决定

It might be worth pointing out that you can Unsubscribe from the aggregator so you can control when it receives notifications. 可能需要指出的是,您可以从聚合器Unsubscribe ,以便您可以控制何时接收通知。 The aggregator uses weak references anyway so you don't need to worry too much about unsubscribing, but it does mean you have control over when your objects receive messages (ie stop listening when the are not active by subscribing on OnActivate and unsubscribing on OnDeactivate ). 聚合器始终使用弱引用,因此您不必担心取消订阅,但这确实意味着您可以控制对象何时接收消息(即,通过订阅OnActivate和取消订阅OnDeactivate停止监听对象何时不活动) 。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM