web-dev-qa-db-de.com

So erstellen Sie ein Klickereignis auf einem Etikett in Xamarin-Formularen dynamisch

Ich arbeite an einer plattformübergreifenden Xamarin-Anwendung und möchte ein Hyperlink-Label für "Passwort vergessen" erstellen. auf der Anmeldeseite ..__ Ich habe folgenden Code zum Erstellen eines Labels verwendet, weiß jedoch nicht, wie ein Onclick-Ereignis darauf erstellt wird.

 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
Dipak

Versuche dies :

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

Probe :

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

Für Personen, die XAML bevorzugen und Command direkt an das ViewModel binden möchten, können Sie Folgendes verwenden:

<Label HorizontalOptions="Center"
       TextColor="Blue"
       FontSize="20"
       Text="Forgot Password?">
    <Label.GestureRecognizers>
        <TapGestureRecognizer Command="{Binding ForgotPasswordCommand}" />
    </Label.GestureRecognizers>
</Label>
10
Mik
MyClickyLabel.GestureRecognizers.Add(
    new TapGestureRecognizer() { 
        Command = new Command(() => { 
            /* Handle the click here */
        } )
    }
);
3
noelicus

Wenn es mehrere Bereiche mit anklickbarem Label gibt, ist es sinnvoll, ein Steuerelement zu erstellen, das von Xamarin.Forms Label erbt, und TapGestureRecognizer nicht überall dort zu platzieren, wo das Label erforderlich ist.

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 Ihre XAML-Datei importieren Sie den Namespace, in dem das Steuerelement definiert ist, z.

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

Und benutze es als gewöhnliche Kontrolle:

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

https://github.com/maxim-saplin/CrossPlatformDiskTest/blob/master/Saplin.CPDT.UICore/Controls/ExtendedLabel.cs

0
Maxim Saplin