如何将此 DataTemplate 移动到单独的 XAML 文件并使命令在 .NET MAUI 上运行?

问题描述 投票:0回答:1
xaml xamarin.forms mobile maui syncfusion
1个回答
0
投票

我们建议您使用 Tapped 事件来实现您的要求,而不是示例中的 TapGestureRecognizer。请参考以下步骤‘如何在SfScheduler中使用Tapped事件进行命令’。

Step1:安装Community.Toolkit.Maui,在MauiProgram.cs中添加UseMauiCommunityToolkit

public static MauiApp CreateMauiApp()
{
    var builder = MauiApp.CreateBuilder();
    builder.ConfigureSyncfusionCore();
    builder.UseMauiApp<App>().UseMauiCommunityToolkit();
    builder
        .UseMauiApp<App>()
        .ConfigureFonts(fonts =>
        {
            fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
            fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
        });

    return builder.Build();
}

第二步:在viewmodel中添加命令

public ICommand AppointmentTappedCommand { get; set; }

public SchedulerViewModel()
{
    this.GenerateAppointments();
    this.DisplayDate = DateTime.Now.Date.AddHours(8).AddMinutes(50);
    AppointmentTappedCommand = new Command<SchedulerTappedEventArgs>(async (appointment) => await OnAppointmentTappedCommand(appointment));
}

private async Task OnAppointmentTappedCommand(SchedulerTappedEventArgs obj)
{
    if (obj != null && obj.Element == SchedulerElement.Appointment && obj.Appointments[0] is SchedulerAppointment appointment)
    {
        await Shell.Current.DisplayAlert("Appointment", $"{appointment.Id} - {appointment.Subject} - {appointment.StartTime} - {appointment.EndTime}", "OK");
    }
}

第 3 步:在 MainPage.xaml 中

<scheduler:SfScheduler x:Name="Scheduler" 
                View="Week"
                DisplayDate="{Binding DisplayDate}"
                AppointmentsSource="{Binding Events}"
                AllowedViews="Day,Week,WorkWeek,Month,TimelineDay,TimelineWeek,TimelineWorkWeek,TimelineMonth">
    <scheduler:SfScheduler.DaysView>
        <scheduler:SchedulerDaysView>
        <scheduler:SchedulerDaysView.AppointmentTemplate>
            <DataTemplate>
                    <local:AppointmentTemplate/>
            </DataTemplate>
        </scheduler:SchedulerDaysView.AppointmentTemplate>
    </scheduler:SchedulerDaysView>
</scheduler:SfScheduler.DaysView>
    <scheduler:SfScheduler.Behaviors>
        <toolkit:EventToCommandBehavior
    EventName="Tapped"
    Command="{Binding AppointmentTappedCommand}" />
    </scheduler:SfScheduler.Behaviors>
</scheduler:SfScheduler>

如果您有任何疑虑,请告诉我们。

© www.soinside.com 2019 - 2024. All rights reserved.