简体   繁体   English

Xamarin.CommunityToolkit.MauiCompat TouchEffect 未应用于 CollectionView 的第一项

[英]Xamarin.CommunityToolkit.MauiCompat TouchEffect is not applied to the first item of the CollectionView

I am trying to port a Xamarin app to a Maui app.我正在尝试将 Xamarin 应用程序移植到 Maui 应用程序。 TouchEffect has not yet been ported from Xamarin Community Toolkit to Maui Community Toolkit, so I added Xamarin.CommunityToolkit.MauiCompat 2.0.2-preview1013 . TouchEffect 还没有从 Xamarin Community Toolkit 移植到 Maui Community Toolkit,所以我添加了Xamarin.CommunityToolkit.MauiCompat 2.0.2-preview1013

The following code is a minimal example of the problem I am facing, not the full app.以下代码是我面临的问题的一个最小示例,而不是完整的应用程序。 You can find the code on Github .您可以在Github上找到代码。 The issue happened on both Android and Windows.问题发生在 Android 和 Windows 上。

My issue is that the first item in the CollectionView seems to ignore the TouchEffect .我的问题是CollectionView中的第一项似乎忽略了TouchEffect In the screenshot below, both Product 2 and Product 3 behave as expected ( Product 3 is being pressed).在下面的屏幕截图中,产品 2产品 3的行为都符合预期(按下产品 3 )。 On the other hand, Product 1 does not have a blue background and does not show a red background when pressed.另一方面,产品 1没有蓝色背景,按下时也不会显示红色背景。

Android: Android: 安卓

Windows: Windows: 视窗

Please find the relevant code below.请在下面找到相关代码。

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
             xmlns:viewmodels="clr-namespace:MauiAppTouchEffect.ViewModels;assembly=MauiAppTouchEffect"
             xmlns:models="clr-namespace:MauiAppTouchEffect.Models;assembly=MauiAppTouchEffect"
             x:Class="MauiAppTouchEffect.Views.ProductsPage"
             x:DataType="viewmodels:ProductsViewModel">
    <CollectionView ItemsSource="{Binding Products}">
        <CollectionView.ItemTemplate>
            <DataTemplate x:DataType="models:Product">
                <Label Text="{Binding Description}"
                       FontSize="Large"
                       xct:TouchEffect.NormalBackgroundColor="Blue"
                       xct:TouchEffect.PressedBackgroundColor="Red" />
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
</ContentPage>
public class ProductsViewModel
{
    public ObservableCollection<Product> Products { get; } = new();

    public ProductsViewModel()
    {
        Products = Enumerable
            .Range(1, 3)
            .Select(x => new Product
            {
                Id = x,
                Description = $"Product {x}"
            })
            .ToObservableCollection();
    }
}
public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();

        builder
            .UseMauiApp<App>()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
            })
            .ConfigureMauiHandlers(options => options.AddCompatibilityRenderers(typeof(TouchEffect).Assembly))
            .ConfigureEffects(options =>
            {
                options.AddCompatibilityEffects(typeof(TouchEffect).Assembly);
                
#if ANDROID
                // https://github.com/xamarin/XamarinCommunityToolkit/issues/1905#issuecomment-1254311606
                options.Add(typeof(TouchEffect), typeof(Xamarin.CommunityToolkit.Android.Effects.PlatformTouchEffect));
#endif
            })
            .UseMauiCommunityToolkit()
            .UseMauiCompatibility();

        builder.Services.AddTransient(typeof(ProductsPage));
        builder.Services.AddTransient(typeof(ProductsViewModel));

#if DEBUG
        builder.Logging.AddDebug();
#endif

        return builder.Build();
    }
}

This reads to me like a bug in the Xamarin.CommunityToolkit.MauiCompat library.这对我来说就像Xamarin.CommunityToolkit.MauiCompat库中的错误。 I encourage you to open an issue with them to investigate further.我鼓励您向他们提出问题以进一步调查。

I update the code and posted to github .我更新代码并发布到github (Included image in repo) (回购中包含图像)

For some reason, adding the same attributes to the CollectionView and to the individual Label element applied the effects to all of the items.出于某种原因,将相同的属性添加到CollectionView单个Label元素会将效果应用于所有项目。

    <CollectionView ItemsSource="{Binding Products}"
                       xct:TouchEffect.NormalBackgroundColor="Green"
                       xct:TouchEffect.PressedBackgroundColor="Red"
    >
        <CollectionView.ItemTemplate>
            <DataTemplate x:DataType="models:Product">
                <Label Text="{Binding Description}"
                       FontSize="Large"
                       TextDecorations="Underline" 
                       xct:TouchEffect.NormalBackgroundColor="Green"
                       xct:TouchEffect.PressedBackgroundColor="Red"
                       />
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Xamarin 社区工具包 TouchEffect.Command 在 CollectionView 中不起作用 - Xamarin community toolkit TouchEffect.Command not working in CollectionView Xamarin - CollectionView 第一项和最后一项的额外空间 - Xamarin - CollectionView extra space for first and last item 当我尝试在 Xamarin Forms - Only the first item in a list is rendered when I try to output a collectionview inside a collectionview in Xamarin Forms Xamarin CollectionView 与 HorizontalItemSpacing 相等的项目宽度 - Xamarin CollectionView equal item width with HorizontalItemSpacing 获取 Tapped collectionview 项的索引 xamarin - Getting Index of Tapped collectionview item xamarin 如何从每个 CollectionView 项目中获取 Xamarin? - How to get Xamarin form each CollectionView item? 我可以绑定到CollectionView的第一项吗? - Can I bind to the first item of a CollectionView? 根据是否选择了 Xamarin CollectionView 项来确定 ReactiveUI 中的 CanExecute - Determining CanExecute in ReactiveUI based upon whether a Xamarin CollectionView item is selected 如何使用 Xamarin Forms 中的 SwipeView 从 CollectionView 中删除项目? - How to delete item from CollectionView with SwipeView in Xamarin Forms? Xamarin - 从 CollectionView 中删除项目,命令不会被调用 - Xamarin - Delete item from CollectionView, Command does not get called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM