【问题标题】:Cannot resolve DataType MyApp.Model.Paper无法解析 DataType MyApp.Model.Paper
【发布时间】:2016-11-13 14:58:59
【问题描述】:

我正在尝试将类绑定到模板。

<UserControl
    x:Class="MyApp.Controls.PaperSelectControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MyApp.Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400">

    <Grid>
        <ScrollViewer>
            <GridView x:Name="paperGrid" 
                      ItemsSource="{x:Bind Papers}"
                      Width="400" Height="300" >
                <GridView.ItemTemplate >
                    <DataTemplate x:DataType="MyApp.Model.Paper" >
                        <TextBlock Text="{x:Bind Color}"/>
                    </DataTemplate>
                </GridView.ItemTemplate>
            </GridView>
        </ScrollViewer>
    </Grid>
</UserControl>

MyApp.Model.Paper 是类命名空间。

namespace MyApp.Model {
    public class Paper {
        public string Name { get; set; }
        public string Color { get; set; }
        public string Thumb { get; set; }
    }
}

但我总是得到

无法解析 DataType MyApp.Model.Paper

错误

我希望有人可以帮助我解决这个问题。谢谢。

【问题讨论】:

  • x 在 XAML 文件的标题中指向什么?
  • 我刚刚添加了完整的标题标记! @user3185569

标签: c# xaml data-binding uwp uwp-xaml


【解决方案1】:

据我所知,我们无法通过namespace.class 格式设置x:DataType 标记。要访问您自己的自定义类型,您可以映射 XAML 命名空间,此映射是通过定义 xmlns 前缀来实现的。例如,xmlns:myTypes 定义了一个新的 XAML 命名空间,通过在所有用法前加上标记 myTypes: 来访问该命名空间。

所以请将此映射xmlns:model="using:MyApp.Model" 添加到您的标头标记列表中。并更新了x:DataType 的 XAML 代码,如下:&lt;DataTemplate x:DataType="model:Paper" &gt;,然后构建你的项目,它就可以工作了。

更多详情请参考Mapping custom types to XAML namespaces and prefixes

【讨论】:

    猜你喜欢
    • 2022-10-21
    • 1970-01-01
    • 2016-01-13
    • 1970-01-01
    • 2020-03-16
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    相关资源
    最近更新 更多