2016-03-05 67 views
6

Çapraz platform xamarin uygulamasında çalışıyorum ve "Şifremi unuttum" için köprü etiketi oluşturmak istiyorum. giriş sayfasında. Etiket oluşturmak için aşağıdaki kodu kullandım ancak onclick olayını nasıl oluşturacağımı bilmiyorum.xamarin formlarında etiket üzerinde tıklama olayı dinamik olarak nasıl oluşturulur

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, 

         }, 
        } 
     } 
      }; 

cevap

9

bu deneyin:

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

Örnek: XAML kullanmayı tercih ve ViewModel doğrudan Command bağlamak hoşlananlar insanlar için

 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

Üzgünüz, ama bu kod benim için çalışmıyor, nedeni ne olabilir? lblLogin.GestureRecognizers.Add (yeni TapGestureRecognizer() {Command = yeni Komut (() => {lblLogin_Clicked();})}); ' ' özel async void lblLogin_Clicked() {await Navigasyon.PushAsync (yeni LoginPage());} ' –

+0

Eğer lblLogin async yapmıyorsanız ne olur? Ya da komutunu uyumsuz ve lblLogin üzerinde bekliyor musun? – noelicus

2

kullanabileceğiniz Bu:

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