我的问题是视单元,由于它属于 IssueModel 类,所以找不到 OnDelete 命令,我尝试更改 Listview 的绑定上下文,但这不会改变除上述绑定之外的任何内容。
有什么方法可以更改视单元的绑定上下文,这样我就不必将命令放入 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>
Edit:
我尝试过其中一个答案,但没有成功。这只会收到一条错误消息:预期类型是对象,但类型是 IssueListPageModel
xmlns:pageModels="clr-namespace:ASFT.PageModels;assembly=ASFT"
<MenuItem Command="{Binding Path=BindingContext.OnDelete, Source={pageModels:IssueListPageModel}}" Text="Delete" IsDestructive="True" />
Add a 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
财产。
为了澄清单独的组件,就像您在评论中所问的那样。
The Path=
在常规绑定中被省略。当没有明确提及时,{Binding MyProperty}
与“{Binding Path=MyProperty}”含义相同。Path
表示需要绑定的值的路径BindingContext
,因此实际上是您要绑定的属性。
The Source
用于指定的来源是什么Path
。这本身就是另一种约束。在我们的例子中,引用是通过我们刚刚给出的页面名称得知的。这样,就可以绑定ViewCell
知道从Source
然后搜索Path
来检索它的值。我希望这能让它变得清楚一些。
如果您愿意,您可以在此处引用任何内容,只要您有权访问此处类的实例即可。不过,我建议将其保留在BindingContext
这实际上是视图模型(注意:BindingContext
是包含视图模型的页面的实际属性)。否则你很快就会失去概览。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)