相对源绑定 Xamarin [英] Relative Source Binding Xamarin

查看:20
本文介绍了相对源绑定 Xamarin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My problem is with the viewcell, the OnDelete command is not found due to it being of the IssueModel class, I've attempted to change the binding-context of the Listview, but that doesn't change anything except the above binding.

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

Is there any way to change the binding context of the viewcell so I don't have to put the command into to 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

I've attempted one of the answers but that didn't work. This just gets an error message: expected type is object, but type is 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".

Add a x:Name attribute to your freshMvvm:FreshBaseContentPage, like i.e.: x:Name="MyAwesomePage".

现在像这样改变你的 ViewCell 绑定:

Now change your ViewCell binding like this:

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

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

Now the binding source is set to the page by using its name. And the path is set to the property BindingContext.OnDelete. So, in your backing view model for this page there should be a OnDelete property.

像您在评论中所问的那样澄清单独的组件.

To clarify the separate components like you've asked in the comments.

Path= 在常规绑定中被省略.如果没有明确提及,{Binding MyProperty} 的含义与{Binding Path=MyProperty}"相同.Path 表示需要从 BindingContext 绑定的值的路径,因此有效地绑定到的属性.

The Path= is omitted with a regular binding. When not explicitly mentioned, {Binding MyProperty} means the same as '{Binding Path=MyProperty}'. Path means the path to the value that needs to be bound from the BindingContext, so effectively the property that you are binding to.

Source 用于指定Path 的来源.这本身就是另一种绑定.在我们的例子中,引用我们刚刚给页面的名称.这样,ViewCell 的绑定就知道从 Source 开始,然后搜索 Path 以检索其值.我希望这能让你明白一点.

The Source is used to specify what is the source of the Path. Which is in itself another binding. In our case to a reference which is known by the name we just gave the page. This way, the binding of the ViewCell knows to start at the Source and then search for the Path to retrieve its value. I hope this makes it a bit clear.

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

You can reference anything here if you want to, as long as you have access to the instance of the class here. I would, however, recommend to keep it to the BindingContext which is effectively the view model (note: BindingContext is the actual property of your page that contains the view model). Else you will lose overview very quickly.

这篇关于相对源绑定 Xamarin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆