Caliburn Micro从后面的视图代码订阅了onpropertychanged事件

时间:2012-11-28 23:46:32

标签: silverlight mvvm caliburn.micro

我正在开发一个拥有文件查看器的silverlight项目。此文件查看器没有Source属性或接受流的其他属性。它有一个LoadDocument(流文件)方法。由于文件将异步加载,我需要“通知”缓冲区可用的视图,然后让View调用LoadDocument方法。

在MVVMLight中,我可以使用“Messenger”功能执行此操作。我看到了EventAggregator,但我看到的一切都是通信的另一种方式。我觉得这应该很容易,但我只是看不到它。

在Views构造函数中是否有办法将方法绑定到ViewModel的属性?似乎这是在xaml中完成的相同功能我只想在后面的代码中执行它。

由于

DBL

1 个答案:

答案 0 :(得分:0)

帖子不确定,但听起来您想要将控件中的事件绑定到视图中的方法

在那种情况下:

<SomeControl cal:Message.Attach="[Event SomeEvent] = [Action SomeMethod($eventArgs)]" />

如果反之亦然,你可以使用事件聚合器(视图可以订阅事件......为什么不,它仍然解耦...)

VM:

SomeEventAggregator.Publish(new SomeMessageInstanceThatTheViewWillSubscribeTo());

查看:

class SomeView : UserControl, IHandle<SomeMessageInstanceThatTheViewWillSubscribeTo>

// dont forget to...
SomeEventAggregator.Subscribe(this);

或者 - 在视图上实现一个接口

class SomeView : UserControl, IAcceptSomeNotificationMessage
{
    public void Notify() { // blah
    }
}

VM:

class SomeViewModel : Screen // whatever 
{
    void SomeMethod() 
    {
        // The VM should be IViewAware so will implement GetView()
        var view = GetView();

        if(view is IAcceptSomeNotificationMessage)
            (view as IAcceptSomeNotificationMessage).Notify();
    }
}

选择以上其中一项 - 我相信还有更多方法。我通常使用事件聚合器 - 当然这取决于你使用了多少IoC以及如何解耦所有内容。

相关问题