【问题标题】:Windows IoT UWP Pivot Item with PasswordBox in a Content Dialog内容对话框中带有 PasswordBox 的 Windows IoT UWP 透视项
【发布时间】:2018-05-04 18:11:03
【问题描述】:

我正在尝试在带有内容对话框的数据透视项中实现密码框。 我想知道在使用pointerpressed单击数据透视项时是否适合触发密码对话框? 此外,一旦单击枢轴项,如何处理事件。 谢谢。

XAML;

Title="Login"
PrimaryButtonText="OK"
SecondaryButtonText="Cancel"
PrimaryButtonClick="ContentDialog_PrimaryButtonClick"
SecondaryButtonClick="ContentDialog_SecondaryButtonClick">

<Grid>
    <StackPanel>
        <PasswordBox x:Name="passwordBox" Width="300" Height="30" PlaceholderText="Enter password" PasswordChar="*"
                     PasswordChanged="PasswordBox_PasswordChanged"/>
        <TextBlock x:Name="statusText" Margin="3,10,2,10" Height="22"/>
    </StackPanel>
</Grid>

更新 - 2017 年 11 月 24 日 我不确定这是否最好是验证我的密码。我还想知道content dialog 关闭后,我在哪里进一步扩展 XAML 代码? 希望我在这里清楚地表达我的情况。谢谢。

<PivotItem Header="Settings" x:Name="settings" PointerPressed="settings_PointerPressed" >
                <ContentDialog Title="Login" x:Name="loginDialog"
                               PrimaryButtonText="OK"
                               SecondaryButtonText="Cancel"
                               PrimaryButtonClick="OK_PrimaryButtonClick"
                               SecondaryButtonClick="Cancel_SecondaryButtonClick">
                    <Grid>
                        <StackPanel>
                            <PasswordBox x:Name="passwordBox" Width="300" Height="40" PlaceholderText="Enter PIN" PasswordChar="*"
                                         PasswordChanged="passwordBox_PasswordChanged" IsPasswordRevealButtonEnabled="False">
                                <PasswordBox.InputScope>
                                    <InputScope>
                                        <InputScope.Names>
                                            <InputScopeName NameValue="NumericPin"/>
                                        </InputScope.Names>
                                    </InputScope>
                                </PasswordBox.InputScope>
                            </PasswordBox>
                            <TextBlock x:Name="passwordStatus" Margin="3,10,2,10" Height="22"/>
                        </StackPanel>
                    </Grid>
                </ContentDialog>

              </PivotItem>

        private async void settings_PointerPressed(object sender, PointerRoutedEventArgs e)
    {
        if(isPasswordGranted==false)
        {
            await loginDialog.ShowAsync();

            //PasswordBox passwordBox = new PasswordBox();
            //passwordBox.Header = "Enter password";

            InputScope scope = new InputScope();
            InputScopeName scopeName = new InputScopeName();
            scopeName.NameValue = InputScopeNameValue.NumericPin; //default = Password
            scope.Names.Add(scopeName);
            passwordBox.InputScope = scope;
        }


    }

   private void OK_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
    {
        if (passwordBox.Password == pinNumber)
        {
            passwordStatus.Text = "Password Granted!";
            isPasswordGranted = true;
        }
        else pivot.SelectedItem = home;
    }

    private void Cancel_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
    {
        pivot.SelectedItem = home;
    }

【问题讨论】:

    标签: c# uwp windows-10-iot-core passwordbox inputscope


    【解决方案1】:

    是的,使用PointerPressed事件触发密码对话框是合适的。当指针在 PivotItem 元素的边界区域内指示按下操作(如按下、按下鼠标按钮、按下笔或按下触摸板按钮)时,将调用此事件方法。请看以下代码:

    *.XAML

        <PivotItem Header="PivotItem Header - 1" PointerPressed="PivotItem_PointerPressed">
            <ContentDialog Title="Login" x:Name="contentDialogForPwd"
                    PrimaryButtonText="OK"
                    SecondaryButtonText="Cancel"
                    PrimaryButtonClick="ContentDialog_PrimaryButtonClick"
                    SecondaryButtonClick="ContentDialog_SecondaryButtonClick">
    
                <Grid>
                    <StackPanel>
                        <PasswordBox x:Name="passwordBox" Width="300" Height="30" PlaceholderText="Enter password" PasswordChar="*"
                     PasswordChanged="passwordBox_PasswordChanged"/>
                        <TextBlock x:Name="statusText" Margin="3,10,2,10" Height="22"/>
                    </StackPanel>
                </Grid>
            </ContentDialog>
        </PivotItem>     
    

    *.CS

        private async void PivotItem_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            ContentDialogResult result = await contentDialogForPwd.ShowAsync();
            if(result == ContentDialogResult.Primary)
            {
                //To do something when clicking Primary button
            }
            else if (result == ContentDialogResult.Secondary)
            {
                //To do something when clicking Secondary button
            }     
        }
    

    【讨论】:

    • 谢谢。有没有办法为密码框激活虚拟键盘?一旦密码被接受,我如何确保任何点击数据透视项都不会导致密码对话框再次出现?
    • 您可以使用设备门户来激活虚拟键盘(设备设置->屏幕键盘,选中复选框)。并且您可以设置一个变量作为标志来指示何时需要显示密码对话框。
    • 谢谢它的工作。我对密码content dialog.. 有其他问题。因为我的密码content dialogpivotitem 中,我如何在content dialog 关闭后在同一个pivotitem 中扩展我的代码?
    • 我已经编辑了代码,请看PivotItem_PointerPressed事件方法。
    • 谢谢迈克尔。 granted 密码后..当 pivotiem_pointerpressed 被激活时,我如何确保dialog 不会再次弹出? & 我无法在 settings pivotitem 中将 XAML 代码扩展到 contentdialog 之外。
    猜你喜欢
    • 2016-12-20
    • 2021-09-20
    • 1970-01-01
    • 2016-11-25
    • 2016-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    相关资源
    最近更新 更多