英文:
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();
}
}
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
<Frame.GestureRecognizers>
<TapGestureRecognizer
Tapped="OnTapGestureRecognizerTapped"
NumberOfTapsRequired="1" />
</Frame.GestureRecognizers>
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论