隐藏在软键盘 UWP 后面的 ContentDialog

ContentDialog hidden behind soft keyboard UWP

我开发了一个 UWP 应用程序,它在 Windows 10 的新版本上有错误行为。在我的应用程序中,我有一些 ContentDialog,其中有一些 TextBox,当显示软键盘时,它应该向上推contentdialog 但在 windows 10 的新版本中这没有发生,有人知道为什么吗? 这是我的 ContentDialog 代码示例:

<ContentDialog
x:Class="ManImp.CDEseguiOdM"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ManImp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
MaxHeight="{StaticResource ContentDialogMaxHeight}"
MaxWidth="{StaticResource ContentDialogMaxWidth}"
Margin="0"
Padding="0">

<Grid Background="{ThemeResource ApplicationBackGroudColor}">

    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"></RowDefinition>
        <!--0-->
        <RowDefinition Height="Auto"></RowDefinition>
        <!--0-->
        <RowDefinition Height="Auto"></RowDefinition>
        <!--1-->
        <RowDefinition Height="Auto"></RowDefinition>
        <!--2-->
        <RowDefinition Height="Auto"></RowDefinition>
        <!--3-->
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"></ColumnDefinition>
    </Grid.ColumnDefinitions>

    <StackPanel Orientation="Vertical" x:Name="stckLabelFirma" Grid.Row="0" Grid.Column="0">
        <TextBlock x:Name="lblFirma" VerticalAlignment="Center" TextWrapping="Wrap" FontSize="24" Foreground="White" FontWeight="Normal" HorizontalAlignment="Center" Margin="0,0,0,5"></TextBlock>
    </StackPanel>

    <StackPanel Orientation="Vertical" Grid.Row="1"  Grid.Column="0">
        <InkToolbar InitialControls="None" VerticalAlignment="Top" HorizontalAlignment="Right" TargetInkCanvas="{x:Bind inkCanvasFirma}" >
            <InkToolbarEraserButton />
            <InkToolbarBallpointPenButton />
        </InkToolbar>

        <Grid BorderBrush="Black" BorderThickness="1">
            <Image Name="imgFirma" Stretch="Uniform"></Image>
            <InkCanvas x:Name="inkCanvasFirma" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"></InkCanvas>
        </Grid>
    </StackPanel>

    <StackPanel Orientation="Vertical" x:Name="stckDatiSupProd" Grid.Row="2" Grid.Column="0">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <!--0-->
                <ColumnDefinition Width="*"></ColumnDefinition>
                <!--1-->
                <ColumnDefinition Width="*"></ColumnDefinition>
                <!--2-->
            </Grid.ColumnDefinitions>

            <StackPanel Orientation="Vertical" Grid.Column="0" Margin="0,10,5,0">
                <TextBlock x:Name="lblCID"  VerticalAlignment="Center" TextWrapping="Wrap" FontSize="20" Foreground="#FF034872" FontWeight="Bold" HorizontalAlignment="Left" Margin="0,0,0,0"></TextBlock>
                <TextBox x:Name="txtCID" BorderBrush="Red" MaxLength="8" TextChanged="txtCID_TextChanged" VerticalAlignment="Center" Background="White" HorizontalAlignment="Stretch"  Margin="0,10,0,0" TextWrapping="Wrap" FontSize="20"></TextBox>
            </StackPanel>

            <StackPanel Orientation="Vertical" Grid.Column="1" Margin="5,10,5,0">
                <TextBlock x:Name="lblAlias"  VerticalAlignment="Center" TextWrapping="Wrap" FontSize="20" Foreground="#FF034872" FontWeight="Bold" HorizontalAlignment="Left" Margin="0,0,0,0"></TextBlock>
                <TextBox x:Name="txtAlias" BorderBrush="Red" TextChanged="txtAlias_TextChanged" VerticalAlignment="Center" Background="White" HorizontalAlignment="Stretch" Margin="0,10,0,0" TextWrapping="Wrap" FontSize="20"></TextBox>
            </StackPanel>

            <StackPanel Orientation="Vertical" Grid.Column="2" Margin="5,10,0,0">
                <TextBlock x:Name="lblPIN"  VerticalAlignment="Center" TextWrapping="Wrap" FontSize="20" Foreground="#FF034872" FontWeight="Bold" HorizontalAlignment="Left" Margin="0,0,0,0"></TextBlock>
                <PasswordBox x:Name="txtPIN" BorderBrush="Red" PasswordChanged="txtPIN_PasswordChanged" VerticalAlignment="Center" Background="White" HorizontalAlignment="Stretch" Margin="0,10,0,0" FontSize="20"></PasswordBox>
            </StackPanel>
        </Grid>
    </StackPanel>

    <StackPanel Orientation="Vertical" Grid.Row="3" Grid.Column="0" Margin="0,10,0,0">
        <TextBlock x:Name="lblErrore" Visibility="Collapsed" VerticalAlignment="Center" TextWrapping="Wrap" FontSize="14" Foreground="Red" FontWeight="Bold" HorizontalAlignment="Left" Margin="0,5,0,0"></TextBlock>
    </StackPanel>

    <StackPanel Orientation="Vertical" Grid.Row="4" Margin="0,10,0,0" Grid.Column="0">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <!--0-->
                <ColumnDefinition Width="*"></ColumnDefinition>
                <!--1-->
            </Grid.ColumnDefinitions>

            <Button x:Name="btnAnnulla" Grid.Column="0"  HorizontalAlignment="Stretch" Margin="0,30,10,0" VerticalAlignment="Top" Click="btnAnnulla_Click"></Button>
            <Button x:Name="btnConfFirmaSupProd" Grid.Column="1" HorizontalAlignment="Stretch" Margin="10,30,0,0" VerticalAlignment="Top" Click="btnConfFirmaSupProd_Click"></Button>
            <Button x:Name="btnConfFirmaOperatore" Grid.Column="1" HorizontalAlignment="Stretch" Margin="10,30,0,0" VerticalAlignment="Top" Click="btnConfFirmaOperatore_Click"></Button>
        </Grid>
    </StackPanel>
</Grid>

这应该是与旧 SDK 有关的问题,当应用程序针对旧版本(如 14393)时。

如果您真的想在 Windows 10 的所有版本上实现此行为。我建议您通过 InputPane.Showing Event and InputPane.Hiding Event 手动处理显示和隐藏事件。然后您可能需要自定义一个看起来像 ContentDialogPopUp 控件。我们不能改变 ContentDialog 的位置,因为它是由系统处理的。但是我们可以改变 PopUp 控件的位置。

当显示屏幕键盘时,将触发显示事件,您可以尝试手动向上移动 PopUp 控件。当键盘隐藏时,只需将 PopUp 控件移回原来的位置即可。

这里是关于如何处理显示和隐藏事件的示例代码:

        protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        InputPane currentInputPane = InputPane.GetForCurrentView();
        // Subscribe to Showing/Hiding events
        currentInputPane.Showing += OnShowing;
        currentInputPane.Hiding += OnHiding;
    }

    protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        InputPane currentInputPane = InputPane.GetForCurrentView();
        // Unsubscribe from Showing/Hiding events
        currentInputPane.Showing -= OnShowing;
        currentInputPane.Hiding -= OnHiding;
    }

    void OnShowing(InputPane sender, InputPaneVisibilityEventArgs e)
    {
        //do something
    }

    void OnHiding(InputPane sender, InputPaneVisibilityEventArgs e)
    {
        //do something
    }

您还可以在此处获取有关触摸键盘的信息:Respond to the presence of the touch keyboard