Search code examples
c#.netvisual-studiomaui

Custom control causes a crash on MAUI app on Release only


NB : As I was writing this question and investigating to give more precision, I actually found the solution. I'll leave the question in case it saves someone else the time I spent on this.

I am using in my MAUI project a custom control I created myself, and I ended up in a weird situation : everything works perfectly fine in Debug, but as soon as I switch to Release, the app doesn't launch anymore, and crashes as soon as the loading screen ends.

I ended up finding a parameter binding in my custom control was the cause (commenting it makes the app launch normally), but after isolating my control in a different project, it worked without any problem, whether I'm in Debug or Release configuration.

The custom control as used in my project :

<utils:SwitchableView UseMainView="{Binding MyParameter}">
    <!--Content of the Control. Basically, two views, and we switch which one is displayed according to UseMainView.-->
</utils:SwitchableView>

MyParameter definition in the ViewModel :

public static bool MyParameter 
{
    get
    {
        return true;
    }
}

Solution

  • Okay time to answer my own question. The solution was very simple : just don't make MyParameter static. It seems making the parameter static messes a bit with the compiler on Release configuration. (If anyone wants to comment about the WHY, feel free to do so, I'd be happy to understand the reason behind this behavior.)

    public bool MyParameter 
    {
        get
        {
            return true; // Return any value you need for your parameter here.
        }
    }