Caliburn.Micro - 将侧边栏中的按钮绑定到 ViewModel 中的方法
Caliburn.Micro - Binding a button in a sidebar to a method in a ViewModel
我在 windows phone 应用程序的边栏中绑定按钮时遇到问题。按钮绑定似乎消失了..
这是我目前的代码
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<sidebar:SidebarControl x:Name="sidebarControl"
HeaderText="WP"
HeaderBackground="YellowGreen"
HeaderForeground="White"
SidebarBackground="{StaticResource PhoneChromeBrush}">
<sidebar:SidebarControl.SidebarContent>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="380">
<Button Content="Go to page 2" x:Name="GoToPage2"/>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
</Grid>
</sidebar:SidebarControl.SidebarContent>
<Grid VerticalAlignment="Top" HorizontalAlignment="Stretch"
Margin="12">
<TextBlock Style="{StaticResource PhoneTextNormalStyle}">Your current view goes here</TextBlock>
</Grid>
</sidebar:SidebarControl>
</Grid>
</Grid>
目前我正在为名为 SidebarWP8 的侧边栏使用 nuget。也许 Calinbrun.Micro 不适用于此?或者我是否必须在网格中插入到 VM 的绑定?
这是 ViewModel 中的方法:
private readonly INavigationService navigationService;
public MainPageViewModel(INavigationService navigationService)
{
this.navigationService = navigationService;
}
public void GoToPage2()
{
navigationService.UriFor<Page2ViewModel>().Navigate();
}
我检查了 Caliburn Micro 的源代码:我通过遍历可视化树来查找具有名称的元素来处理约定绑定。但它只检查每个控件的默认 Content 属性。
这意味着它只会通过 Content 或 Children 元素,而不是像 SidebarContent[= 这样的自定义属性21=],所以在那里找不到命名元素。
您必须手动连接它(通过绑定命令或添加 click 处理程序)。
<Button cm:Message.Attach="[Event Click] = [Action GoToPage2()]" />
这应该可行,因为其他评论者关于默认控件是正确的...自定义控件需要添加更多处理,这可能会很痛苦,但用 CM 以上的短手来完成将在按钮中查找 属性 并进行相应处理。
我在 windows phone 应用程序的边栏中绑定按钮时遇到问题。按钮绑定似乎消失了..
这是我目前的代码
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<sidebar:SidebarControl x:Name="sidebarControl"
HeaderText="WP"
HeaderBackground="YellowGreen"
HeaderForeground="White"
SidebarBackground="{StaticResource PhoneChromeBrush}">
<sidebar:SidebarControl.SidebarContent>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="380">
<Button Content="Go to page 2" x:Name="GoToPage2"/>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
</Grid>
</sidebar:SidebarControl.SidebarContent>
<Grid VerticalAlignment="Top" HorizontalAlignment="Stretch"
Margin="12">
<TextBlock Style="{StaticResource PhoneTextNormalStyle}">Your current view goes here</TextBlock>
</Grid>
</sidebar:SidebarControl>
</Grid>
</Grid>
目前我正在为名为 SidebarWP8 的侧边栏使用 nuget。也许 Calinbrun.Micro 不适用于此?或者我是否必须在网格中插入到 VM 的绑定?
这是 ViewModel 中的方法:
private readonly INavigationService navigationService;
public MainPageViewModel(INavigationService navigationService)
{
this.navigationService = navigationService;
}
public void GoToPage2()
{
navigationService.UriFor<Page2ViewModel>().Navigate();
}
我检查了 Caliburn Micro 的源代码:我通过遍历可视化树来查找具有名称的元素来处理约定绑定。但它只检查每个控件的默认 Content 属性。
这意味着它只会通过 Content 或 Children 元素,而不是像 SidebarContent[= 这样的自定义属性21=],所以在那里找不到命名元素。
您必须手动连接它(通过绑定命令或添加 click 处理程序)。
<Button cm:Message.Attach="[Event Click] = [Action GoToPage2()]" />
这应该可行,因为其他评论者关于默认控件是正确的...自定义控件需要添加更多处理,这可能会很痛苦,但用 CM 以上的短手来完成将在按钮中查找 属性 并进行相应处理。