Caliburn.Micro: Binding Button in a Screen to a command in ScreenConductor
我正在关注
我使用的是WPF,而不是Silverlight,并且在App.xaml中进行了相应的更改(对Bootstrapper使用MergedDictionary)。
原始的"简单导航"示例具有一个带有两个按钮的外壳,以及一个内容区域,其中显示了由
然后,我尝试将每个按钮移至其对应的"视图",以便PageOne可以将一个按钮移至PageTwo,反之亦然。我这样做是因为我不希望Home Shell在整个应用程序中不断"展示其内幕"。
事实是,如果我只是将按钮移动到
我面临的症状是:当我运行该应用程序时,将显示PageOneView,其中带有"转到第二页"按钮,但是当我单击该按钮时,什么也没有发生。
我问的问题是:"如何正确地将ScreenView.xaml中的按钮"泡"到ScreenConductorViewModel.cs中的某个动作上?
我当前的代码如下:
PageOneView.xaml
1 2 3 4 5 6 7 8 9 10 11 | <UserControl x:Class="ScreenConductor.PageOneView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Grid Background="LightGreen"> <Button x:Name="ShowPageTwo" Content="Show Page Two" HorizontalAlignment="Center" VerticalAlignment="Top" /> </Grid> </UserControl> |
PageOneViewModel
1 2 3 4 5 6 7 8 9 | using Caliburn.Micro; namespace ScreenConductor { public class PageOneViewModel : Screen { protected override void OnActivate() { base.OnActivate(); } } } |
ShellView.xaml
1 2 3 4 5 6 7 | <Window x:Class="ScreenConductor.ShellView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <ContentControl x:Name="ActiveItem" /> </Window> |
ShellViewModel.cs
使用Caliburn.Micro;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | namespace ScreenConductor { public class ShellViewModel : Conductor<object> { public ShellViewModel() { ShowPageOne(); } public void ShowPageOne() { ActivateItem(new PageOneViewModel()); } public void ShowPageTwo() { ActivateItem(new PageTwoViewModel()); } } } |
只要您使用显式操作绑定语法绑定命令,冒泡就可以正常工作。这是因为按名称绑定,CM会检查绑定到当前vm的视图上的控件。如果没有匹配的命名控件,则不会创建绑定。
您可以使用此:
1 | <SomeControl cal:Message.Attach="SomeMethodOnParentVM" /> |
这将尝试使消息在控制层次结构中冒泡,直到找到合适的处理程序为止。请注意,如果找不到处理程序,这将引发异常。我记得有一个选项可以关闭绑定,但是您可能要检查一下文档(目前我正在通过手机提供信息:-)
编辑:
好吧,如果您想在幕后提供更多信息,最好的检查地方实际上就是来源。作者Rob Eisenberg提到源代码非常小(我认为大约2700行代码),因此易于检查且易于掌握
值得阅读CodePlex站点上的所有文档(虽然页面很少,但它们解释得足够详细,以便您将其余部分组合在一起)。
不确定您对包含
http://caliburnmicro.codeplex.com/SourceControl/changeset/view/35582bb2a8dfdd3fcd71a07fa82581ddb93a786f#src/Caliburn.Micro.Silverlight/Message.cs
(是的,它是Silverlight的源代码,但这是所有其他版本的基础,并且只是一些编译器指令以及其他版本(例如,WPF / WinRT)中出现的一些其他类)
如果您查看源代码,则可以看到设置了attached属性后,
1 | <Button cal:Message.Attach="[Event Click] = [Action SomeButtonWasClicked()]" /> |
要么
1 | <Button cal:Message.Attach="[Event MouseEnter] = [Action MouseEnteredAButton($eventargs)" /> |
在上方可以看到使用了
您还可以传递其他控件的CM默认属性,例如
1 2 | <TextBox x:Name="TextBox1" /> <Button cal:Message.Attach="MouseClicked(TextBox1)" /> |
通过检查可视化树并尝试绑定到每个级别来进行冒泡(在
这很简单,但很有效(它只使用
不知道您可能还需要什么:P信息
如果您尚未引用
使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <UserControl x:Class="ScreenConductor.PageOneView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:cal="http://www.caliburnproject.org" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Grid> <Button Content="ShowPageTwo"> <i:Interaction.Triggers> <i:EventTrigger EventName="Click"> <cal:ActionMessage MethodName="ShowPageTwo" /> </i:EventTrigger> </i:Interaction.Triggers> </Button> </Grid> </UserControl> |
您可以在这里找到更多有关操作的信息,CodePlex上的官方文档。
动作消息将冒泡到导体上,如果找不到合适的方法,则会抛出