密码输入提示coding4fun - 如果输入的密码错误,请继续显示

时间:2014-03-06 13:48:33

标签: windows-phone-7 windows-phone password-prompt

我正在开发一个Windows Phone 7.1应用程序并在Coding4fun库中使用PasswordInputPrompt控件。  我初始化控件并为Completed事件添加一个EventHandler,然后显示控件。

PasswordInputPrompt passwordInput = new PasswordInputPrompt
    {
        Title = "Application Password",
        Message = "Please Enter App Password",
    };
passwordInput.Completed += Pwd_Entered;
passwordInput.Show();

Completed事件处理程序中,如果密码为空,我会检查,如果是,那么我希望显示提示

    void Pwd_Entered(object sender, PopUpEventArgs<string, PopUpResult> e)
    {
        if (!string.IsNullOrWhiteSpace(passwordInput.Value))
        {
            //Do something
        }
        else
        {
            passwordInput.Show();  //This is not working. Is this the correct way???
        }
    }

else部分不起作用。即使输入的密码为空,提示也会关闭。 有人能告诉我实现这个目标的正确方法吗?

1 个答案:

答案 0 :(得分:0)

我做了一些快速测试,它似乎工作。控件的源代码有

 public virtual void OnCompleted(PopUpEventArgs<T, TPopUpResult> result)
{
  this._alreadyFired = true;
  if (this.Completed != null)
    this.Completed((object) this, result);
  if (this.PopUpService != null)
    this.PopUpService.Hide();
  if (this.PopUpService == null || !this.PopUpService.BackButtonPressed)
    return;
  this.ResetWorldAndDestroyPopUp();
}

暗示您可以覆盖该方法。

因此,创建一个继承自控件的类

public class PasswordInputPromptOveride : PasswordInputPrompt
{
    public override void OnCompleted(PopUpEventArgs<string, PopUpResult> result)
    {
        //Validate for empty string, when it fails, bail out.
        if (string.IsNullOrWhiteSpace(result.Result)) return;


        //continue if we do not have an empty response
        base.OnCompleted(result);
    }
}

在你的代码背后:

 PasswordInputPrompt passwordInput;

    private void PasswordPrompt(object sender, System.Windows.Input.GestureEventArgs e)
    {
        InitializePopup();
    }

    private void InitializePopup()
    {
        passwordInput = new PasswordInputPromptOveride
        {
            Title = "Application Password",
            Message = "Please Enter App Password",
        };

        passwordInput.Completed += Pwd_Entered;
        passwordInput.Show();
    }

    void Pwd_Entered(object sender, PopUpEventArgs<string, PopUpResult> e)
    {
        //You should ony get here when the input is not null.
        MessageBox.Show(e.Result);
    }

Xaml触发密码提示

 <Grid VerticalAlignment="Top" x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
  <Button Content="ShowPasswordPrompt" Tap="PasswordPrompt"></Button>
</Grid>