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

查看:89
本文介绍了相对源绑定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" />

推荐答案

freshMvvm:FreshBaseContentPage添加x:Name属性,例如: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天全站免登陆