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

vptzau2j  于 2023-04-03  发布在  其他
关注(0)|答案(5)|浏览(182)

我正在开发跨平台的xamarin应用程序,我想在登录页面上为“忘记密码?”创建超链接标签。我使用了以下代码来创建标签,但我不知道如何在其上创建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,

                        },
                    } 
        }
            };
8ulbf1ek

8ulbf1ek1#

对于喜欢使用XAML和喜欢将Command直接绑定到ViewModel的人,可以使用以下命令:

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

然后在你的ViewModel中,你只需要将命令分配给你的函数:

public ICommand ForgotPasswordCommand => new Command(OnForgotPassword);

然后定义函数并完成所有工作:

private async void OnForgotPassword()
{ ... }

PS:你需要声明你是using System.Windows.Input;

0h4hbjxa

0h4hbjxa2#

试试这个:

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

fzwojiic3#

如果有几个地方有可点击的标签,那么创建一个从Xamarin.Forms Label继承的控件是有意义的,并且在需要标签的地方不要放置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();
            }
        }
    }
}

在您的XAML文件中,您导入定义控件的名称空间,例如

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

并将其用作普通控件:

<ctrl:ExtendedLabel x:Name="quitButton" Clicked="OnQuit">
rseugnpd

rseugnpd4#

MyClickyLabel.GestureRecognizers.Add(
    new TapGestureRecognizer() { 
        Command = new Command(() => { 
            /* Handle the click here */
        } )
    }
);
ua4mk5z4

ua4mk5z45#

有一个很好的示例应用程序写在DotNet毛伊岛的窗口,检测什么字被点击在标签上。我相信提供了一个很好的解决方案,人们可以从中获得更多的想法。这是一个公共的Github回购:https://github.com/rafaelduluc/DOTNETMAUI-CHATGPT将为您提供“知识”-

相关问题