xaml - 与 Prism 导航绑定
问题描述
我正在尝试使用 ListView 中按钮的 prism navigateto 扩展方法在 XAML 中绑定字符串路径。
显然 BindingContext 不被识别为与 ListView 中的相同
这是我想要实现的示例代码。
<ListView
x:Name="MainMenu"
CachingStrategy="RetainElement"
HasUnevenRows="True"
ItemsSource="{Binding MenuItems}"
Margin="20,0,0,0"
SeparatorVisibility="Default">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<buttons:SfButton
Style="{StaticResource BaseButtonStyle}"
HeightRequest="70"
TextColor="{StaticResource MenuTextColor}"
Text="{Binding Title}"
HorizontalTextAlignment="Start"
VerticalOptions="CenterAndExpand"
HorizontalOptions="FillAndExpand"
ImageSource="{Binding MenuItemType, Converter={StaticResource MenuItemTypeConverter}}"
ShowIcon="True"
Command="{prism:NavigateTo Name={Binding Item.View}}"
/>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
在这种情况下如何设置绑定有什么想法吗?
问候。
解决方案
在任何类型的 ListView、CollectionView 等中,您要绑定 ItemsSource,然后使用某种 DataTemplate 来显示该集合中的单个项目,该 DataTemplate 中的绑定上下文是集合中的单个项目,而不是提供的 ViewModel您的 Page 和 ListView 的绑定上下文。
从技术上讲,您需要了解几个部分。假设您的模型如下所示:
public class FooModel
{
public string Text { get; set; }
public string NavigationPath { get; set; }
}
假设您有一个 FooModel 集合,例如:
public ObservableCollection<FooModel> FooItems { get; }
在 XAML 中,您可能有类似的内容:
<ListView ItemsSource="{Binding FooItems}">
但是,当您转到 FooItem 的引用属性时,您只会像这样引用它们:
<ListView.DataTemplate>
<DataTemplate>
<ViewCell>
<Button Text="{Binding Text}"
Command="{prism:NavigateTo Name={Binding NavigationPath}" />
</ViewCell>
</DataTemplate>
</ListView.DataTemplate>
现在假设问题不在于您只是Item
错误地添加,让我们看看其他一些可能的问题/解决方案。首先,让我们看一下页面的开头。
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="http://prismlibrary.com"
x:Name="page"
x:Class="HelloWorld.Views.ViewA">
这里要注意的一件大事是我添加了x:Name
属性,以便以后可以引用页面本身。一般来说,在 ListView 之类的上下文中,如果我需要访问FooCommand
我的 ViewModel 中的属性,我可能会更改我的 XAML 标记:
<Button Command="{Binding FooCommand}" />
而是像这样查看页面的 BindingContext:
<Button Command="{Binding BindingContext.FooCommand, Source={x:Reference page}}" />
虽然这通常会在您的 ListView 中帮助您,但它仍然不一定能帮助您解决使用 Prism 导航扩展的问题。为此,您可能需要传入 SourcePage,如下所示:
<Button Command="{prism:NavigateTo 'Foo', SourcePage={x:Reference page}}" />
如果由于某种原因这不起作用,那么您可能没有在导航扩展本身上正确设置 BindingContext。要解决此问题,您需要按如下方式更新命令:
<Button x:Name="cellButton"
Command="{prism:NavigateTo Name={Binding View},
BindingContext={x:Reference cellButton}}" />
请注意,如果您需要引用 SourcePage 或添加 BindingContext 来解决您的问题,请提供重现问题的示例并在 GitHub 上打开问题。
推荐阅读
- javascript - 如何在javascript中比较同一数组中的不同对象
- neural-network - 区分人工神经网络中的学习和记忆
- php - PHP SQL LEFT JOIN 多个值
- vue.js - 使用 ASP.net Core 2.0 应用程序在 IIS 上托管时出现 VueJs Url 重写错误
- git - GitHub 合作者私人档案
- php - laravel 获取当前登录用户的数据
- ios - 可选的通用参数 - 无法推断通用参数“T”
- c# - 如何在 MVC 项目之间共享 React jsx 文件?
- php - 在非重复值上合并数组
- copy - ansible:如何根据组成员身份将文件复制到主机?