maui - Command not getting triggered after enabling MauiEnableXamlCBindingWithSourceCompilation for using Compiled Binding - Sta

I'm working on a .NET MAUI app and recently enabled compiled bindings by adding this to my .csproj

I'm working on a .NET MAUI app and recently enabled compiled bindings by adding this to my .csproj file:

<MauiEnableXamlCBindingWithSourceCompilation>true</MauiEnableXamlCBindingWithSourceCompilation>

After doing this, a Command (NavigateToLogDetailsCommand) in my CollectionView is no longer firing when defined like this:

<?xml version="1.0" encoding="utf-8" ?>
<baseClass:MobileContentPageBase
    x:Class="My.UI.LogListView.LogListViewPage"
    xmlns=";
    xmlns:x=";
    xmlns:baseClass="clr-namespace:My.BaseClasses"
    xmlns:baseConv="clr-namespace:My.Core.Converters"
    xmlns:controls="clr-namespace:My.Core.Controls"
    xmlns:core="clr-namespace:My.UI.Core"
    xmlns:helpers="clr-namespace:My.Core.Helpers"
    xmlns:toolkit=";
    xmlns:viewModels="clr-namespace:My.ViewModels"
    xmlns:vm="clr-namespace:My.LogListView"
    x:Name="_this"
    Title="View Log"
    x:DataType="vm:LogListViewPageViewModel"
    Shell.BackgroundColor="{StaticResource Primary}"
    Shell.TitleColor="{StaticResource White}">

    <Shell.BackButtonBehavior>
        <BackButtonBehavior Command="{Binding Path=NavigateBackCommand}"/>
    </Shell.BackButtonBehavior>

    <baseClass:MobileContentPageBase.Resources>
        <baseConv:DateFormatConverter x:Key="dateFormatter"/>
    </baseClass:MobileContentPageBase.Resources>

    <baseClass:MobileContentPageBase.ToolbarItems>
        <controls:ActionBarItem
            x:Name="copyAction"
            Clicked="CopyAction_Clicked"
            IsEnabled="False">
            <controls:ActionBarItem.IconImageSource>
                <FontImageSource
                    FontFamily="{x:Static core:Constants.MaterialDesignIcons}"
                    Glyph="{x:Static helpers:MaterialFontHelper.ContentCopy}"
                    Size="22"
                    Color="{DynamicResource Key=AppPrimaryForegroundColor}"/>
            </controls:ActionBarItem.IconImageSource>
        </controls:ActionBarItem>
    </baseClass:MobileContentPageBase.ToolbarItems>

    <AbsoluteLayout HorizontalOptions="Fill"
                    VerticalOptions="Fill">
        <Grid
            AbsoluteLayout.LayoutBounds="0,0,1,1"
            AbsoluteLayout.LayoutFlags="All"
            RowDefinitions="Auto, *">
            <Label
                Grid.Row="0"
                Margin="10,15"
                Style="{StaticResource CardHeaderLabelStyle}"
                Text="Log Events"/>
            <CollectionView
                Grid.Row="1"
                ItemsSource="{Binding LogList}"
                RemainingItemsThreshold="1"
                RemainingItemsThresholdReachedCommand="{Binding LoadDataCommand}"
                SelectionChanged="CollectionView_SelectionChanged"
                SelectionMode="Multiple">
                <CollectionView.ItemTemplate>
                    <DataTemplate x:DataType="viewModels:LogEventViewModel">
                        <Border Margin="2"
                                Padding="12">
                            <Grid
                                ColumnDefinitions="220, *"
                                RowDefinitions="Auto,*,Auto"
                                RowSpacing="4">
                                <Label
                                    Grid.Row="0"
                                    Grid.Column="0"
                                    Text="{Binding Path=Timestamp, Converter={StaticResource dateFormatter}, ConverterParameter='long'}"/>
                                <Label
                                    Grid.Row="0"
                                    Grid.Column="1"
                                    Text="{Binding Path=Level}"/>
                                <Image
                                    Grid.Row="0"
                                    Grid.Column="2"
                                    Margin="0,0,8,0"
                                    Aspect="AspectFit"
                                    HeightRequest="18"
                                    HorizontalOptions="End"
                                    IsVisible="{Binding Path=IsOnline}">
                                    <Image.Source>
                                        <FontImageSource
                                            FontFamily="MaterialDesignIcons"
                                            Glyph="{x:Static helpers:MaterialFontHelper.CloudSyncOutline}"
                                            Color="{StaticResource Black}"/>
                                    </Image.Source>
                                </Image>

                                <Grid Grid.Row="1"
                                      Grid.ColumnSpan="2"
                                      ColumnDefinitions="*, Auto">
                                    <Label
                                        Grid.Row="0"
                                        Grid.Column="0"
                                        LineBreakMode="TailTruncation"
                                        MaxLines="1"
                                        Text="{Binding Path=Message}"/>
                                    <Button
                                        IsEnabled="True"
                                        Grid.Column="1"
                                        Padding="10,0,0,0"
                                        BackgroundColor="Transparent"
                                        Command="{Binding Path=NavigateToLogDetailsCommand, Source={x:Reference _this}, x:DataType=vm:LogListViewPageViewModel}"
                                        CommandParameter="{Binding .}"
                                        Text="more..."/>
                                </Grid>
                            </Grid>
                        </Border>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>
        </Grid>
    </AbsoluteLayout>
</baseClass:MobileContentPageBase>

However, if I disable compiled bindings, it works, but I get this warning:

XamlC warning XC0025: Binding was not compiled because it has an explicitly set Source property.

How can I fix this while keeping compiled bindings enabled?

I'm working on a .NET MAUI app and recently enabled compiled bindings by adding this to my .csproj file:

<MauiEnableXamlCBindingWithSourceCompilation>true</MauiEnableXamlCBindingWithSourceCompilation>

After doing this, a Command (NavigateToLogDetailsCommand) in my CollectionView is no longer firing when defined like this:

<?xml version="1.0" encoding="utf-8" ?>
<baseClass:MobileContentPageBase
    x:Class="My.UI.LogListView.LogListViewPage"
    xmlns="http://schemas.microsoft/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft/winfx/2009/xaml"
    xmlns:baseClass="clr-namespace:My.BaseClasses"
    xmlns:baseConv="clr-namespace:My.Core.Converters"
    xmlns:controls="clr-namespace:My.Core.Controls"
    xmlns:core="clr-namespace:My.UI.Core"
    xmlns:helpers="clr-namespace:My.Core.Helpers"
    xmlns:toolkit="http://schemas.microsoft/dotnet/2022/maui/toolkit"
    xmlns:viewModels="clr-namespace:My.ViewModels"
    xmlns:vm="clr-namespace:My.LogListView"
    x:Name="_this"
    Title="View Log"
    x:DataType="vm:LogListViewPageViewModel"
    Shell.BackgroundColor="{StaticResource Primary}"
    Shell.TitleColor="{StaticResource White}">

    <Shell.BackButtonBehavior>
        <BackButtonBehavior Command="{Binding Path=NavigateBackCommand}"/>
    </Shell.BackButtonBehavior>

    <baseClass:MobileContentPageBase.Resources>
        <baseConv:DateFormatConverter x:Key="dateFormatter"/>
    </baseClass:MobileContentPageBase.Resources>

    <baseClass:MobileContentPageBase.ToolbarItems>
        <controls:ActionBarItem
            x:Name="copyAction"
            Clicked="CopyAction_Clicked"
            IsEnabled="False">
            <controls:ActionBarItem.IconImageSource>
                <FontImageSource
                    FontFamily="{x:Static core:Constants.MaterialDesignIcons}"
                    Glyph="{x:Static helpers:MaterialFontHelper.ContentCopy}"
                    Size="22"
                    Color="{DynamicResource Key=AppPrimaryForegroundColor}"/>
            </controls:ActionBarItem.IconImageSource>
        </controls:ActionBarItem>
    </baseClass:MobileContentPageBase.ToolbarItems>

    <AbsoluteLayout HorizontalOptions="Fill"
                    VerticalOptions="Fill">
        <Grid
            AbsoluteLayout.LayoutBounds="0,0,1,1"
            AbsoluteLayout.LayoutFlags="All"
            RowDefinitions="Auto, *">
            <Label
                Grid.Row="0"
                Margin="10,15"
                Style="{StaticResource CardHeaderLabelStyle}"
                Text="Log Events"/>
            <CollectionView
                Grid.Row="1"
                ItemsSource="{Binding LogList}"
                RemainingItemsThreshold="1"
                RemainingItemsThresholdReachedCommand="{Binding LoadDataCommand}"
                SelectionChanged="CollectionView_SelectionChanged"
                SelectionMode="Multiple">
                <CollectionView.ItemTemplate>
                    <DataTemplate x:DataType="viewModels:LogEventViewModel">
                        <Border Margin="2"
                                Padding="12">
                            <Grid
                                ColumnDefinitions="220, *"
                                RowDefinitions="Auto,*,Auto"
                                RowSpacing="4">
                                <Label
                                    Grid.Row="0"
                                    Grid.Column="0"
                                    Text="{Binding Path=Timestamp, Converter={StaticResource dateFormatter}, ConverterParameter='long'}"/>
                                <Label
                                    Grid.Row="0"
                                    Grid.Column="1"
                                    Text="{Binding Path=Level}"/>
                                <Image
                                    Grid.Row="0"
                                    Grid.Column="2"
                                    Margin="0,0,8,0"
                                    Aspect="AspectFit"
                                    HeightRequest="18"
                                    HorizontalOptions="End"
                                    IsVisible="{Binding Path=IsOnline}">
                                    <Image.Source>
                                        <FontImageSource
                                            FontFamily="MaterialDesignIcons"
                                            Glyph="{x:Static helpers:MaterialFontHelper.CloudSyncOutline}"
                                            Color="{StaticResource Black}"/>
                                    </Image.Source>
                                </Image>

                                <Grid Grid.Row="1"
                                      Grid.ColumnSpan="2"
                                      ColumnDefinitions="*, Auto">
                                    <Label
                                        Grid.Row="0"
                                        Grid.Column="0"
                                        LineBreakMode="TailTruncation"
                                        MaxLines="1"
                                        Text="{Binding Path=Message}"/>
                                    <Button
                                        IsEnabled="True"
                                        Grid.Column="1"
                                        Padding="10,0,0,0"
                                        BackgroundColor="Transparent"
                                        Command="{Binding Path=NavigateToLogDetailsCommand, Source={x:Reference _this}, x:DataType=vm:LogListViewPageViewModel}"
                                        CommandParameter="{Binding .}"
                                        Text="more..."/>
                                </Grid>
                            </Grid>
                        </Border>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>
        </Grid>
    </AbsoluteLayout>
</baseClass:MobileContentPageBase>

However, if I disable compiled bindings, it works, but I get this warning:

XamlC warning XC0025: Binding was not compiled because it has an explicitly set Source property.

How can I fix this while keeping compiled bindings enabled?

Share Improve this question edited Mar 10 at 10:04 Divyesh asked Mar 10 at 9:56 DivyeshDivyesh 2,41727 silver badges47 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

If we scrutinize the Command, we can see the problem:

Command="{Binding Path=NavigateToLogDetailsCommand,
                  Source={x:Reference _this},
                  x:DataType=vm:LogListViewPageViewModel}"

You're telling it Source={x:Reference _this}. From reading above, we quickly identify that "_this" refers to the MobileContentPageBase which isn't a LogListViewPageViewModel.

If I had to guess, I suspect NavigateToLogDetailsCommand comes from your view model which is set to the BindingContext of your page. I had to make this assumption since no code-behind was supplied to support or deny this.

Anyhow, to correct this, if the above assumption is correct, then, the Command probably needs to change to:

Command="{Binding Path=NavigateToLogDetailsCommand, BindingContext={Binding BindingContext, x:DataType=ContentPage, Source={x:Reference _this}}, x:DataType=vm:LogListViewPageViewModel}"
Command="{Binding Path=BindingContext.NavigateToLogDetailsCommand,
                  Source={x:Reference _this},
                  x:DataType=ContentPage}" />

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744853117a4597246.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信