【问题标题】:Correct way to specify type for storage in attached property?在附加属性中指定存储类型的正确方法?
【发布时间】:2015-03-19 12:40:45
【问题描述】:

我已经实现了一个附加属性,它保留了一个 System.Type 值,该值在 WPF 中运行良好,但会引发

Windows.UI.Xaml.Markup.XamlParseException

在 WinRT 应用程序中使用时。我猜无法解析指定的类型。

这里是附加属性的实现:

using System;
using System.Diagnostics;
#if NETFX_CORE
using Windows.UI.Xaml;
#else
using System.Windows;
#endif

namespace Test
{
    public class AttachedClass
    {
    }

    public static class PropaClass
    {
        public static Type GetAttachedType(DependencyObject obj)
        {
            return (Type)obj.GetValue(AttachedTypeProperty);
        }

        public static void SetAttachedType(DependencyObject obj, Type value)
        {
            obj.SetValue(AttachedTypeProperty, value);
        }

        public static readonly DependencyProperty AttachedTypeProperty =
            DependencyProperty.RegisterAttached("AttachedType",
            typeof(Type), typeof(PropaClass), new PropertyMetadata(null, OnAttachedTypeChanged));

        private static void OnAttachedTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Debug.WriteLine("Set value : {0}", e.NewValue.ToString(), null);
        }
    }
}

这是它在 WPF 应用程序中的使用方式:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:test="clr-namespace:Test"
    Title="MainWindow" Height="350" Width="525"
    test:PropaClass.AttachedType="{x:Type test:AttachedClass}">
<Grid>

</Grid>
</Window>

WinRT 应用页面:

<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:test="using:Test"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    test:PropaClass.AttachedType="test:AttachedClass">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    </Grid>
</Page>

我在 WinRT 应用程序中遇到的异常是:

Exception:Thrown: "Det gick inte att hitta texten som associeras med den här felkoden.

Failed to create a '%1' from the text '%0'. [Line: 10 Position: 5]" (Windows.UI.Xaml.Markup.XamlParseException)
A Windows.UI.Xaml.Markup.XamlParseException was thrown: "Det gick inte att hitta texten som associeras med den här felkoden.

Failed to create a '%1' from the text '%0'. [Line: 10 Position: 5]"
WinRT information: Failed to create a '%1' from the text '%0'. [Line: 10 Position: 5]
Time: 2015-03-19 12:05:00
Thread:<No Name>[396]

所以我的问题是:我做错了什么?如果我在 WinRT 页面 XAML 中将 test:PropaClass.AttachedType="test:AttachedClass"&gt; 替换为 test:PropaClass.AttachedType="test:PropaClass"&gt;,它就像一个魅力!

有没有办法让 XAML 解析器识别 AttachedClass?

附:这是使用针对 .NET 框架 4.5 和 Windows8.1 的 VS2013

编辑:

将此添加到 WinRT XAML 页面会删除异常。

<Page.Resources>
    <test:AttachedClass x:Key="test"/>
</Page.Resources>

因此,向解析器“引入”类型似乎可以解决问题。但是,这不是一个有效的解决方法,因为我希望能够使用不可实例化的类型。

我开始倾向于解析器错误...

另一个编辑:

我终于找到了一个可行的解决方法;添加一个空样式,针对要放入附加属性的类型。

添加下面的资源可以消除异常。

<Page.Resources>
    <Style TargetType="test:AttachedClass"/>
</Page.Resources>

此解决方案允许我将接口或抽象基类指定为类型以进入附加属性。

【问题讨论】:

  • 对于说英语的人,谷歌翻译说异常开头的错误消息是:“找不到与此错误代码关联的文本”
  • 翻译正确,遗漏见谅!

标签: c# xaml windows-runtime winrt-xaml attached-properties


【解决方案1】:

我已经尝试了上面“另一个编辑”中定义的解决方法,并且效果很好。我将它作为答案发布,因为它解决了我的问题并且没有发布其他答案。

此问题很可能是由于 XAML 解析器中的错误导致无法使用提供的名称解析类型。如果以另一种方式将类型引入解析器,则可以很好地识别它。我用来介绍类型的方式是定义一个空的、未使用的样式,以存储在附加属性中的类型为目标。

<Page.Resources>
    <Style TargetType="test:AttachedClass"/>
</Page.Resources>

这样任何类型都可以存储在属性中。

【讨论】:

    猜你喜欢
    • 2012-11-26
    • 1970-01-01
    • 2010-09-21
    • 1970-01-01
    • 2014-05-18
    • 1970-01-01
    • 2012-09-08
    • 2016-04-07
    • 1970-01-01
    相关资源
    最近更新 更多