【发布时间】:2016-03-10 11:10:51
【问题描述】:
我正在尝试构建我的 UWP 应用,但在尝试将 DataTemplate 与资源字典中的 x:Bind 一起使用时,我遇到了设计器异常。
我创建了一个带有相应代码隐藏的资源字典“ItemTemplates.xaml”(以确保 x:Bind 初始化)。该文件只包含一个模板:
<DataTemplate x:Key="HomeViewCategoryListItemTemplate" x:DataType="models:Category">
<Button Background="#88333333" Height="110" VerticalContentAlignment="Top" Padding="10" HorizontalAlignment="Stretch">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock FontWeight="Light" HorizontalAlignment="Center" Text="{x:Bind Name}" FontSize="{ThemeResource TextStyleExtraLargeFontSize}" />
<TextBlock Foreground="{ThemeResource ToolTipForegroundThemeBrush}" HorizontalAlignment="Center" Margin="0,10,0,0" Text="{x:Bind Description}" Grid.Row="1" TextAlignment="Center" TextWrapping="Wrap" />
</Grid>
</Button>
</DataTemplate>
然后我将这个资源字典添加到 App.xaml 中,如下所示:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///Resources/Core.xaml" />
<resources:ItemTemplates />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
现在项目无法使用,因为设计器抛出了奇怪的异常,但是当我清理并重建项目并导航到 HomeView.xaml 页面时,设计器只显示默认的“ToString()”项(基本上列表视图包含ListView 和我的 ListView 的 ItemTemplate 属性中只有三倍的文本“Models.Categories”)带有下划线并显示以下错误:
The resource "HomeViewCategoryListItemTemplate" could not be resolved.
当我导航回 App.xaml 时,我看到另一个下划线(<resources:ItemTemplates /> 行)显示:
The property 'DataType' was not found in type 'DataTemplate'.
这两个错误都是无意义的,因为当我实际运行该应用程序时,没有任何问题并且一切正常。到目前为止,我发现的唯一解决方法是以经典方式和“编译”方式两次包含 ResourceDictionary:
<ResourceDictionary Source="ItemTemplates.xaml" />
<resoures:ItemTemplates />
这个解决方案有效,然后在设计时和运行时一切都有效,但我真的认为它非常混乱,必须有更好、更安全的方法,否则我会遗漏一些琐碎的东西。
我正在运行 Visual Studio 2015 Update 1 并安装了最新的 UWP SDK。该项目的目标是构建 10240。
编辑: 设计师经常抛出的另一个异常并完全崩溃:
Unable to cast object of type 'System.String' to type 'Models.Data.Categories.Category'.
根据 StackTrace 输出,这发生在 ItemTemplates.xaml.cs 代码中 - 特别是生成的方法 ProcessBindings。同样,该项目仍然可以正常编译和运行,但设计者甚至懒得尝试显示输出。
【问题讨论】:
-
您的 ItemTemplates 类代码是什么样的?也许本指南有帮助? igrali.com/2015/06/14/…
-
我听到了你的痛苦。即使一切正常运行,我也遇到了很多 UWP 应用程序的设计器问题......
-
sibbl:我的 ItemTemplates 不起作用,即使它们只有简单的 '
' 内容。我确实检查并按照您链接的指南中的步骤进行操作,但没有帮助...
标签: binding datatemplate uwp designer xbind