increase the bindings performance

Eduardo Gomez Romero 1,175 Reputation points
2025-01-04T14:38:08.8266667+00:00

Hello happy new year to everyone

I am trying to increase performance on my xaml

Since I updated to .Net 9, I got this warning

Binding was not compiled because it has an explicitly set Source property and compilation of bindings with Source is not enabled. Consider enabling this optimization by setting the <MauiEnableXamlCBindingWithSourceCompilation>true</MauiEnableXamlCBindingWithSourceCompilation> in your project file and make sure the correct x:DataType is specified for this binding. See https://zcusa.951200.xyz/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information.

<Shell
    x:Class="METROWIND.AppShell"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:controls="clr-namespace:METROWIND.Controls"
    xmlns:local="clr-namespace:METROWIND"
    xmlns:popup="clr-namespace:Syncfusion.Maui.Popup;assembly=Syncfusion.Maui.Popup"
    xmlns:rex="clr-namespace:METROWIND.Resources"
    xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
    xmlns:views="clr-namespace:METROWIND.Views"
    xmlns:vm="clr-namespace:METROWIND.ViewModel"
    x:Name="shelly"
    x:DataType="vm:AppShellViewModel"
    Shell.FlyoutBehavior="{OnIdiom Desktop=Locked,
                                   Phone=Disabled}">


    <Shell.Behaviors>
        <toolkit:EventToCommandBehavior
            Command="{Binding AppearingCommand}"
            CommandParameter="{Binding Source={x:Reference shelly}}"
            EventName="Appearing" />
    </Shell.Behaviors>

    <Shell.FlyoutHeader>
        <Grid>
            <Image
                x:Name="ConfigMenu"
                Margin="10,10,0,15"
                HeightRequest="48"
                HorizontalOptions="Start"
                Source="windmill.png"
                WidthRequest="48">
                <Image.GestureRecognizers>
                    <TapGestureRecognizer Command="{Binding OpenMenuCommand}" />
                </Image.GestureRecognizers>
            </Image>
            <popup:SfPopup
                Padding="5"
                AbsoluteY="5"
                AutoSizeMode="Both"
                IsOpen="{Binding IsMenuPopUpOpen}"
                RelativePosition="AlignToRightOf"
                RelativeView="{x:Reference ConfigMenu}"
                ShowHeader="False"
                ShowOverlayAlways="False">
                <popup:SfPopup.PopupStyle>
                    <popup:PopupStyle CornerRadius="8" />
                </popup:SfPopup.PopupStyle>
                <popup:SfPopup.ContentTemplate>
                    <DataTemplate>
                        <HorizontalStackLayout
                            BackgroundColor="#908686"
                            WidthRequest="180">
                            <Label
                                Margin="10,0,0,0"
                                Text="{x:Static rex:AppResource.CompactMode}"
                                VerticalTextAlignment="Center" />
                            <controls:CustomSwitch
                                Margin="10,0,0,0"
                                IsToggled="{Binding IsCompactMode}"
                                OnColor="#4E2B75">
                                <Switch.Behaviors>
                                    <toolkit:EventToCommandBehavior
                                        Command="{Binding ToggleSwitchCommand}"
                                        CommandParameter="{Binding IsCompactMode}"
                                        EventName="Toggled" />
                                </Switch.Behaviors>
                            </controls:CustomSwitch>
                        </HorizontalStackLayout>
                    </DataTemplate>
                </popup:SfPopup.ContentTemplate>
            </popup:SfPopup>
        </Grid>
    </Shell.FlyoutHeader>


So, I did

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>net9.0-android;net9.0-ios;net9.0-maccatalyst</TargetFrameworks>
    <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net9.0-windows10.0.19041.0</TargetFrameworks>
    <!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
    <!-- <TargetFrameworks>$(TargetFrameworks);net9.0-tizen</TargetFrameworks> -->
    <!-- Note for MacCatalyst:
		The default runtime is maccatalyst-x64, except in Release config, in which case the default is maccatalyst-x64;maccatalyst-arm64.
		When specifying both architectures, use the plural <RuntimeIdentifiers> instead of the singular <RuntimeIdentifier>.
		The Mac App Store will NOT accept apps with ONLY maccatalyst-arm64 indicated;
		either BOTH runtimes must be indicated or ONLY macatalyst-x64. -->
    <!-- For example: <RuntimeIdentifiers>maccatalyst-x64;maccatalyst-arm64</RuntimeIdentifiers> -->
    <OutputType>Exe</OutputType>
	  <RootNamespace>METROWIND</RootNamespace>
    <UseMaui>true</UseMaui>
    <SingleProject>true</SingleProject>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
	<MauiEnableXamlCBindingWithSourceCompilation>true</MauiEnableXamlCBindingWithSourceCompilation>

Now I am getting an error, where _shell

 public partial class AppShellViewModel: ObservableObject, IPinClickHandler
 {
  
     private void TurbineService_NoInternet()
     {
         _noInternetPopUp.Show();
     }
     private void InitializeCommand()
     {
         _commandHandler.SetPinClickedCommand(new Command<TurbinePin>(async (pin) =>
         await PinMarkerClicked(pin)));
     }
     [RelayCommand]
     async Task Appearing(AppShell appShell)
     {
         _shell = appShell;
         try
         {
             if (TurbinePins.Count == 0 && !isInitializing)
             {
                 isInitializing = true;
                 await _turbineService.InitializeAsync();
                 isInitializing = false;
             }
         }
         catch (Exception ex)
         {
             Debug.WriteLine($"Initialization failed: {ex.Message}");
             isInitializing = false;
         }
     }
     private void Connectivity_ConnectivityChanged(object? sender, ConnectivityChangedEventArgs e)
     {
         HandleConnectivityChangeAsync();
     }
     private async void HandleConnectivityChangeAsync()
     {
         if (_connectivity.NetworkAccess == NetworkAccess.Internet)
         {
             _noInternetPopUp.IsOpen = false;
             try
             {
                 if (TurbinePins.Count == 0 && !isInitializing)
                 {
                     isInitializing = true;
                     await _turbineService.InitializeAsync();
                     isInitializing = false;
                 }
             }
             catch (Exception ex)
             {
                 Debug.WriteLine($"Initialization failed: {ex.Message}");
                 isInitializing = false;
             }
         }
         else
         {
             TurbineService_NoInternet();
         }
     }
     [RelayCommand]
     void OpenMenu()
     {
         IsMenuPopUpOpen = true;
     }
     [RelayCommand]
     void ToggleSwitch()
     {
         if (IsCompactMode)
         {
             _shell!.FlyoutWidth = 65;
         }
         else
         {
             _shell!.FlyoutWidth = 300;
         }
         IsMenuPopUpOpen = false;
     }
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,805 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 46,821 Reputation points Microsoft Vendor
    2025-01-06T01:27:58.45+00:00

    Hello,

    This is a Maui Xaml file compilation warning numbered XC0025. The official documentation has provided specific solutions on how to fix this problem.

    Please refer to XAML compiled bindings warnings for more details.

    Binding was not compiled because it has an explicitly set Source property and compilation of bindings with Source is not enabled. Consider enabling this optimization by setting the <MauiEnableXamlCBindingWithSourceCompilation>true</MauiEnableXamlCBindingWithSourceCompilation> in your project file and make sure the correct x:DataType is specified for this binding. To fix this warning, enable the $(MauiEnableXamlCBindingWithSourceCompilation) build property in your project file, and annotate all your bindings with the appropriate x:DataType.

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.