最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

xaml - Work-around for warning with Compiled bindings - Stack Overflow

programmeradmin4浏览0评论

I am attempting to improve runtime performance and running into an issue.

I have a button on my ContentPage named MyCommand, this receives an Item (from a DataTemplate) and processes it. I have a few examples that I have tried, the issue is that only one solution works (but doesn't compile without warnings for runtime performance) and the others produce a similar warning, error and simply do not hit MyCommand or do not send the Item in question.

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage ...
             x:DataType="pages:MyContentPage"
             x:Name="MyPage">
             
    <syncfusion:SfListView>
        <syncfusion:SfListView.ItemTemplate>
            <DataTemplate x:DataType="models:MyItem">

                <Grid ...>
                    <Grid ...>
                        <Border ...>
                            <Grid ...>
                                <Grid.GestureRecognizers>

                                    <!--Works, but Compile issue below-->
                                    <!--ISSUE: Binding could be compiled to improve runtime performance if x:DataType is not explicitly null. See  for more information.-->
                                    <!--Has x:DataType of null and allows it to work-->
                                    <TapGestureRecognizer x:DataType="{x:Null}"
                                                          Command="{Binding Source={RelativeSource AncestorType={x:Type ContentPage}}, Path=BindingContext.MyCommand}"
                                                          CommandParameter="{Binding Msg}" />

                                    <!--ISSUE: Click does not work, never hit, above has x:DataType-->
                                    <!--<TapGestureRecognizer Command="{Binding Source={RelativeSource AncestorType={x:Type ContentPage}}, Path=BindingContext.MyCommand}"
                                                          CommandParameter="{Binding .}" />-->

                                    <!--ISSUE: <TapGestureRecognizer Command="{Binding Source={x:Reference MyPace}, Path=BindingContext.MyCommand}"
                                                          CommandParameter="{Binding .}" />-->

                                    <!--ISSUE: {Binding .} no longer works-->
                                    <!--<TapGestureRecognizer x:DataType="pages:MyContentPage"
                                                          Command="{Binding Source={x:Reference MyPage}, Path=MyCommand}"
                                                          CommandParameter="{Binding .}" />-->
                                </Grid.GestureRecognizers>

                                ...
                                
                            </Grid>
                        </Border>

                        ...

                    </Grid>
                </Grid>
            </DataTemplate>
        </syncfusion:SfListView.ItemTemplate>
    </syncfusion:SfListView>
             
    </ContentPage.Content>
</ContentPage>

Wondering what approach I could use to resolve this issue. I don't believe I can put the Command in my Object class, I need it in my ContentPage.

I am attempting to improve runtime performance and running into an issue.

I have a button on my ContentPage named MyCommand, this receives an Item (from a DataTemplate) and processes it. I have a few examples that I have tried, the issue is that only one solution works (but doesn't compile without warnings for runtime performance) and the others produce a similar warning, error and simply do not hit MyCommand or do not send the Item in question.

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage ...
             x:DataType="pages:MyContentPage"
             x:Name="MyPage">
             
    <syncfusion:SfListView>
        <syncfusion:SfListView.ItemTemplate>
            <DataTemplate x:DataType="models:MyItem">

                <Grid ...>
                    <Grid ...>
                        <Border ...>
                            <Grid ...>
                                <Grid.GestureRecognizers>

                                    <!--Works, but Compile issue below-->
                                    <!--ISSUE: Binding could be compiled to improve runtime performance if x:DataType is not explicitly null. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information.-->
                                    <!--Has x:DataType of null and allows it to work-->
                                    <TapGestureRecognizer x:DataType="{x:Null}"
                                                          Command="{Binding Source={RelativeSource AncestorType={x:Type ContentPage}}, Path=BindingContext.MyCommand}"
                                                          CommandParameter="{Binding Msg}" />

                                    <!--ISSUE: Click does not work, never hit, above has x:DataType-->
                                    <!--<TapGestureRecognizer Command="{Binding Source={RelativeSource AncestorType={x:Type ContentPage}}, Path=BindingContext.MyCommand}"
                                                          CommandParameter="{Binding .}" />-->

                                    <!--ISSUE: <TapGestureRecognizer Command="{Binding Source={x:Reference MyPace}, Path=BindingContext.MyCommand}"
                                                          CommandParameter="{Binding .}" />-->

                                    <!--ISSUE: {Binding .} no longer works-->
                                    <!--<TapGestureRecognizer x:DataType="pages:MyContentPage"
                                                          Command="{Binding Source={x:Reference MyPage}, Path=MyCommand}"
                                                          CommandParameter="{Binding .}" />-->
                                </Grid.GestureRecognizers>

                                ...
                                
                            </Grid>
                        </Border>

                        ...

                    </Grid>
                </Grid>
            </DataTemplate>
        </syncfusion:SfListView.ItemTemplate>
    </syncfusion:SfListView>
             
    </ContentPage.Content>
</ContentPage>

Wondering what approach I could use to resolve this issue. I don't believe I can put the Command in my Object class, I need it in my ContentPage.

Share Improve this question asked Jan 20 at 19:40 DerekDerek 6821 gold badge7 silver badges21 bronze badges 1
  • why can't it just inherit DataType from the DataTemplate? – Jason Commented Jan 20 at 19:49
Add a comment  | 

1 Answer 1

Reset to default 0

Looks like the x:DataType can be added to the Command, but not the CommandParameter, so I was able to solve it by having the TapGestureRecognizer as the following:

<Grid.GestureRecognizers>
    <TapGestureRecognizer CommandParameter="{Binding Msg}">
        <TapGestureRecognizer.Command>
            <Binding x:DataType="pages:MyContentPage"
                     Source="{x:Reference MyPage}"
                     Path="MyCommand" />
        </TapGestureRecognizer.Command>
    </TapGestureRecognizer>
</Grid.GestureRecognizers>

The following warning no longer exists after the above changes:

Binding could be compiled to improve runtime performance if x:DataType is not explicitly null. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information.

发布评论

评论列表(0)

  1. 暂无评论