如何创建可点击的自定义控件?

huangapple go评论68阅读模式
英文:

How do I create a clickable custom control?

问题

我需要使蓝色框架可点击,这样每当用户点击它时,它应该打开一个弹出窗口。我猜想在自定义控件上创建一个Clicked属性,但我不知道从哪里开始。

英文:

My custom control:

<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
			 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
			 x:Class="OneCampus.Components.TimeEntryCard"
			 x:Name="TimeCardEntryView">

	<VerticalStackLayout>
		<Frame BackgroundColor="Blue" 
			   HorizontalOptions="Fill">
			<Grid>
				<Grid.RowDefinitions>
					<RowDefinition Height="*" />
				</Grid.RowDefinitions>
				<Grid.ColumnDefinitions>
					<ColumnDefinition Width="*" />
					<ColumnDefinition Width="*" />
					<ColumnDefinition Width="*" />
				</Grid.ColumnDefinitions>
				<Label Text="{Binding Source={x:Reference TimeCardEntryView}, Path=Day}" 
					   HorizontalOptions="Start" VerticalOptions="Center" Grid.Row="0" Grid.Column="0" />
				<VerticalStackLayout VerticalOptions="Center" Grid.Row="0" Grid.Column="1">
					<Label Text="InTime" HorizontalOptions="Center" />
					<Label Text="OutTime" HorizontalOptions="Center" />
				</VerticalStackLayout>
				<Label Text="TotalTime" HorizontalOptions="End" VerticalOptions="Center" Grid.Row="0" Grid.Column="2" />
			</Grid>
		</Frame>
	</VerticalStackLayout>
	
</ContentView>
namespace OneCampus.Components;

public partial class TimeEntryCard : ContentView
{
	// does this need to be "public static??"
	public static readonly BindableProperty DayProperty = BindableProperty.Create(nameof(Day), typeof(string), typeof(TimeEntryCard), string.Empty);

	public string Day
	{
		get => (string)GetValue(DayProperty);
		set => SetValue(DayProperty, value);
	}

	public TimeEntryCard()
	{
		InitializeComponent();
	}
}

Output:
如何创建可点击的自定义控件?


What I need:

I need the blue frame to be clickable so that whenever the user clicks on it, it should open a popup. How do I go about it? I am guessing creating a Clicked property on the custom control, but I don't know where to begin.

答案1

得分: 2

使用手势识别器

    <Frame.GestureRecognizers>
        <TapGestureRecognizer
                Tapped="OnTapGestureRecognizerTapped"
                NumberOfTapsRequired="1" />
    </Frame.GestureRecognizers>

然后在您的代码后台

    void OnTapGestureRecognizerTapped(object sender, EventArgs args)
    {
    }

您还可以使用 `Command` 而不是 `Tapped`,以在您的 VM 中指定一个命令,而不是在代码后台中使用事件处理程序

  [1]: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/gestures/tap
英文:

use a Gesture Recognizer

&lt;Frame.GestureRecognizers&gt;
    &lt;TapGestureRecognizer
            Tapped=&quot;OnTapGestureRecognizerTapped&quot;
            NumberOfTapsRequired=&quot;1&quot; /&gt;
&lt;/Frame.GestureRecognizers&gt;

then in your code behind

void OnTapGestureRecognizerTapped(object sender, EventArgs args)
{
}

you can also use Command instead of Tapped to specify a command in your VM instead of an event handler in your code behind

huangapple
  • 本文由 发表于 2023年3月3日 22:43:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定