我的Quickly应用程序的data / glib-2.0文件夹中的架构XML文件是什么?

我用Quickly创建了一个Ubuntu应用程序,我可以在项目根目录的data / glib-2.0文件夹中看到一个XML文件,我不确定这是为了什么。 XML看起来像这样:

    '' Sample setting Longer description of this sample setting. Talk about allowed values and what it does.    

此外,在使用新应用程序创建的默认首选项对话框代码中,我可以看到以下代码:

 settings = Gio.Settings("net.launchpad.sample-application") widget = self.builder.get_object('example_entry') settings.bind("example", widget, "text", Gio.SettingsBindFlags.DEFAULT) 

而且我不确定这是做什么的。

XML文件定义了一组密钥,您可以在整个应用程序中使用这些密钥来存储用户首选项。

在元素模式中,您将注意到两个属性:id和path。 id是您在实例化设置对象时用于引用代码中的模式的内容。 路径是存储密钥的位置。

您可以通过安装dconf-tools包并运行dconf-editor来查找设置。 然后导航到上面定义的节点。 问题中XML中的节点(/ net / launchpad / sample-application)将在这里:

路径/ net / launchpad / sample-application中的dconf-editor窗口

该架构用于GSettings API。 您可以在此处查看基于GTK C的文档: http : //developer.gnome.org/gio/stable/GSettings.html 。 这是Gio模块的一部分

至于代码。

settings = Gio.Settings("net.launchpad.sample-application")

这将创建GSettings对象,您可以使用该对象将窗口小部件绑定到存储设置。 该参数需要完全匹配XML中schema元素的id属性。

settings.bind("example", widget, "text", Gio.SettingsBindFlags.DEFAULT)

这将在架构中的example键与widgettext属性之间创建绑定。 传递给键的值是指定窗口小部件的text属性。 对于GtkEntry,text是最明显的属性。 因为它是绑定的,所以每当您在首选项窗口中更改属性的值时,它都会自动更新。 最终参数确定绑定的工作方向 – 有关详细信息,请参阅文档 。

您在密钥的XML中指定的类型应与其中一个可用选项匹配。 通常是单个字符,您将在定义的常量中看到它。 例如,对于布尔值:

#define G_VARIANT_TYPE_BOOLEAN((const GVariantType *)“b”)

所以布尔等于b

所以,只是说你想要一个布尔键,你可以在schema元素下面添加以下内容:

  false Whether the task is complete This key is used to determine if a particular part of the application is complete or not  

然后绑定活动属性,如下所示:

 # Get the widget (named chk_complete) complete = self.ui.chk_complete # Bind the property settings.bind("complete", complete, "active", Gio.SettingsBindFlags.DEFAULT) 

然后可以使用get函数访问设置,特定于数据类型。 例如

 settings = Gio.Settings("net.launchpad.sample-application") example = settings.get_string("example") complete = settings.get_boolean("complete")