Search code examples
c#xamarin.formsuwpcommandwindows-community-toolkit

Xamarin.CommunityToolKit TouchEffect.Command not working in UWP


I can't succeed to make the xct:TouchEffect.Command working on UWP while:

  • it is working for android in the same project ...
  • there is no build error or warning

I made a very small project to test it:

  1. Start a new Mobile APP project in VS2019

  2. Add the Xamarin.CommunityToolKit

  3. Add two properties in the Mainpage.xaml.cs (ok, it could be better implemented but it works) :

     private int count = 10;
     public  int Count
     {
         get => count;
         set
         {
             count = value;
             OnPropertyChanged(nameof(Count));
         }
     }
    
     public ICommand PressedCommand => new Command ( () => Count++ );
    
  4. Update the MainPage.xaml like this:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
             x:Class="CommunityToolKit.MainPage"
             x:Name="this">

    <StackLayout>

        <Frame BackgroundColor="#2196F3" Padding="24" CornerRadius="0">
            <Label Text="Welcome to Xamarin.Forms!" HorizontalTextAlignment="Center" TextColor="White" FontSize="36"/>
        </Frame>
        <StackLayout BackgroundColor="IndianRed"
                     xct:TouchEffect.Command="{Binding Path=PressedCommand, Source={x:Reference this}}">
            <Label Text="Start developing now" FontSize="Title" Padding="30,10,30,10"/>
            <Label Text="Make changes to your XAML file and save" FontSize="16" Padding="30,0,30,0"/>
        </StackLayout>

        <Label Text="{Binding Path=Count, Source={x:Reference this}}" Padding="30" HorizontalOptions="Center"/>
        <Button Text="Click Me" Command="{Binding Path=PressedCommand, Source={x:Reference this}}"/>
    </StackLayout>

</ContentPage>

The binding for the command of the button is working on both UWP and Android projects. But the binding of the touchEffect applied on the StackLayout is not working on the UWP project (When I click on the IndianRed Background or wherever in the StackLayout).

What did I miss ?!? Any idea ?

EDIT:

I mention that I'm aware of this post: https://github.com/xamarin/XamarinCommunityToolkit/issues/1456

So my options are curently the folowing: enter image description here Version of Xamarin 5.0.0.2012


Solution

  • Ok, I get the solution from AndreiMisiukevich on the Xamarin.CommunityToolKit project here

    In order to let the ToolKit running in UWP release mode (it already works in debug mode by standard), I had to add those lines in file App.xaml.cs of the UWP project:

    var assembliesToInclude = new List<Assembly>() { typeof(Xamarin.CommunityToolkit.Effects.TouchEffect).GetTypeInfo().Assembly };
    Xamarin.Forms.Forms.Init(e, assembliesToInclude);
    

    It works with release 1.2.0 of the CommunityToolKit and Xamarin 5.0.0.2083.

    In fact, as I use Rg.Plugins.Popup, I transform those lines like this:

    var assembliesToInclude = new List<Assembly>() { typeof(Xamarin.CommunityToolkit.Effects.TouchEffect).GetTypeInfo().Assembly };
    assembliesToInclude.AddRange(Rg.Plugins.Popup.Popup.GetExtraAssemblies());
    Xamarin.Forms.Forms.Init(e, assembliesToInclude);
    

    Thanks everybody for the suggestions and AndreiMisiukevich for the solution.