从资源中使用PathIcon会导致XamlParseException

时间:2016-01-27 13:39:41

标签: windows xaml windows-runtime windows-phone-8.1 winrt-xaml

8.1的Universal Store Project。

我在ResourceDictionary中声明了<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

两种方式都让我看到一个看起来很好的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 实例(icon1和icon2似乎相同)。

AppBarButton

我尝试使用 SomeCommandBar.PrimaryCommands.Add(new AppBarButton(){ Label = "Label", Icon = icon1 or icon2, Command = SomeCommand }); 的图标:

XamlParseException

问题是:当我使用icon2(使用XamlReader创建)时,一切正常,但是当我使用icon1(从resourcedictionary获取)时,我得到"Failed to assign to property '%0'. [Line: 0 Position: 0]"

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

我很感激为什么会发生这种情况。

更新

这也不起作用(错误与上面相同):

XmlReader.Load()

所以,我想,这根本无法发挥作用。它根本不适用于这个地方的静态资源。猜测我必须每次都存储带有图标的字符串资源和 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 } } }; ,正如Chris W.在评论中所建议的那样。

无论其

以下内容因某些原因(并非以任何方式有用)起作用:

Function ConvertStringToNumber(input As String, pattern As Char()) As Integer
    Dim number As Integer = 0
    Dim charDigits = pattern.ToList()
    Dim numberBase = charDigits.Count
    For i As var = 0 To input.Length - 1
        Dim digit = charDigits.IndexOf(input(input.Length - 1 - i))
        If digit <> -1 Then
            number += digit * CInt(Math.Pow(numberBase, i))
        End If
    Next
    Return number
End Function

1 个答案:

答案 0 :(得分:1)

我发现资源的声明性XAML中的数据路径比Data属性的隐式转换器复杂得多。由于您将它作为Data属性工作,因此让Visual Studio完成工作。在XAML设计器中打开属性对话框,然后单击右侧的小方块(首先是黑色)net到Data属性,然后选择&#34; Make Resource&#34;。生成的向导将指导您,您将看到数据字符串已转换为其组成部分,并且在您执行此操作后将按照您希望的方式工作。

enter image description here

祝你好运。