多个ViewModel:将事件传递给其他ViewModel

时间:2015-01-31 07:14:31

标签: c# wpf mvvm caliburn.micro

我在Button中有ShellView我想将此按钮触发到FooViewModel但不起作用,不知道。

<Window x:Class="CM.Views.ShellView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">

<Button cal:Message.Attach="[Event Click]=[Action FooViewModel.About]"/>
</Window>


namespace CM.ViewModels
 {
    public class ShellViewModel : Conductor<IScreen>.Collection.OneActive, IShellViewModel
    {
        public ShellViewModel()
        {
        }
   }

   public class FooViewModel
   {
         public void About()
         {
             MessageBox.Show("About method fired");
         }
   }
}

3 个答案:

答案 0 :(得分:1)

Caliburn.Micro在视图模型之间传递消息Event Aggregator

在这种情况下,您可以向按钮调用的ShellViewModel添加方法。这会在FooViewModel可以监听的事件聚合器上发布消息。

答案 1 :(得分:0)

答案 2 :(得分:0)

我发现解决方案工作正常,但也许不是正确的方法。如果不是正确的方法指导掌握。

    //Register with Windsor
    public class ViewInstaller : IWindsorInstaller
    {
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            container.Register(Component.For<IShellViewModel>().ImplementedBy<ShellViewModel>().LifestyleSingleton());
            container.Register(Component.For<IFooViewModel >().ImplementedBy<FooViewModel>().LifestyleSingleton());
        }
    }

<Window x:Class="CM.Views.ShellView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">

<Button cal:Message.Attach="[Event Click]=[Action About]" cal:Action.Target="CM.ViewModels.FooViewModel"/>
</Window>

namespace CM.ViewModels
 {
    public class ShellViewModel : Conductor<IScreen>.Collection.OneActive, IShellViewModel
    {
        public ShellViewModel()
        {
        }
   }

   public class FooViewModel : IFooViewModel
   {
         public void About()
         {
             MessageBox.Show("About method fired");
         }
   }
}