XamarinCommunityToolkit: [Bug] [MauiCompat] TouchEffect not working

Hi,

I am struggling to get the TouchEffect to work in a brand new Maui project.

Description

I have created a brand new project and installed the following two nuget packages:

Xamarin.CommunityToolkit.MauiCompat 2.0.2-preview1013 CommunityToolkit.Maui 1.2.0

The app compiles fine and everything else appear to work as intended

I have confirmed that other effects such as xct:CornerRadiusEffect works in my repro so I believe I am importing the correct namespaces.

Just the touch effects seem to do nothing.

In my reproduction I followed the tutorial on https://www.youtube.com/watch?v=2QKFsI7zcts

I have added the effects and compatibility renderers in my MauiProgra.cs

public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder.UseMauiApp<App>()
        .UseMauiCompatibility()
        .ConfigureMauiHandlers(handlers =>
        {
            // Register ALL handlers in the Xamarin Community Toolkit assembly
            handlers.AddCompatibilityRenderers(typeof(Xamarin.CommunityToolkit.Effects.TouchEffect).Assembly);
        })
        .ConfigureEffects(effects =>
        {
            effects.AddCompatibilityEffects(typeof(Xamarin.CommunityToolkit.Effects.TouchEffect).Assembly);
        
            // Attempt to just add the touch effect
            // effects.Add(typeof(TouchEffect), typeof(PlatformTouchEffect));
        })
        .ConfigureFonts(fonts =>
        {
            fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
            fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
        });
        
        return builder.Build();
    }
    ```
    
A small snippet from MainPage.xaml:
    
    ```
    <?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:local="clr-namespace:MyTouchEffect"
             xmlns:xct="clr-namespace:Xamarin.CommunityToolkit.Effects;assembly=Xamarin.CommunityToolkit.MauiCompat"
             x:DataType="local:SampleViewModel"
             x:Class="MyTouchEffect.MainPage">

        <StackLayout HeightRequest="100"
                         xct:TouchEffect.PressedBackgroundColor="Red"
                         BackgroundColor="Yellow"
                         xct:CornerRadiusEffect.CornerRadius="100"
                         WidthRequest="100">

                <Label FontSize="Title"
                       TextColor="Black"
                       VerticalOptions="CenterAndExpand"
                       HorizontalOptions="CenterAndExpand"
                       Text="{Binding Count}" />

            </StackLayout>                 
            </ContentPage>

Stack Trace

Link to Reproduction Sample

(https://github.com/jonakirke94/MauiTouchEffectRepro)

Steps to Reproduce

  1. Create a new Maui Project
  2. Add the Xamarin.CommunityToolkit.MauiCompat
  3. Attempt the TouchEffect to work

Expected Behavior

The TouchEffects works as in Xamarin

Actual Behavior

Usings of the TouchEffects do nothing

Basic Information

maui-android 6.0.486/6.0.400

Workaround

I haven’t been able to find a workaround

Reproduction imagery

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 20 (2 by maintainers)

Most upvoted comments

@jonakirke94 It is a lot of files so i just created a repository https://github.com/LennoxP90/XCTTouchEffectMAUI

I ended up pulling in the source code for the Touch Effect manually into my MAUI project fix some of the errors on compile, and it seems to work fine after doing that also you need to make sure you have

.ConfigureEffects( effects =>
      {
#if ANDROID || IOS
        effects.Add( typeof( TouchEffect ), typeof( PlatformTouchEffect ) );
#endif
      } )

in your MauiProgram.cs

Created this repo based on Xamarin CommunityToolkit for Maui https://github.com/aminparsa18/Maui.TouchEffect

@LennoxP90 thank you for sharing your workaround!

@LennoxP90 Awesome, great work!

Will use this workaround until it works as intended in the community toolkit 🎉

@LennoxP90 Looks good. Would you be willing to share the platform implementations in a gist? I find myself struggling with mapping the new namespaces…