使用资源中的 PathIcon 导致 XamlParseException

Using PathIcon from resources results in XamlParseException

此处为 8.1 的通用商店项目。

我在 ResourceDictionary 中声明了一个 PathIcon,如下所示:

<PathIcon 
    x:Key="PhoneIcon" 
    Data="F0 M22,22z M0,0z M17.4,22.533333C19.111111,25.955556,22.044444,28.766667,25.466667,30.6L28.155556,27.911111C28.522222,27.544444 29.011111,27.422222 29.377778,27.666667 30.722222,28.155556 32.188889,28.4 33.777778,28.4 34.511111,28.4 35,28.888889 35,29.622222L35,33.777778C35,34.511111 34.511111,35 33.777778,35 22.288889,35 13,25.711111 13,14.222222 13,13.488889 13.488889,13 14.222222,13L18.5,13C19.233333,13 19.722222,13.488889 19.722222,14.222222 19.722222,15.688889 19.966667,17.155556 20.455556,18.622222 20.577778,18.988889 20.455556,19.477778 20.211111,19.844444L17.4,22.533333 17.4,22.533333z"
    />

我可以像这样在后面的代码中获取此资源:

PathIcon icon1 = null;
object resource;
if (Application.Current.Resources.TryGetValue("PhoneIcon", out resource)) {
  icon1 = resource as PathIcon;
};

或者,我可以这样创建它(避免查询资源字典):

var icon2 = XamlReader.Load(
  @"<PathIcon 
        xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" 
        Data=""F0 M22,22z M0,0z M17.4,22.533333C19.111111,25.955556,22.044444,28.766667,25.466667,30.6L28.155556,27.911111C28.522222,27.544444 29.011111,27.422222 29.377778,27.666667 30.722222,28.155556 32.188889,28.4 33.777778,28.4 34.511111,28.4 35,28.888889 35,29.622222L35,33.777778C35,34.511111 34.511111,35 33.777778,35 22.288889,35 13,25.711111 13,14.222222 13,13.488889 13.488889,13 14.222222,13L18.5,13C19.233333,13 19.722222,13.488889 19.722222,14.222222 19.722222,15.688889 19.966667,17.155556 20.455556,18.622222 20.577778,18.988889 20.455556,19.477778 20.211111,19.844444L17.4,22.533333 17.4,22.533333z"" 
        />"
) as PathIcon;

这两种方式都让我得到了一个看起来不错的 PathIcon 实例(icon1 和 icon2 似乎是相同的)。

Debug.WriteLine( 
  "{0} equals {1}: {2}",
  icon1.Data.Bounds, icon2.Data.Bounds,
  icon1.Data.Bounds.Equals(icon2.Data.Bounds)
); // outputs 13,13,22,22 equals 13,13,22,22: True

我正在尝试将图标用于 AppBarButton:

        SomeCommandBar.PrimaryCommands.Add(new AppBarButton(){
          Label = "Label",
          Icon = icon1 or icon2,
          Command = SomeCommand
        });    

问题是:当我使用 icon2(使用 XamlReader 创建)时,一切正常,但是当我使用 icon1(从 resourcedictionary 获取)时,我得到 XamlParseException:

"Failed to assign to property '%0'. [Line: 0 Position: 0]"

对于为什么会发生这种情况的任何想法,我将不胜感激。

更新

这个也不行(报错同上):

<Page.BottomAppBar>
    <CommandBar>
        <AppBarButton
            Label="Test"
            Icon="{StaticResource PhoneIcon}"
            />
    </CommandBar>
</Page.BottomAppBar>

所以,我想,这根本行不通。它根本不适用于这个地方的静态资源。猜猜我必须每次都用图标和 XmlReader.Load() 存储字符串资源,正如 Chris W. 在评论中建议的那样。

然而

出于某种原因,以下内容确实有效(并不是说它有任何用处):

  PathIcon icon1 = null;
  object resource;
  if (Application.Current.Resources.TryGetValue("PhoneIcon", out resource)) {
    icon1 = resource as PathIcon;
    // if the resource is removed from the dictionary before it is used,
    // no exception is thrown.
    foreach(var m in Application.Current.Resources.MergedDictionaries) {
      if (m.ContainsKey("PhoneIcon")) {
        m.Remove("PhoneIcon"); // This does it
      }
    }
  };

我发现资源的声明XAML中的数据路径比数据属性的隐式转换器复杂得多。由于您将其用作数据 属性,因此让 Visual Studio 完成工作。在 XAML 设计器中打开属性对话框,然后单击右边的小方块(一开始它是黑色的)网络到数据 属性 和 select "Make Resource"。生成的向导将为您提供指导,您将看到数据字符串已转换为它的组成部分,并且在您执行此操作后将以您希望的方式工作。

祝你好运。