如何在 xamarin 表单中动态创建标签上的点击事件
Posted
技术标签:
【中文标题】如何在 xamarin 表单中动态创建标签上的点击事件【英文标题】:How to create click event on label in xamarin forms dynamically 【发布时间】:2016-06-19 01:46:54 【问题描述】:我正在开发跨平台 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,
,
;
【问题讨论】:
我在***.com/questions/67686485/… 遇到了类似但不完全相同的问题。有什么建议吗? 【参考方案1】:对于喜欢使用 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;
【讨论】:
这是最简单的方法,纯 XAML,正是我所需要的。谢谢! 我在***.com/questions/67686485/… 遇到了类似但不完全相同的问题。我想获取该事件,以便查看单击时是否按住 CTRL 按钮。我一直在使用 Interface builder,但我可以在 ViewController.h 代码中看到 IAction 事件。 很好的解决方案。如果您想在 xaml.cs 中管理事件而不是“ViewModel”,则可以使用“Tapped”属性。埃斯。试试这个:
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】:如果有多个带有可点击标签的位置,则创建一个从 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();
在您的 XAML 文件中,您导入定义控件的命名空间,例如
<ContentPage xmlns:ctrl="clr-namespace:UICore.Controls" ...
并将其用作普通控件:
<ctrl:ExtendedLabel x:Name="quitButton" Clicked="OnQuit">
【讨论】:
click?.Invoke(this, null);
会更安全
修复了帖子。谢谢!
我不知道为什么标签类型本身没有 on_click 事件处理程序的实现。我计划在我的应用程序中使用 on_click 事件有多个标签,所以这很棒!【参考方案4】:
MyClickyLabel.GestureRecognizers.Add(
new TapGestureRecognizer()
Command = new Command(() =>
/* Handle the click here */
)
);
【讨论】:
抱歉,这段代码对我不起作用,可能是什么原因?lblLogin.GestureRecognizers.Add(new TapGestureRecognizer()Command = new Command(() => lblLogin_Clicked();));
private async void lblLogin_Clicked()await Navigation.PushAsync(new LoginPage());
如果你不让 lblLogin 异步会发生什么?或者让你的命令异步并在 lblLogin 上等待?以上是关于如何在 xamarin 表单中动态创建标签上的点击事件的主要内容,如果未能解决你的问题,请参考以下文章
使用 Prism Xamarin 表单创建动态 TabbedPage