2012-09-20 2 views
0

Je bouton:validation au cours Cliquez sur le bouton

<sdk:Frame Margin="0,37,0,-15"  
        Source="/View/Login/LogIn.xaml"> 
      <navigation:Frame.UriMapper> 
       <uriMapper:UriMapper> 
        <uriMapper:UriMapping Uri="/Login" MappedUri="/View/Login/LogIn.xaml" /> 
        <uriMapper:UriMapping Uri="/Home" MappedUri="/View/Home.xaml" /> 
        <uriMapper:UriMapping Uri="/AddPayment" MappedUri="/View/AddPayment.xaml" /> 
        <uriMapper:UriMapping Uri="/Reports" MappedUri="/View/Reports.xaml" /> 
        <uriMapper:UriMapping Uri="/Admin" MappedUri="/View/Admin.xaml" /> 
        <uriMapper:UriMapping Uri="/Init" MappedUri="/View/Init.xaml" /> 
       </uriMapper:UriMapper> 
      </navigation:Frame.UriMapper> 
     </sdk:Frame> 
<HyperlinkButton Grid.Row="2" Grid.Column="1" Content="Log in" NavigateUri="/Home" Command="{Binding LoginCommand, NotifyOnValidationError=True}"/> 

et le modèle de vue:

public RelayCommand LoginCommand 
     { 
      get 
      { 
       return this.loginCommand = new RelayCommand(param => this.LoginExecute(), param => (bool)this.CanLoginCommand); 
      } 
     } 

     private object CanLoginCommand 
     { 
      get 
      { 
       return true; 
      } 
     } 

     private void LoginExecute() 
     { 
      UserBag users = IsolatedStorageCacheManager<UserBag>.Retrieve(typeof(UserBag)); 
      if (users != null && users.Users != null && users.Users.Any(u => u.UserName == this.login && u.Password == this.password)) 
      { 

      } 
      else 
      { 
       throw new Exception("Bad password"); 
      } 
     } 

J'ai besoin que lorsque l'exception du retour de la commande, il sera revenir à la vue et affiché puis uri pas naviguer sera être invoqué. C'est possible?

+0

Vous pouvez lier la propriété Visibility du lien hypertexte à une propriété dans votre modèle de vue et la masquer lorsque vous obtenez une exception dans votre méthode LoginExecute(). –

+0

Lorsque je cache le bouton pendant la commande, hiperlink ne sera pas invoqué? – netmajor

+0

J'ai mal compris. Voulez-vous annuler la navigation que le bouton a appelée? –

Répondre

0

vous pouvez gérer la navigation dans votre ViewModel avec cette classe statique:

public static class NavigationService 
{ 
    private static Frame _frame; 
    private static Frame Instance 
    { 
     get 
     { 
      if (_frame == null) 
       throw new Exception("You must set the instance first"); 

      return _frame; 
     } 
    } 


    public static void Navigate(string url) 
    { 
     Instance.Navigate(new Uri(url, UriKind.Relative)); 
    } 

    public static void SetInstance(Frame frame) 
    { 
     _frame = frame; 
    } 

    public static void GoBack() 
    { 
     Instance.GoBack(); 
    } 
} 

Dans votre MainPage.xaml.cs:

NavigationService.SetInstance(this.ContentFrame); // ContentFrame = your navigation Frame 

Un moment dans votre ViewModel:

private void LoginExecute() 
    { 
     UserBag users = IsolatedStorageCacheManager<UserBag>.Retrieve(typeof(UserBag)); 
     if (users != null && users.Users != null && users.Users.Any(u => u.UserName == this.login && u.Password == this.password)) 
     { 
      NavigationService.Navigate("/Home"); 
     } 
     else 
     { 
      throw new Exception("Bad password"); 
     } 
    } 

Vous devez retirer le NavigateUri dans votre HyperlienButto n;)