无法在Flyout中的TextBox控件中输入输入文本

M.S*_*ack 14 c# xaml commandbar flyout uwp

我想使用CommandBar和a Flyout来构建这样的东西.

搜索弹出窗口

用户应单击CommandBar(Flyout打开)中的按钮,然后在其中输入文本TextBox,然后单击右侧的按钮TextBox以启动搜索请求.问题是,当我单击TextBox时,我无法输入文本.在我写东西之前,它似乎失去了焦点.下面是示例代码.怎么了?

<Page.Resources>
    <DataTemplate x:Key="Search">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="200" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <TextBox Grid.Column="0" />
            <AppBarButton Grid.Column="1" Icon="Find" />
        </Grid>
    </DataTemplate>
</Page.Resources>

<Grid>
    <CommandBar RequestedTheme="Dark">
        <AppBarButton Icon="Find">
            <AppBarButton.Flyout>
                <Flyout Placement="Bottom" >
                    <ContentPresenter ContentTemplate="{StaticResource Search}"/>
                </Flyout>
            </AppBarButton.Flyout>
        </AppBarButton>
    </CommandBar>
</Grid>
Run Code Online (Sandbox Code Playgroud)

Art*_*ous 16

设置AllowFocusOnInteraction属性trueAppBarButton.

XAML中的解决方案(适用于Windows 10,版本1607)

    <AppBarButton x:Name="myAppBarButton"
                  Icon="Find"
                  AllowFocusOnInteraction="True">
        <AppBarButton.Flyout>
            <Flyout Placement="Bottom" >
                <ContentPresenter ContentTemplate="{StaticResource Search}"/>
            </Flyout>
        </AppBarButton.Flyout>
    </AppBarButton>
Run Code Online (Sandbox Code Playgroud)

或者,如果您的目标是Windows 10周年更新(1607)内部版本为14393或更高版本,但应用程序的最低Windows 10版本较低,则应检查该AllowFocusOnInteraction属性是否在平台上可用.

因此,您无法AllowFocusOnInteraction在XAML中设置该属性.相反,在代码隐藏中执行此操作:

C#代码隐藏中的解决方案

if (Windows.Foundation.Metadata.ApiInformation.IsPropertyPresent("Windows.UI.Xaml.FrameworkElement", "AllowFocusOnInteraction"))
     myAppBarButton.AllowFocusOnInteraction = true;
Run Code Online (Sandbox Code Playgroud)

您还可以将其包装到附加属性中,即使在所有Windows 10版本上也可以在XAML中使用.

更多信息

您正在使用Windows 10周年更新(1607),构建14393 上的新功能.

对于大多数应用栏使用而言,这是一个改进但会干扰您的应用,因此当您将构建更改为14393而不是10586时,您需要覆盖默认值.

这是一篇关于附加到AppBarButton的弹出窗口上的ComboBox的博客文章,在1607上丢失了鼠标输入.它还包含附加的属性实现.