Universal Store Project for 8.1在這裏。從資源中使用PathIcon導致XamlParseException
我有一個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 (從資源字典獲取),我得到一個XamlParseException
:
"Failed to assign to property '%0'. [Line: 0 Position: 0]"
我會很感激的任何想法,爲什麼這可能會發生。
UPDATE
這不工作,要麼(誤差同上):
<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
}
}
};
你是如何抓取它的? –
這是問題的第二個代碼塊。 –
啊對不起,以前一直在尋找{StaticResource ...}的xaml equiv,但你得到了靜態application.current,所以我想我可能是錯的。 –