简体   繁体   English

如何在 xamarin 表单中动态创建标签上的点击事件

[英]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?"我正在跨平台 xamarin 应用程序上工作,我想为“忘记密码?”创建超链接标签。 on login page.在登录页面上。 I have used following code to create label but I don't know how to create onclick event on it.我使用以下代码创建标签,但我不知道如何在其上创建 onclick 事件。

 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,

                        },
                    } 
        }
            };

For people who prefer to use XAML and who like to bind Command directly to the ViewModel, you can use this:对于喜欢使用 XAML 并且喜欢将 Command 直接绑定到 ViewModel 的人,您可以使用:

<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:然后在您的 ViewModel 中,您只需将命令分配给您的函数:

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; PS:您需要声明您正在using System.Windows.Input;

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

If the're several places with clickable Label, it makes sense to create a control inheriting from Xamarin.Forms Label and DO NOT PUT TapGestureRecognizer everywhere the label is required.如果有多个带有可点击标签的地方,创建一个继承自 Xamarin.Forms 标签的控件是有意义的,并且不要在需要标签的任何地方放置 TapGestureRecognizer。

public class ExtendedLabel : Label
{
    private event EventHandler click;

    public string Name
    {
        get; set;
    }

    public void DoClick()
    {
        click?.Invoke(this, null);
    }

    public event EventHandler Clicked
    {
        add
        {
            lock (this)
            {
                click += value;

                var g = new TapGestureRecognizer();

                g.Tapped += (s, e) => click?.Invoke(s, e);

                GestureRecognizers.Add(g);
            }
        }
        remove
        {
            lock (this)
            {
                click -= value;

                GestureRecognizers.Clear();
            }
        }
    }
}    

In your XAML file you import the namespace where the control is defined, eg在您的 XAML 文件中,您导入定义控件的命名空间,例如

<ContentPage xmlns:ctrl="clr-namespace:UICore.Controls" ...

And use it as ordinary control:并将其用作普通控件:

<ctrl:ExtendedLabel x:Name="quitButton" Clicked="OnQuit">
MyClickyLabel.GestureRecognizers.Add(
    new TapGestureRecognizer() { 
        Command = new Command(() => { 
            /* Handle the click here */
        } )
    }
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM