5

我正在尝试构建我的 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 中的文本“Models.Categories”)和我的 ListView 的ItemTemplate属性带有下划线并显示以下错误:

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。同样,该项目仍然可以正常编译和运行,但设计者甚至不会费心尝试显示输出。

4

1 回答 1

0

对于这个当前版本,更喜欢 Binding 而不是 x:Bind

微软工程师的回答

我遇到了和你一样的问题,加上在设计时使用 x:Bind 时出现了大量错误。最快的修复方法:将 Binding 用作旧时。并且当您发布时,如果正在考虑性能,请将 Binding 更改为 x:Bind

于 2016-05-05T10:45:57.937 回答