Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';t在文本框控件内部弹出按钮中输入文本_C#_Xaml_Uwp_Flyout_Commandbar - Fatal编程技术网

C# Can';t在文本框控件内部弹出按钮中输入文本

C# Can';t在文本框控件内部弹出按钮中输入文本,c#,xaml,uwp,flyout,commandbar,C#,Xaml,Uwp,Flyout,Commandbar,我想使用命令栏和弹出按钮来构建类似的东西 用户应单击命令栏中的按钮(弹出按钮打开),然后在文本框中输入文本,然后单击文本框右侧的按钮启动搜索请求。 问题是,当我点击文本框时,我无法输入文本。在我写东西之前,它似乎失去了焦点。下面是示例代码。怎么了 <Page.Resources> <DataTemplate x:Key="Search"> <Grid> <Grid.ColumnDefinitions>

我想使用
命令栏和
弹出按钮来构建类似的东西

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

<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>

您的文本框实际上从未获得过焦点,不知何故,弹出按钮阻止了它,我可以从该文本框中获得的唯一操作是指针越过状态-使它看起来像是获得了焦点,但实际上没有

您需要在代码中设置焦点,例如当弹出按钮打开时-它可以工作,但可能不是最好的解决方案,因为您需要命名文本框以便从代码隐藏中获取它

<Grid>
    <CommandBar RequestedTheme="Dark">
        <AppBarButton Icon="Find">
            <AppBarButton.Flyout>
                <Flyout Placement="Bottom" Opened="FlyoutBase_OnOpened">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="200" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <TextBox x:Name="Test"/>
                        <AppBarButton Grid.Column="1" Icon="Find"/>
                    </Grid>
                </Flyout>
            </AppBarButton.Flyout>
        </AppBarButton>
    </CommandBar>
</Grid>

我可以复制这个问题。我认为这是周年更新(1607)SDK(14393)中的一个bug,因为如果我将目标SDK降级到10586,everythink就可以正常工作


注:我不知道如何向Microsoft报告此错误。

AppBarButton
上将
AllowFocusOnInteraction
属性设置为
true

XAML溶液(如果应用程序最小目标版本为10.0.14393或更高版本) 您还可以将其包装为附加属性,即使在旧的Windows 10版本上,该属性也可以在XAML中使用

更多信息 这是Windows 10周年更新(1607)版本14393的一项新功能

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


这里有一篇博文。它还包含附加的属性实现。

我无法复制您的问题。使用您提供的代码,一切正常。你是否在代码隐藏中阻止了文本框?请小心,在触摸屏设备上,屏幕键盘可能位于弹出按钮顶部的一层中,有效地隐藏了文本框。然后,如果要通过单击屏幕上弹出按钮外的区域来隐藏键盘,也将关闭弹出按钮。不是很好的UX这是框架中的一个bug还是在代码背后设置焦点是正常的?看起来像是一个bug,特别是如果Toth Tibor所写的(只有周年更新才会发生)是真的。在代码背后设置焦点更像是“hack”,这绝对不是一个好的解决方案(但仍然有效)。微软是否会解决这个问题,在UWP上还有很多微软很久没有解决的bug,所以我宁愿不要等待。尤其是在没有地方可以发布错误报告的情况下。如果在xaml中设置属性,我会得到这个编译错误。XBF语法错误“0x09C4”:未找到属性。检查您在XAML中设置的属性是否存在于项目中指定的平台的最低版本(TargetPlatformMinVersion)中,这是正确的。正如我在回答中所述,您的目标是Windows 10周年更新(1607)版本14393或更高版本,但该应用程序的最低Windows 10版本较低,您应该检查平台上是否提供AllowFocusOnInteraction属性。因此,不能在XAML中设置AllowFocusOnInteraction属性。另一个解决方案是创建一个包含此行为的附加属性,并在XAML中使用此附加属性
private void FlyoutBase_OnOpened(object sender, object e)
{
    Test.Focus(FocusState.Programmatic);
}
    <AppBarButton x:Name="myAppBarButton"
                  Icon="Find"
                  AllowFocusOnInteraction="True">
        <AppBarButton.Flyout>
            <Flyout Placement="Bottom" >
                <ContentPresenter ContentTemplate="{StaticResource Search}"/>
            </Flyout>
        </AppBarButton.Flyout>
    </AppBarButton>
// check if the AllowFocusOnInteraction property is available on the platform 
if (Windows.Foundation.Metadata.ApiInformation.IsPropertyPresent("Windows.UI.Xaml.FrameworkElement", "AllowFocusOnInteraction"))
     myAppBarButton.AllowFocusOnInteraction = true;