在 Windows Phone 中序列化 FontFamily

Serialize FontFamily in Windows Phone

我正在尝试使用 XmlSerializer 在我的 windows phone 应用程序中 read/save 一个 xml 文件。不幸的是我不能 read/write 因为我在 class:

中使用了 FontFamily
public class Article
{
    public string name { get; set; }
    private FontFamily _fontitem;
    public FontFamily FontItem
    {
        get
        {
            return _fontitem;
        }
        set
        {
            _fontitem = value;
            RaisePropertyChanged(() => FontItem);
        }
    }
}

我正在使用 class 命名的 StorageHelper,就像这里一样:http://blogs.msdn.com/b/dawate/archive/2010/08/31/windows-phone-7-xml-isolatedstorage-example.aspx 我使用 class "Article".

而不是 class "Jog"

当我尝试 load/save xml 时出现此错误:“fontfamily 无法序列化,因为它没有无参数构造函数”。 =14=]

在这种情况下,您可以根据其名称创建一个 FontFamily。因此,您可以将 Article class 修改为:

[Serializable]
public class Article
{
    private string _fontItemString;

    public string Name { get; set; }

    [XmlIgnore]
    public FontFamily FontItem
    {
        get
        {
            return new FontFamily(FontItemString);
        }
    }

    public string FontItemString
    {
        get { return _fontItemString; }
        set
        {
            _fontItemString = value;
            RaisePropertyChanged(() => FontItem);
        }
    }
}

这样您就不会序列化 FontFamily 对象本身,而是序列化要使用的字体系列的名称。每当字体系列字符串更改时,您可以通知实际 FontItem 已更改,然后从 FontItemString

初始化 FontFamily