我无法在Xbox UWP应用弹出窗口中获取选择框

我无法在Xbox UWP应用弹出窗口中获取选择框,uwp,xbox,Uwp,Xbox,我已经创建了一个弹出窗口,上面有一些按钮,但我无法选择这些按钮,而且按钮周围也不会出现XBox默认选择框 <Grid> <StackPanel HorizontalAlignment="Center" Margin="0,50,0,0"> <Button Content="Show Popup Flyout" Click="ShowFlyoutPopup" /> </StackPanel> <P

我已经创建了一个弹出窗口,上面有一些按钮,但我无法选择这些按钮,而且按钮周围也不会出现XBox默认选择框

<Grid>

    <StackPanel HorizontalAlignment="Center" Margin="0,50,0,0">

        <Button Content="Show Popup Flyout" Click="ShowFlyoutPopup" />

    </StackPanel>

    <Popup x:Name="logincontrol1" IsOpen="False" IsLightDismissEnabled="True">

        <Popup.ChildTransitions>

            <TransitionCollection>

                <PaneThemeTransition />

            </TransitionCollection>

        </Popup.ChildTransitions>
        <StackPanel Orientation="Vertical" Background="#313131" Width="1000" Height="500"  x:Name="pop" Margin="0,100,0,0" >

            <StackPanel>
                <GridView ItemsSource="{Binding GamesList}" >
                <GridView.ItemTemplate>
                    <DataTemplate x:Name="testtemp">
                            <Button x:Name="SwapBtn" Command="{Binding TestClick,Mode=OneWay}">
                            <StackPanel Width="202" VerticalAlignment="Top">
                                <Image Source="{Binding ImageUrl}" Height="324"/>
                                <StackPanel Background="Black" Padding="19,9,0,0">
                                    <TextBlock FontWeight="Semibold" TextTrimming="CharacterEllipsis" FontFamily="Segoe Pro" Foreground="White" TextAlignment="Left" FontSize="24" Text="{Binding Title}" TextWrapping="Wrap" Height="65"/>
                                    <TextBlock FontFamily="Segoe Pro" Foreground="White" TextAlignment="Left" FontSize="16" Text="{Binding Price}" Margin="0,48,0,0"/>
                                </StackPanel>
                            </StackPanel>
                        </Button>
                        </DataTemplate>
                </GridView.ItemTemplate>
            </GridView>

            </StackPanel>

        </StackPanel>
    </Popup>

</Grid>

}


焦点不在弹出窗口中,我无法使用gamepad与之交互。

您可以在弹出窗口的一个控件上强制聚焦:

logincontrol1.IsOpen = true;
loginclick.Focus(FocusState.Programmatic);

我会处理弹出窗口的打开事件

<Popup x:Name="logincontrol1" IsOpen="False" IsLightDismissEnabled="True" Opened="Logincontrol1_OnOpened">

弹出数据模板中的控件不会出现在页面/视图的可视树上,它会有自己的可视树,为此,您可以使用VisualTreeHelper获取弹出数据模板中的控件以设置焦点

private void Logincontrol1_OnOpened(object sender, object e)
{
    Popup popup = sender as Popup;
    if (popup != null)
    {
        Button button = FindElementInVisualTree<Button>(mygrid);
        button?.Focus(FocusState.Programmatic);
    }
}

private T FindElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
{
    var count = VisualTreeHelper.GetChildrenCount(parentElement);
    if (count == 0) return null;

    for (int i = 0; i < count; i++)
    {
        var child = VisualTreeHelper.GetChild(parentElement, i);
        if (child != null && child is T)
            return (T)child;
        else
        {
            var result = FindElementInVisualTree<T>(child);
            if (result != null)
                return result;
        }
    }
    return null;
}
private void Logincontrol1\u打开(对象发送方,对象e)
{
Popup Popup=发送方作为弹出窗口;
如果(弹出!=null)
{
Button Button=FindElementInVisualTree(mygrid);
按钮?.Focus(聚焦状态编程);
}
}
私有T FindElementInVisualTree(DependencyObject parentElement),其中T:DependencyObject
{
var count=VisualTreeHelper.GetChildrenCount(parentElement);
if(count==0)返回null;
for(int i=0;i
如果弹出窗口中的内容是使用列表填充的,如何特别设置焦点?我正在使用item.template填充数据。您可以将焦点设置为
列表视图
本身。我已经更新了代码,请检查一下。我有一个gridview.itemtemplate,我可以关注它吗?您可以在
gridview
上设置
x:Name
。如果要访问网格中的项目,请使用
GridViewItem item=ResultGv.ContainerFromIndex(0)作为GridViewItem
然后您可以尝试
FindName
查找焦点中的按钮,这会给我一个空引用异常。有没有更简单的方法?我是UWP的新手。
private void Logincontrol1_OnOpened(object sender, object e)
{
    Popup popup = sender as Popup;
    if (popup != null)
    {
        Button button = FindElementInVisualTree<Button>(mygrid);
        button?.Focus(FocusState.Programmatic);
    }
}

private T FindElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
{
    var count = VisualTreeHelper.GetChildrenCount(parentElement);
    if (count == 0) return null;

    for (int i = 0; i < count; i++)
    {
        var child = VisualTreeHelper.GetChild(parentElement, i);
        if (child != null && child is T)
            return (T)child;
        else
        {
            var result = FindElementInVisualTree<T>(child);
            if (result != null)
                return result;
        }
    }
    return null;
}