2014-03-06 73 views
0

我正在開發一個Windows Phone 7.1應用程序並在Coding4fun庫中使用PasswordInputPrompt控件。 我初始化控件併爲Completed事件添加EventHandler,然後顯示控件。密碼輸入提示編碼4fun - 如果輸入的密碼錯誤,請繼續顯示

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部分不起作用。即使輸入的密碼爲空,提示也會關閉。 有人能告訴我實現這個的正確方法嗎?

回答

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); 
    } 
} 

繼承在後面的代碼A類:

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>