2016-03-05 37 views
6

我正在开发跨平台的xamarin应用程序,并且我想为“忘记密码?”创建超链接标签。在登录页面。 我已经使用下面的代码来创建标签,但我不知道如何创建onclick事件。如何在xamarin表单中的标签上创建点击事件动态

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, 

         }, 
        } 
     } 
      }; 

回答

9

试试这个:

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

样品:

 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

对不起,但这段代码不适合我,原因是什么? ();}};' '私人异步无效lblLogin_Clicked(){等待Navigation.PushAsync(新的LoginPage()){ );}' –

+0

如果你不使lblLogin异步,会发生什么?或者让你的命令异步并等待在lblLogin上? – noelicus

2

对于谁喜欢使用XAML和谁喜欢直接绑定命令到视图模型的人,你可以使用这个:

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