XAML 在 ListView 中使用的单独 ContentView 文件中绑定智能感知
XAML binding intellisense in a separate ContentView file used in a ListView
我有一个带有此项目模板的 ListView:
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<views:ProjectListEntry />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
ProjectListEntry
相当复杂并且在另一个 ListView 中使用,所以我将它放在自己的文件中。我是这样设置的:
<ContentView
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
...
BindingContext="{x:Static vms:DesignTimeData.ProjectListEntryVm}">
如您所见,为了绑定 Intellisense(使用 ReSharper),我尝试将 BindingContext
设置为 DesignTimeData class 上的静态成员。这适用于我的页面(我在运行时替换 BindingContext),但对于 ListView 中使用的 ContentView,ContentView 的 BindingContext 似乎是继承的(我猜是从 ViewCell)。这意味着我的 ContentView 上的显式 BindingContext 实际上将覆盖 ListView 在 ViewCell 上设置的 BindingContext,并且我的所有列表元素都将在运行时反映静态设计时数据。但是,如果我删除 BindingContext,我将无法获得在 ContentView 文件中绑定到的成员的智能感知。
有没有一种简单的方法可以像这样为 ContentView 中的绑定获取 Intellisense?
(如前所述,我无法在 ListView 定义中内联 ContentView,因为 ContentView 相当复杂并且在多个列表中使用。我也不能使用某种 VM 定位器,因为虽然我使用绑定,我没有使用 "full" MVVM - 我使用的是类似 Redux 的架构。而且我猜 VM 定位器无论如何都不适用于这种情况,原因与上述不起作用相同.)
(从 Xamarin Forums 交叉发布,我没有收到任何回复。)
This solution 似乎工作正常。简而言之:添加
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
并使用 d:DataContext
而不是 BindingContext
。从上面的例子来看,它应该是这样的:
<ContentView
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
...
BindingContext="{x:Static vms:DesignTimeData.ProjectListEntryVm}">
这会导致来自 ReSharper 的工作绑定智能感知,并且在 运行 时不会导致问题。
我有一个带有此项目模板的 ListView:
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<views:ProjectListEntry />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
ProjectListEntry
相当复杂并且在另一个 ListView 中使用,所以我将它放在自己的文件中。我是这样设置的:
<ContentView
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
...
BindingContext="{x:Static vms:DesignTimeData.ProjectListEntryVm}">
如您所见,为了绑定 Intellisense(使用 ReSharper),我尝试将 BindingContext
设置为 DesignTimeData class 上的静态成员。这适用于我的页面(我在运行时替换 BindingContext),但对于 ListView 中使用的 ContentView,ContentView 的 BindingContext 似乎是继承的(我猜是从 ViewCell)。这意味着我的 ContentView 上的显式 BindingContext 实际上将覆盖 ListView 在 ViewCell 上设置的 BindingContext,并且我的所有列表元素都将在运行时反映静态设计时数据。但是,如果我删除 BindingContext,我将无法获得在 ContentView 文件中绑定到的成员的智能感知。
有没有一种简单的方法可以像这样为 ContentView 中的绑定获取 Intellisense?
(如前所述,我无法在 ListView 定义中内联 ContentView,因为 ContentView 相当复杂并且在多个列表中使用。我也不能使用某种 VM 定位器,因为虽然我使用绑定,我没有使用 "full" MVVM - 我使用的是类似 Redux 的架构。而且我猜 VM 定位器无论如何都不适用于这种情况,原因与上述不起作用相同.)
(从 Xamarin Forums 交叉发布,我没有收到任何回复。)
This solution 似乎工作正常。简而言之:添加
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
并使用 d:DataContext
而不是 BindingContext
。从上面的例子来看,它应该是这样的:
<ContentView
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
...
BindingContext="{x:Static vms:DesignTimeData.ProjectListEntryVm}">
这会导致来自 ReSharper 的工作绑定智能感知,并且在 运行 时不会导致问题。