如何将用户字体首选项保存到xml文件 [英] how to save user font preference to an xml file

查看:88
本文介绍了如何将用户字体首选项保存到xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求是将用户字体选择保存到XML文件并检索回来。

到目前为止,我所做的是。

在我的表格中有两个字体,名为 Fnt ChqDateF

用于写入XML文件的方法是

My requirement is to save user font selection to an XML file and retrieve back.
What i have done so far is.
In my form there is two Font named as Fnt and ChqDateF
The method i used to write to an XML file is

new XAttribute("F", ChqDateF==null ? Fnt:ChqDateF)



我得到的结果是


and the result i am getting is

F="[Font: Name=Tahoma, Size=19.75, Units=3, GdiCharSet=1, GdiVerticalFont=False]"



从这个字符串我想要检索FontName和FontSize。

在上面的字符串中FontStyle没有得到。我还需要FontStyle

什么是实现这个任务的最佳方法

请指导我

推荐答案

最好不要手动读/写XML,特别是你只需要启动一些选项。一切都已经为你完成了。请参阅: http://msdn.microsoft.com/en-us /library/ms733127%28v=vs.110%29.aspx [ ^ ]。



基本上,你只是创建一些数据类型(甚至是一组)类型形成复杂的数据结构,形成任何可想象的对象图,甚至不必是树)并使用 DataContract DataMember 属性。序列化程序将在您需要时自动将数据保存到任何流,并在以后完全恢复数据。你甚至不需要知道它是如何用XML表示的。



-SA
Better don't read/write XML manually, especially of you just need to start some set of options. Everything is already done for you. Please see: http://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx[^].

Basically, you just create some data type (or even a set of type to form complex data structure, forming any thinkable object graph, which does not even have to be a tree) and mark the types and the members you want to be a part of contract with DataContract and DataMember attributes. The serializer will automatically save the data to any stream when you need it, and will later restore the data exactly as it was before. You don't even need to know how is that represented in XML.

—SA


那么你可以在这里使用Xaml代码。

Well you can use Xaml here is the code.
public static FonSettings Load(string file)
{
    var p = XamlServices.Load(file) as FonSettings;
    return p;
}

public static void Save(string file, ref FonSettings pf)
{
    File.WriteAllText(file, XamlServices.Save(pf));
}



这里是FonSettings类


and here is the FonSettings class

public class FonSettings
{
    public Font FontToSave { get; set; }
}



你所做的就是加载并保存文件,每次都会返回班级设置。

希望它是完整的帮助。

编辑:

注意:你必须添加对dotnet中的System.Xaml dll的引用

您可以使用此方法保存所有变量。


all you do is load and save the file and it will return the class with setting every time.
Hope it was help full.

NOTE: you must add reference to the System.Xaml dll that is in dotnet
You can save all kinde of variables using this method.


一种解决方案是使用类型转换器 [ ^ ]如下例所示:

One solution is to use Type Convertor[^] like in the next example:
Font myFont = new Font("Arial", 12, FontStyle.Italic);
TypeConverter converter = TypeDescriptor.GetConverter(typeof(Font));
//
// Saving Font object as a string
string fontString = converter.ConvertToString(myFont);
//
// ...Then use this string into your XML
//
// Load an instance of Font from a string
Font font = (Font)converter.ConvertFromString(fontString);


这篇关于如何将用户字体首选项保存到xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆