简体   繁体   English

在C#中使用XML文件创建GUI

[英]Creating GUI using XML file in C#

How can I create automatic GUI using XML file in C#? 如何在C#中使用XML文件创建自动GUI? Should I write a parser for the file and define a sort of "protocol" for the file's structure, and after the parse - create the GUI controls manually (respective to the data in the files)? 我是否应该为文件编写一个解析器并为文件的结构定义某种“协议”,然后在解析之后-手动创建GUI控件(与文件中的数据有关)?

Or is there a better way? 或者,还有更好的方法? Is there a tool, or a built-in code in the .NET environment which can do that for me automatically? .NET环境中是否有工具或内置代码可以自动为我执行此操作?

(I am currently working with win forms, but I am willing to consider any other technology - as long as it's supported in MONO, since the code should be portable to Linux as well). (我目前正在使用win窗体,但是我愿意考虑任何其他技术-只要MONO支持该技术,因为该代码也应该可以移植到Linux)。

Glade is a RAD tool to enable quick & easy development of user interfaces for the GTK+ toolkit and the GNOME desktop environment. Glade是RAD的一种工具,可用于GTK +工具箱和GNOME桌面环境的快速,轻松开发用户界面。

The user interfaces designed in Glade are saved as XML, and by using the GtkBuilder GTK+ object these can be loaded by applications dynamically as needed. Glade中设计的用户界面另存为XML,通过使用GtkBuilder GTK +对象,应用程序可以根据需要动态加载这些用户界面。

By using GtkBuilder, Glade XML files can be used in numerous programming languages including C, C++, C#, Vala, Java, Perl, Python,and others. 通过使用GtkBuilder,Glade XML文件可以在多种编程语言中使用,包括C,C ++,C#,Vala,Java,Perl,Python等。

I've used glade with C# and I was pleased with the result. 我在C#中使用了林间空地,并且对结果感到满意。 Glade probably won't suit you directly, but you can at least borrow some ideas from it. Glade可能不会直接适合您,但是您至少可以从中借鉴一些想法。

If you're going to use XML, then you should really know about XML schemas - these are XML files that describe the content of an XML file and DevStudio (and other editors) can read them and do autocompletion, which is useful. 如果要使用XML,那么您应该真正了解XML模式-这些是描述XML文件内容的XML文件,DevStudio(和其他编辑器)可以读取它们并进行自动补全,这很有用。 Also, you can validate an XML against a schema to ensure the content contains no structural errors. 此外,您可以根据架构验证XML,以确保内容不包含任何结构性错误。

Also, as Paul wrote, XAML is an XML system, but you'd need to use WPF framework to parse it. 同样,正如Paul所写的那样,XAML是XML系统,但是您需要使用WPF框架来对其进行解析。

WPF使用xml定义大多数东西,即xaml。

"Is there a tool, or a built-in code in the .NET environment which can do that for me automatically?" “ .NET环境中是否存在可以自动为我执行此操作的工具或内置代码?”

There are various wrappers for .NET around XULRunner on code.google.com 在code.google.com上,围绕XULRunner的.NET有各种包装。

Check out: 查看:

https://social.msdn.microsoft.com/Forums/en-US/554eefae-429f-495c-aee0-b2e971494ed0/how-do-i-create-a-gui-which-reads-xml-file-and-adds-controls-to-it-at-runtime?forum=csharplanguage https://social.msdn.microsoft.com/Forums/en-US/554eefae-429f-495c-aee0-b2e971494ed0/how-do-i-create-a-gui-which-reads-xml-file-and-增加-控制到它,在运行时?论坛= csharplanguage

using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Linq;

namespace WindowsFormsApplication_DynamicGUIFromXML
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        XDocument guiConfig = XDocument.Load(@"../../Gui.xml");

        foreach (XElement item in from y in guiConfig.Descendants("Item") select y)
        {
            Control tmp = new Control();
            switch (item.Attribute("type").Value)
            {
                case "Button":
                    tmp = new Button();
                    break;
                case "TextBox":
                    tmp = new TextBox();
                    break;
            }

            tmp.Name = item.Attribute("name").Value;
            tmp.Text = item.Attribute("text").Value;
            tmp.Location = new Point(Int32.Parse(item.Attribute("x").Value), Int32.Parse(item.Attribute("y").Value));
            Controls.Add(tmp);
        }

    }
    }
 }

// ***********************************************
// Contents of Gui.xml
// ***********************************************
//<?xml version="1.0" encoding="utf-8" ?>
//<Gui>
//  <Item type="Button" name="foo" text="bar" x="100" y="100"  />
//  <Item type="TextBox" name="foo2" text="bar2" x="200" y="200"  />
//</Gui>
// ***********************************************

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM