c# - 相对源绑定(bind) Xamarin

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

我的问题是 viewcell,找不到 OnDelete 命令,因为它属于 IssueModel 类,我试图更改 Listview 的绑定(bind)上下文,但除了上述绑定(bind)之外,这不会改变任何东西.

有什么方法可以更改视单元的绑定(bind)上下文,这样我就不必将命令放入 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" />

最佳答案

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

现在像这样更改您的 ViewCell 绑定(bind):

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

现在,绑定(bind)源已使用其名称设置为页面。路径设置为属性 BindingContext.OnDelete。因此,在此页面的后备 View 模型中应该有一个 OnDelete 属性。

像您在评论中提出的那样澄 list 独的组件。

正则绑定(bind)省略了 Path=。如果未明确提及,{Binding MyProperty} 与“{Binding Path=MyProperty}”的含义相同。 Path 表示需要从 BindingContext 绑定(bind)的值的路径,因此实际上是您要绑定(bind)到的属性。

Source 用于指定Path 的来源。这本身就是另一种约束。在我们的例子中,引用是我们刚刚给页面命名的。这样,ViewCell 的绑定(bind)就知道从 Source 开始,然后搜索 Path 以检索其值。我希望这能让它更清楚一些。

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

关于c# - 相对源绑定(bind) Xamarin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49507092/

相关文章:

ios - Xamarin Forms,当我在应用程序商店(Testflight)上发布我的应用程序时,标签字体大小在不同的 iPhone 6s 设备上不同

xamarin - 目标 'MonoAndroid,Version=v6.0' 项目依赖项

c# - 如何在 C# 中创建自定义对象类型类?

c# - 如何使用 GDI+ 合法地滚动?

c# - 在 C# 中更改网格的 Canvas.ZIndex

wpf - 我们可以在窗口上使用多 xaml 布局吗?

ios - Xamarin iOS 如何检查位置服务是否已关闭或应用级设备位置是否已关闭

C# 正则表达式匹配 15 个字符、单个空格、字母数字

c# - Azure函数: Could not load file or assembly Newtonsoft. Json

c# - 如何在WinRT中实现无缝的页面导航?