0

我正在开发一个 Windows Phone 7.1 应用程序并PasswordInputPrompt在 Coding4fun 库中使用控件。我初始化控件并为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部分不工作。即使输入的密码为空,提示也会关闭。有人可以告诉我实现这一目标的正确方法吗?

4

1 回答 1

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>
于 2014-03-06T14:37:13.390 回答