【问题标题】:x:Bind Design Time issuesx:绑定设计时问题
【发布时间】: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 时,我看到另一个下划线(&lt;resources:ItemTemplates /&gt; 行)显示:

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


【解决方案1】:

对于当前版本,首选 Binding 而不是 x:Bind

an answer from a Microsoft engineers

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

【讨论】:

  • -1 你说更喜欢Binding 好像这得到了你的链接和微软的支持。但事实并非如此。在您的链接中,来自 Microsoft 的某个人决定使用 Binding,因为 Design Time Data 而不是因为 Visual Studio 的设计时间模式存在问题。所以这并不能真正回答问题。
  • 仍有一些属性不能与 x:Bind 一起使用
猜你喜欢
  • 1970-01-01
  • 2018-03-07
  • 1970-01-01
  • 2018-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-26
相关资源
最近更新 更多