【问题标题】:Binding a command to a parent view model in MAUI在 MAUI 中将命令绑定到父视图模型
【发布时间】:2022-01-09 15:59:05
【问题描述】:

我想将父视图模型引用为命令绑定。 我原以为 MAUI 语法会像 Xamarin 一样工作,但我收到以下错误:

  • '}' 预期
  • '�' 预期

这是我尝试过的语法:

    <ContentPage ... x:Class="ParentPage" x:DataType="ParentViewModel" x:Name="Parent">
        <StackLayout>
            <ListView ItemsSource="{Binding Tabs}">
                <ListView.ItemTemplate>
                    <DataTemplate x:DataType="TabViewModel">
                        <ViewCell>
                            <Button Text="Do it"
                                Command="{Binding Path=SelectTab
                                    RelativeSource={RelativeSource AncestorType={x:Type ParentPage}}}" />
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ContentPage>



Or:
    <ContentPage ... x:Class="ParentPage" x:DataType="ParentViewModel" x:Name="Parent">
        <StackLayout>
            <ListView ItemsSource="{Binding Tabs}">
                <ListView.ItemTemplate>
                    <DataTemplate x:DataType="TabViewModel">
                        <ViewCell>
                            <Button Text="Do it"
                                Command="{Binding Path=SelectTab
                                    Source={x:Reference Parent}}" />
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ContentPage>

这个语法有什么问题?

是否有关于 MAUI 绑定的特定文档?

【问题讨论】:

    标签: xaml data-binding maui


    【解决方案1】:

    在第一个 sn-p 中,您缺少一个逗号和一个超出需要的右大括号:

    <Button Text="Do it"
        Command="{Binding Path=SelectTab, 
                          RelativeSource={RelativeSource AncestorType={x:Type ParentPage}}}"
    />
    

    【讨论】:

    • 这是一个复制粘贴错误,我得到相同的语法错误,括号数正确
    • 您是否在“SelectTab”之后插入了逗号?
    • 确实,这是主要问题。我还得换成Source={RelativeSource AncestorType={x:Type ParentPage}}}
    猜你喜欢
    • 2023-02-14
    • 2023-01-14
    • 2022-01-17
    • 2014-11-08
    • 2012-10-02
    • 1970-01-01
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多