C# 多视图模型:将事件传递给其他视图模型
我在C# 多视图模型:将事件传递给其他视图模型,c#,wpf,mvvm,caliburn.micro,C#,Wpf,Mvvm,Caliburn.micro,我在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]"/> </Win
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");
}
}
}
命名空间CM.ViewModels
{
公共类ShellViewModel:Conductor.Collection.OneActive,IShellViewModel
{
公共ShellViewModel()
{
}
}
公共类视图模型
{
关于()的公开声明
{
Show(“关于激发的方法”);
}
}
}
您可以使用中介模式,如Marlon Grech和Josh Smith撰写的文章中所述:
我发现解决方案很好,但可能不是正确的方法。如果它不是正确的进近引导大师
//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");
}
}
}
//向温莎注册
公共类ViewInstaller:IWindsorInstaller
{
public void安装(IWindsorContainer、IConfigurationStore)
{
container.Register(Component.For().ImplementedBy().LifestyleSingleton());
container.Register(Component.For().ImplementedBy().LifestyleSingleton());
}
}
命名空间CM.ViewModels
{
公共类ShellViewModel:Conductor.Collection.OneActive,IShellViewModel
{
公共ShellViewModel()
{
}
}
公共类FooViewModel:IFooViewModel
{
关于()的公开声明
{
Show(“关于激发的方法”);
}
}
}
Caliburn.Micro具有在视图模型之间传递消息的功能
在这个场景中,您可以向按钮调用的
ShellViewModel
添加一个方法。这会在事件聚合器上发布一条消息,FooViewModel
可以侦听该消息。啊,对不起,我自己不使用该消息。我可能会看看codeplex.com上Caliburn.Micro文档中的“事件聚合器”。我认为viewmodel为视图提供数据和命令。但在本例中,您希望在另一个视图中为按钮发出命令。你不认为它打破了mvvm的观念吗?