Windows IoT UWP在内容对话框中使用PasswordBox透视项目

时间:2017-11-21 10:11:08

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

我正在尝试使用内容对话框在pivotitem中实现密码框。 我想知道使用pointerpressed点击pivotitem是否适合触发密码对话框? 另外,在单击pivotitem后如何处理事件。 谢谢。

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>

更新 - 24-11-2017 我不确定这是最好的是验证我的密码。我还想在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;
    }

1 个答案:

答案 0 :(得分: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
        }     
    }