【问题标题】:Button does not respond for the first click第一次点击按钮没有响应
【发布时间】:2016-09-26 13:40:41
【问题描述】:

我有用户输入个人电话号码的页面。当 TextBox 获得焦点时,会显示虚拟键盘,但是当用户完成输入电话号码并单击“下一步”绿色按钮(对于屏幕截图上的俄语表示抱歉)时,第一次单击“通过”按钮并关闭键盘只有下一次点击才会激活点击事件。 任何想法如何解决这个问题? 也许页面焦点状态存在一些问题,但我真的不知道。

更新: 这是我页面的 XAML

<Grid x:Name="LayoutRoot" Style="{StaticResource GeneralAppBackground}">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    // Control to show the loading state
    <controls:LoadingPageState x:Name="LoadingGrid" Visibility="Collapsed" Grid.RowSpan="3" Canvas.ZIndex="1" TitleOfOperation="Проверка существования кошелька..." />
    <Grid Style="{StaticResource GeneralAppHeaderStyle}">
        <TextBlock Text="Введите номер телефона" FontWeight="Normal" HorizontalAlignment="Center" VerticalAlignment="Center" Style="{StaticResource PageHeaderTextBlockStyle}" />
    </Grid>
    <Grid Grid.Row="1">
        <StackPanel Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="20,0,20,0">
            <TextBlock x:Uid="Greeting" Foreground="#979797" TextAlignment="Center" Margin="0,0,0,10" />
            <Grid Margin="0,0,0,5">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <TextBox Grid.Column="0" PlaceholderText="+7" FontSize="18" BorderThickness="0,0,0,2" BorderBrush="#979797" Background="Transparent" IsReadOnly="True" Margin="0,0,10,0" Template="{StaticResource DefaultTextBoxStyle}"/>
                <TextBox x:Name="UserPhoneNumberInput" Grid.Column="1" IsTextPredictionEnabled="False" IsSpellCheckEnabled="False" PlaceholderText="777 123 12 12" FontSize="18" BorderThickness="0,0,0,2" BorderBrush="#979797" MaxLength="10" Background="Transparent" InputScope="TelephoneNumber" Template="{StaticResource DefaultTextBoxStyle}" TextChanged="UserPhoneNumberInput_TextChanged" />
            </Grid>
            <TextBlock x:Name="HintText" TextWrapping="WrapWholeWords" Foreground="#979797" TextAlignment="Center">
                Ваш телефон будет использоваться в качестве номера кошелька
            </TextBlock>
        </StackPanel>
    </Grid>
    <Button x:Name="NextStepButton" Grid.Row="2" IsEnabled="False" Content="Далее" VerticalAlignment="Bottom" Style="{StaticResource WideGreenButtonStyle}" Click="NextStepButton_Click" />
</Grid>

这是我的按钮点击事件的 C# 代码

private void NextStepButton_Click(object sender, RoutedEventArgs e)
    {
        if (!UserPhoneNumberInput.Text.All(Char.IsDigit) || UserPhoneNumberInput.Text.Length == 0)
        {
            NotifyUser.ShowWrongPhoneNumberFormatMessage();
            return;
        }

        phoneNumber = Helpers.FormatNumber(UserPhoneNumberInput.Text);

        // Activate the ContentLoading ring
        LoadingGrid.Visibility = Visibility.Visible;
        LoadingGrid.IsProgressRingActive = true;

        CheckNumber(phoneNumber);
    }

在这里,如果我的“LoadingGrid”的 XAML 很重要

<Grid x:Name="LayoutRoot" Background="#CC000000">
    <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
        <ProgressRing x:Name="ProgressRing" Width="50" Height="50" Foreground="White" IsActive="{x:Bind IsProgressRingActive, Mode=TwoWay}" />
        <TextBlock x:Name="OperationTitle" Text="{x:Bind TitleOfOperation, Mode=TwoWay}" TextAlignment="Center" FontSize="18" Foreground="White" Margin="0,20,0,0" />
    </StackPanel>
</Grid>

【问题讨论】:

  • 在您发布代码之前似乎不清楚。
  • @Rakitić 我已经用我的代码更新了我的问题。
  • @Henk Holterman,我认为情况并非如此,因为有时按钮单击可以正常工作。这很可能是我的代码中的一个问题,但我不知道它可能在哪里!

标签: wpf win-universal-app uwp windows-10-universal windows-10-mobile


【解决方案1】:

这是设计使然。

当您单击 NextStepButton 按钮时,UserPhoneNumberInput TextBox 失去焦点,然后键盘自动消失。很可能,但根据您的自定义布局模板,NextStepButton 也只是略微向下移动。当您释放鼠标按钮时(假设您使用的是模拟器),此时 NextStepButton 并不存在。这就是它不会触发 Click 事件的原因。如您所知,按钮的默认 ClickModeClickMode.Release。我最近遇到了完全相同的问题。

您可以尝试将 IsTabStop="False" 添加到您的按钮属性以解决此问题。

【讨论】:

  • 非常感谢您的清晰解释!你的回答对我帮助很大。
猜你喜欢
  • 2014-08-28
  • 2019-09-01
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 2019-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多