DataTemplate 无法解析 DataType 前缀数据

DataTemplate not able to resolve DataType prefix data

我正在尝试使用 Window 的示例代码在 UWP 中实现 ListView

<ListView.GroupStyle>
            <GroupStyle >
                <GroupStyle.HeaderTemplate>
                    <DataTemplate x:DataType="data:GroupInfoList">
                        <TextBlock Text="{x:Bind Key}" 
                                   Style="{ThemeResource TitleTextBlockStyle}"/>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
        </ListView.GroupStyle>

行 -

DataTemplate x:DataType="data:GroupInfoList"

给我错误,如左图所示,创建模型时我想创建它们 differently.It 说

The namespace prefix "data" is not defined.

它是我需要包含的命名空间吗?

在您的例子中,data:GroupInfoList 是名称空间映射 data 中的类型 GroupInfoList
您必须先定义名称空间映射才能使用它。

SimpleListViewSamplePage 元素中你应该有这样的东西:

<Page
    x:Class="HermantsListV2.Sample.SimpleListViewSample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:data="HermantsListV2.Model">
...

(注意 xmlns:data="HermantsListV2.Model 映射。)

只需将上例中的命名空间替换为您项目中的正确命名空间即可。

我刚刚遇到了同样的问题,此页面 copied/pasted 来自 Universal ListView 示例。

粘贴的页面充满了不良字符,例如换行符等。我通过删除数据模板周围的行和换行符来清理页面,一切正常。

如果您在 gridview 等中剪切代码,然后保存、构建并将其粘贴回去,我有时会设法使它正常工作。不知道为什么,但它有时会修复它,也许 VS 在那里隐藏了一些东西。

这是 Visual Studio 2015 中的一个错误 解决它只需评论那部分代码并 运行 它。之后取消注释,它将 运行 没有任何错误。

1- 注释这部分代码:

<!--<DataTemplate x:DataType="data:GroupInfoList">
          <TextBlock Text="{x:Bind Key}" 
                     Style="{ThemeResource TitleTextBlockStyle}"/>
 </DataTemplate>-->

2- 运行 您的应用。

3-取消注释这部分代码:

<DataTemplate x:DataType="data:GroupInfoList">
          <TextBlock Text="{x:Bind Key}" 
                     Style="{ThemeResource TitleTextBlockStyle}"/>
 </DataTemplate>

4- 运行 应用程序。