AppBar 中按钮的绑定命令不起作用

CEv*_*per 5 c# binding winrt-xaml

为什么下面例子中的Command不执行?

我有一个带有 AppBar 和 Button 的命名页面:

   <Page
    x:Class="App13.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" 
    x:Name="myPage"
    >
    <Page.BottomAppBar>
        <AppBar>
           <Button Content="OK" Command="{Binding OkCommand, ElementName=myPage}" />
        </AppBar>
    </Page.BottomAppBar>
</Page>
Run Code Online (Sandbox Code Playgroud)

命令“OkCommand”在代码隐藏中定义如下(使用 MVVM 轻型框架):

public RelayCommand OkCommand
{
    get
    {
        return m_OkCommand
            ?? (m_OkCommand = new RelayCommand(
                                  async () =>
                                  {
                                      await new MessageDialog("OkCommand").ShowAsync();
                                  }));
    }
}
Run Code Online (Sandbox Code Playgroud)

输出窗口中没有绑定错误或任何其他提示,让我知道为什么这不起作用。(另外:如果按钮放在 AppBar 之外,一切正常)

有谁知道这里有什么问题?

Dam*_*Arh 3

AppBar如果命令绑定适用于页面上其他位置的按钮,则应该没有理由对按钮不起作用。

我怀疑问题与如何DataContext设置有关。您应该将其设置在页面级别而不是控制树的较低级别。所有其他按钮都位于页面的顶部内容控件内,而 则位于页面的外部,如果设置在顶部内容控件或下方,AppBar则会导致绑定不起作用。DataContext

您可以尝试使用以下工作示例:

MainPage.xaml:

<common:LayoutAwarePage
    x:Class="App16.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App16"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:common="using:App16.Common"
    x:Name="MyPageName"
    mc:Ignorable="d">

    <StackPanel x:Name="MainGrid" Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <Button Content="Ok" Command="{Binding OkCommand}" />
    </StackPanel>

    <Page.BottomAppBar>
        <AppBar x:Name="bottomAppBar" Padding="10,10,10,10"  >
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                <Button Style="{StaticResource SkipBackAppBarButtonStyle}" Command="{Binding OkCommand}" >
                </Button>
            </StackPanel>
        </AppBar>
    </Page.BottomAppBar>
</common:LayoutAwarePage>
Run Code Online (Sandbox Code Playgroud)

ViewModel.cs:

public class ViewModel
{
    private RelayCommand _okCommand;
    public RelayCommand OkCommand
    {
        get
        {
            return _okCommand
                ?? (_okCommand = new RelayCommand(
                                      async _ =>
                                      {
                                          await new MessageDialog("OkCommand").ShowAsync();
                                      }));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

MainPage.xaml.cs:

public sealed partial class MainPage : LayoutAwarePage
{
    public MainPage()
    {
        this.InitializeComponent();
        DataContext = new ViewModel();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @CEvenSharper 因为看起来“AppBar”元素看不到页面的其余部分,因此“ElementName”绑定不起作用。由于您的“Command”位于代码后面,因此您可以直接使用“Click”事件处理程序而不是“Command”,除非您还需要“CanExecute”来禁用它。或者您可以创建一个单独的“ViewModel”,我通常发现这是一个更好的想法而不是解决方法。 (2认同)