【问题标题】:Relative Source Binding Xamarin相对源绑定 Xamarin
【发布时间】:2018-09-05 12:29:37
【问题描述】:

我的问题出在 viewcell 上,由于它属于 IssueModel 类,因此找不到 OnDelete 命令,我尝试更改 Listview 的绑定上下文,但除了上述绑定之外没有任何改变.

有什么方法可以改变 viewcell 的绑定上下文,这样我就不必将命令放入 IssueModel 中了吗?

freshMvvm:FreshBaseContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:converters="clr-namespace:ASFT.Converters;assembly=ASFT"
             xmlns:freshMvvm="clr-namespace:FreshMvvm;assembly=FreshMvvm"
             xmlns:helperMethods="clr-namespace:ASFT.HelperMethods;assembly=ASFT"
             x:Class="ASFT.Pages.IssueListPage">
    <ContentPage.Resources>
        <ResourceDictionary>
            <converters:SelectedItemEventArgsToSelectedItemConverter x:Key="SelectedItemConverter" />
            <converters:DateTextConverter x:Key="DateToTextConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>
<ListView ItemsSource="{Binding Issues}" SeparatorColor="#444444" RowHeight="90" IsPullToRefreshEnabled="True" IsRefreshing="{Binding IsBusy}" RefreshCommand="{Binding PullRefreshCommand}" >
        <ListView.Behaviors>
        <helperMethods:EventToCommandBehavior EventName="ItemSelected" 
                                          Command="{Binding OnSelectedIssueCommand}" 
                                          Converter="{StaticResource SelectedItemConverter}" />
        </ListView.Behaviors>
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell >
                    <ViewCell.ContextActions>
                        <MenuItem  Command="{Binding OnDelete}"  Text="Delete" IsDestructive="True" />
                    </ViewCell.ContextActions>

                    <ViewCell.View>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"></RowDefinition>
                                <RowDefinition Height="Auto"></RowDefinition>
                                <RowDefinition Height="*"></RowDefinition>
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="70"></ColumnDefinition>
                                <ColumnDefinition Width="*"></ColumnDefinition>
                                <ColumnDefinition Width="50"></ColumnDefinition>
                            </Grid.ColumnDefinitions>

                            <Image Grid.Row="0" Grid.RowSpan="3" Grid.Column="0" Source="{Binding SeverityImagePath}" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="70"/>
                            <Image Grid.Row="0" Grid.RowSpan="3" Grid.Column="2" Source="{Binding StatusImagePath}" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="60"/>

                            <Label Grid.Row="0" Grid.Column="1" Text="{Binding Title}" LineBreakMode="TailTruncation" YAlign="Center" VerticalOptions="Start" Font="Bold, Medium"/>
                            <Label Grid.Row="1" Grid.Column="1" Text="{Binding Created, Converter={StaticResource DateToTextConverter}}" YAlign="Center" VerticalOptions="Start" Font="Medium"/>
                            <Label Grid.Row="2" Grid.Column="1" Text="{Binding Description}" LineBreakMode="WordWrap" YAlign="Start" VerticalOptions="Start" Font="Small"/>
                        </Grid>

                    </ViewCell.View>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

</freshMvvm:FreshBaseContentPage>

编辑:

我尝试了其中一个答案,但没有奏效。这只是收到一条错误消息:预期类型是对象,但类型是 IssueListPageModel

     xmlns:pageModels="clr-namespace:ASFT.PageModels;assembly=ASFT"



 <MenuItem Command="{Binding Path=BindingContext.OnDelete, Source={pageModels:IssueListPageModel}}" Text="Delete" IsDestructive="True" />

【问题讨论】:

    标签: c# xaml xamarin data-binding xamarin.forms


    【解决方案1】:

    x:Name 属性添加到您的freshMvvm:FreshBaseContentPage,例如:x:Name="MyAwesomePage"

    现在更改您的 ViewCell 绑定,如下所示:

    <MenuItem Command="{Binding Path=BindingContext.OnDelete, Source={x:Reference Name=MyAwesomePage}}" Text="Delete" IsDestructive="True" />
    

    现在绑定源通过使用其名称设置到页面。并且路径设置为属性BindingContext.OnDelete。因此,在此页面的后备视图模型中应该有一个 OnDelete 属性。

    像您在 cmets 中询问的那样澄清单独的组件。

    Path= 在常规绑定中被省略。如果没有明确提及,{Binding MyProperty} 的含义与“{Binding Path=MyProperty}”相同。 Path 表示需要从BindingContext 绑定的值的路径,因此实际上是您要绑定的属性。

    Source 用于指定Path 的来源。这本身就是另一个约束。在我们的例子中,引用以我们刚刚给出的页面的名称而闻名。这样,ViewCell 的绑定就知道从Source 开始,然后搜索Path 以检索其值。我希望这能说明一点。

    如果您愿意,您可以在此处引用任何内容,只要您可以访问此处的类实例。但是,我建议将其保留为实际上是视图模型的 BindingContext(注意:BindingContext 是包含视图模型的页面的实际属性)。否则您将很快失去概览。

    【讨论】:

    • 嗨杰拉德!我以前在 xamarin 网站上看到过这个,但他们没有解释各个组件的作用。 Binding Path, Source 有什么作用?我只是想为未来学习。
    • 我如何使用它来处理 ViewModel/PageModel?我不能在普通的 c# 类上做 x:reference?
    • 为您更新答案!
    • 仍然不确定如何在我自己的编辑中使用我自己的示例,因为找不到页面模型并且您不能 x:name 常规类?
    • 你会如何处理不是页面的东西?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-27
    • 2021-08-27
    • 2016-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多