2016-03-05 20 views
6

Sto lavorando su un'applicazione cross platform xamarin e voglio creare un'etichetta hyperlink per "Password dimenticata?" sulla pagina di accesso. Ho usato il seguente codice per creare l'etichetta ma non so come creare eventi onclick su di esso.Come creare un evento click sull'etichetta nei moduli xamarin dinamicamente

MainPage = new ContentPage 
      { 
       BackgroundImage = "background.png", 
       Content = new StackLayout 
       { 
        VerticalOptions = LayoutOptions.CenterAndExpand, 
        HorizontalOptions = LayoutOptions.CenterAndExpand, 
        Spacing = 50, 
        Children = { 

         new Label { 
          HorizontalTextAlignment = TextAlignment.Center, 
          Text = "Welcome, Please Sign in!", 
          FontSize=50, 
          TextColor=Color.Gray, 
         }, 


         new Entry 
         { 
          Placeholder="Username", 
          VerticalOptions = LayoutOptions.Center, 
          Keyboard = Keyboard.Text, 
          HorizontalOptions = LayoutOptions.Center, 
          WidthRequest = 350, 
          HeightRequest = 50, 
          FontSize=20, 
          TextColor=Color.Gray, 
          PlaceholderColor=Color.Gray, 
         }, 

          new Entry 
         { 
          Placeholder="Password", 
          VerticalOptions = LayoutOptions.Center, 

          Keyboard = Keyboard.Text, 
          HorizontalOptions = LayoutOptions.Center, 
          WidthRequest = 350, 
          HeightRequest = 50, 
          FontSize=25, 
          TextColor=Color.Gray, 
          IsPassword=true, 
           PlaceholderColor =Color.Gray, 
         }, 
         new Button 
         { 
          Text="Login", 
          FontSize=Device.GetNamedSize(NamedSize.Large,typeof(Button)), 
          HorizontalOptions=LayoutOptions.Center, 
          VerticalOptions=LayoutOptions.Fill, 
          WidthRequest=350, 
          TextColor=Color.Silver, 
          BackgroundColor=Color.Red, 
          BorderColor=Color.Red, 
         }, 
         new Label //for this label I want to create click event to open new page 
         { 
          Text="Forgot Password?", 
          FontSize=20, 
          TextColor=Color.Blue, 
          HorizontalOptions=LayoutOptions.Center, 

         }, 
        } 
     } 
      }; 

risposta

9

Prova questo:

 var forgetPasswordLabel = new Label // Your Forget Password Label 
     { 
      Text = "Forgot Password?", 
      FontSize = 20, 
      TextColor = Color.Blue, 
      HorizontalOptions = LayoutOptions.Center, 
     }; 


     // Your label tap event 
     var forgetPassword_tap = new TapGestureRecognizer(); 
     forgetPassword_tap.Tapped += (s,e) => 
     { 
      // 
      // Do your work here. 
      // 
     }; 
     forgetPasswordLabel.GestureRecognizers.Add(forgetPassword_tap); 

Esempio:

 var forgetPasswordLabel = new Label // Your Forget Password Label 
     { 
      Text = "Forgot Password?", 
      FontSize = 20, 
      TextColor = Color.Blue, 
      HorizontalOptions = LayoutOptions.Center, 
     }; 

     MainPage = new ContentPage 
     { 
      BackgroundImage = "background.png", 
      Content = new StackLayout 
      { 
       VerticalOptions = LayoutOptions.CenterAndExpand, 
       HorizontalOptions = LayoutOptions.CenterAndExpand, 
       Spacing = 50, 
       Children = { 

        new Label { 
         //HorizontalTextAlignment = TextAlignment.Center, 
         Text = "Welcome, Please Sign in!", 
         FontSize=50, 
         TextColor=Color.Gray, 
        }, 


        new Entry 
        { 
         Placeholder="Username", 
         VerticalOptions = LayoutOptions.Center, 
         Keyboard = Keyboard.Text, 
         HorizontalOptions = LayoutOptions.Center, 
         WidthRequest = 350, 
         HeightRequest = 50, 
         FontSize=20, 
         TextColor=Color.Gray, 
         PlaceholderColor=Color.Gray, 
        }, 

        new Entry 
        { 
         Placeholder="Password", 
         VerticalOptions = LayoutOptions.Center, 

         Keyboard = Keyboard.Text, 
         HorizontalOptions = LayoutOptions.Center, 
         WidthRequest = 350, 
         HeightRequest = 50, 
         FontSize=25, 
         TextColor=Color.Gray, 
         IsPassword=true, 
         PlaceholderColor =Color.Gray, 
        }, 
        new Button 
        { 
         Text="Login", 
         FontSize=Device.GetNamedSize(NamedSize.Large,typeof(Button)), 
         HorizontalOptions=LayoutOptions.Center, 
         VerticalOptions=LayoutOptions.Fill, 
         WidthRequest=350, 
         TextColor=Color.Silver, 
         BackgroundColor=Color.Red, 
         BorderColor=Color.Red, 
        }, 
        forgetPasswordLabel 
       } 
      } 
     }; 

     var forgetPassword_tap = new TapGestureRecognizer(); 
     forgetPassword_tap.Tapped += (s,e) => 
     { 
      // 
      // Do your work here. 
      // 
     }; 
     forgetPasswordLabel.GestureRecognizers.Add(forgetPassword_tap); 
3
MyClickyLabel.GestureRecognizers.Add(
    new TapGestureRecognizer() { 
     Command = new Command(() => { 
      /* Handle the click here */ 
     }) 
    } 
); 
+0

Ci dispiace, ma questo codice non funziona per me, quale potrebbe essere la ragione? 'lblLogin.GestureRecognizers.Add (new TapGestureRecognizer() {Command = new Command (() => {lblLogin_Clicked();})});' 'void async privato lblLogin_Clicked() {Attendi Navigation.PushAsync (new LoginPage()); ' –

+0

Cosa succede se non si esegue lblLogin async? Oppure fai il comando asincrono e attendi su lblLogin? – noelicus

2

Per le persone che preferiscono utilizzare XAML e che amano legarsi comando direttamente al ViewModel, è possibile utilizzare this:

<Label HorizontalOptions="Center" 
     TextColor="Blue" 
     FontSize="20" 
     Text="Forgot Password?"> 
    <Label.GestureRecognizers> 
     <TapGestureRecognizer Command="{Binding ForgotPasswordCommand}" /> 
    </Label.GestureRecognizers> 
</Label>