Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create click event on label in xamarin forms dynamically

I am working on cross platform xamarin application and I want to create hyperlink label for "Forgot password?" on login page. I have used following code to create label but I don't know how to create onclick event on it.

 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,

                        },
                    } 
        }
            };
like image 817
Dipak Avatar asked Mar 05 '16 06:03

Dipak


2 Answers

For people who prefer to use XAML and who like to bind Command directly to the ViewModel, you can use this:

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

And then in your ViewModel, you'll just assign the command to your function:

public ICommand ForgotPasswordCommand => new Command(OnForgotPassword);

And then define the function with all the work get done:

private async void OnForgotPassword()
{ ... }

PS: You will need to declare that you are using System.Windows.Input;

like image 159
Mik Avatar answered Nov 12 '22 17:11

Mik


Try this :

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

Sample :

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);
like image 40
Yksh Avatar answered Nov 12 '22 16:11

Yksh