0

I am trying to implement a passwordbox within a pivotitem with content dialog. I want to find out if upon clicking on the pivotitem using pointerpressed is suitable to trigger the dialog for password? Also how do handle the event once the pivotitem is clicked. Thanks.

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>

Update - 24-11-2017 I am not sure if this is the best was to verify my password. I also wanna find out after the content dialog closes, where do i further expand the XAML code? Hope I am expressing my scenario clearly here. Thanks.

<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;
    }
mylim
  • 313
  • 4
  • 16

1 Answers1

1

Yes, it's suitable to trigger the dialog for password by using PointerPressed event. When the pointer indicates a press action (such as a touch down, mouse button down, pen down, or touchpad button down) within the bounding area of the PivotItem element, this event method will be invoked. Please see below codes:

*.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
        }     
    }
Michael Xu
  • 4,382
  • 1
  • 8
  • 16
  • Thanks. Is there a way to activate virtual keyboard for passwordbox? once password is accepted, how do I ensure any clicking on the pivotitem will not cause the password dialog to appear again? – mylim Nov 22 '17 at 09:47
  • You can use **Device Portal** to activate virtual keyboard(Device Settings->On-screen Keyboard, select the checkbox). And you can set a variable as a flag to indicate when need to show the password dialog. – Michael Xu Nov 23 '17 at 01:05
  • Thanks it works. I have additional question on the password `content dialog`.. as my password `content dialog` is in `pivotitem`, how do i expand my code in the same `pivotitem` after the `content dialog` closes? – mylim Nov 23 '17 at 16:00
  • I have edited the code,please look at **PivotItem_PointerPressed** event method. – Michael Xu Nov 24 '17 at 01:10
  • Thanks Micheal. After the password is **granted** .. how do i ensure the `dialog` does not pop up again when **pivotitem_pointerpressed** is activated? & I can't expand the `XAML` code outside `contentdialog` in the **settings** `pivotitem`. – mylim Nov 24 '17 at 03:22