Why does the Swipe Binding work differently under Debug and Release mode?

dg2k 1,386 Reputation points
2022-11-09T08:48:46.507+00:00

I am using the Swipe gesture with CollectionView, with Command Binding as follows:

<SwipeItem Command="{Binding SwipeRightCommand, Source={RelativeSource AncestorType={x:Type vm:MainPageViewModel}}}" CommandParameter="{Binding .}" />  

The method relayed to by the bound command is as follows:

public async void OnSwipeRight(DataItemGroup groupItem)  
{  
    if (groupItem == null) return;  
    await DataFolderActions(groupItem);  
}  

For the ContentPage XAML, the relevant attributes are defined as:
xmlns:vm="clr-namespace:MySample.ViewModels"
x:DataType="vm:MainPageViewModel"

Platform: .NET MAUI Android; Visual Studio: 17.5.0 Preview 1.0; Framework: 6.0.543.4

IN DEBUG MODE:

Everything works as expected and in the Method above, the Swipe CommandParameter is forwarded as non-null.

IN RELEASE MODE:

I noted that the CommadParameter passed to the Method is now null, hence the Binding doesn't work as expected.

CHANGED BINDING FOR CommandParameter:

To overcome the above issue, I tried to change the Binding for the CommandParameter as follows:

<SwipeItem Command="{Binding SwipeRightCommand, Source={RelativeSource AncestorType={x:Type vm:MainPageViewModel}}}" CommandParameter="{Binding Source={RelativeSource Self}, Path=BindingContext}" />  

Now the above works both for Debug and Release modes. The difference in the Binding is now for CommandParameter as summarised below:

CommandParameter="{Binding .}"  
CommandParameter="{Binding Source={RelativeSource Self}, Path=BindingContext}"   

The second alternative Binding which I came across as a suggested solution for a similar question, but gives no explanation. What I couldn't be sure is therefore how the above two codes can be different under Debug and Release mode.

What is the explanation for code #1 to work in Debug but fail in Release mode?
Put differently, how are the two codes different to produce different operational results?

PS: I am adding the dotnet-maui Tag just in case there are peculiarities arising from .NET MAUI to contribute to this issue For example, I've noticed Swipe gesture issues with CollectionView under WinUI and therefore couldn't rule in or rule out the .NET MAUI effect.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,900 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,278 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 35,806 Reputation points Microsoft Vendor
    2022-11-10T07:19:36.47+00:00

    Hello,

    What is the explanation for code #1 to work in Debug but fail in Release mode?

    The main difference between debug mode and release mode is that the code will be optimized in release mode.

    Therefore, it should be caused by some code being optimized by the compiler in release mode.

    For the point that your program behaves inconsistently in debug and release modes, it is more recommended to file a new issue on GitHub to make our product group aware of this.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.