简体   繁体   中英

Creating GUI using XML file in C#

How can I create automatic GUI using XML file in C#? 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)?

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?

(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).

Glade is a RAD tool to enable quick & easy development of user interfaces for the GTK+ toolkit and the GNOME desktop environment.

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.

By using GtkBuilder, Glade XML files can be used in numerous programming languages including C, C++, C#, Vala, Java, Perl, Python,and others.

I've used glade with C# and I was pleased with the result. Glade probably won't suit you directly, but you can at least borrow some ideas from it.

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. Also, you can validate an XML against a schema to ensure the content contains no structural errors.

Also, as Paul wrote, XAML is an XML system, but you'd need to use WPF framework to parse it.

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

"Is there a tool, or a built-in code in the .NET environment which can do that for me automatically?"

There are various wrappers for .NET around XULRunner on code.google.com

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

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>
// ***********************************************

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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