我有一个xml将FontAwesome图标保存为字符串,如
<Icon Name="faMainMenuFile">
<Content>FolderOutline</Content>
<Size>50</Size>
</Icon>
所以我解析了xml文件,我将得到FolderOutline字符串
但是将图标字段设为
faMainMenuFile.Icon = FontAwesome.WPF.FontAwesomeIcon.FolderOutline
我尝试创建一个将字符串转换为FontAwesome.WPF.FontAwesomeIcon的函数
public FontAwesome.WPF.FontAwesomeIcon GetUIFontAwesome(string strIcon)
{
if(strIcon == "Calculator") return FontAwesome.WPF.FontAwesomeIcon.Calculator;
if (strIcon == "FolderOutline") return FontAwesome.WPF.FontAwesomeIcon.FolderOutline;
return FontAwesome.WPF.FontAwesomeIcon.None;
}
但我会创建一个巨大的列表来处理这个功能,
我该如何解决?
答案 0 :(得分:1)
您可以执行此操作将字符串转换为枚举:
public FontAwesome.WPF.FontAwesomeIcon GetUIFontAwesome(string strIcon)
{
FontAwesome.WPF.FontAwesomeIcon item;
if (Enum.TryParse(strIcon, out item))
return item;
else
return FontAwesome.WPF.FontAwesomeIcon.None;
}