示例#1
0
        public void LoadWindow(string str)
        {
            Window = new PopupWindow();
            Dictionary <string, object> data = JsonConvert.DeserializeObject <Dictionary <string, object> >(str);

            foreach (string Key in data.Keys)
            {
                object value = data[Key];
                switch (Key)
                {
                case "type":
                    EnsureType(typeof(string), value, "type");
                    if ((string)value != "window")
                    {
                        throw new Exception("Not a Window type.");
                    }
                    break;

                case "title":
                    EnsureType(typeof(string), value, "title");
                    this.Title = (string)value;
                    break;

                case "width":
                    EnsureType(typeof(long), value, "width");
                    this.Width = Convert.ToInt32(value);
                    if (this.Width < 1)
                    {
                        throw new Exception($"Window width ({this.Width}) must be greater than 0.");
                    }
                    break;

                case "height":
                    EnsureType(typeof(long), value, "height");
                    this.Height = Convert.ToInt32(value);
                    if (this.Height < 1)
                    {
                        throw new Exception($"Window height ({this.Height}) must be greater than 0.");
                    }
                    break;

                case "fonts":
                    if (!(value is JObject))
                    {
                        throw new Exception($"Expected an Object, but got a {value.GetType().Name} in key 'fonts'");
                    }
                    Dictionary <string, object> fonts = ((JObject)value).ToObject <Dictionary <string, object> >();
                    foreach (string font in fonts.Keys)
                    {
                        if (!(fonts[font] is JObject))
                        {
                            throw new Exception($"Expected an Object, but got a {fonts[font].GetType().Name} in key '{font}' inside 'fonts'");
                        }
                        this.Fonts.Add(font, ParseFont(((JObject)fonts[font]).ToObject <Dictionary <string, object> >()));
                    }
                    break;

                case "widgets":
                    if (!(value is JObject))
                    {
                        throw new Exception($"Expected an Object, but got a {value.GetType().Name} in key 'widgets'");
                    }
                    this.MainWidgets = ParseWidgets(Window, ((JObject)value).ToObject <Dictionary <string, object> >());
                    break;

                case "save":
                    if (!(value is JObject))
                    {
                        throw new Exception($"Expected an Object, but got a {value.GetType().Name} in key 'save_format'");
                    }
                    Dictionary <string, object> format = ((JObject)value).ToObject <Dictionary <string, object> >();
                    foreach (string formatkey in format.Keys)
                    {
                        object formatvalue = format[formatkey];
                        switch (formatkey)
                        {
                        case "filename":
                            EnsureType(typeof(string), formatvalue, "filename");
                            SaveFormat.Add("filename", (string)formatvalue);
                            break;

                        case "format":
                            if (!(formatvalue is JObject))
                            {
                                throw new Exception($"Expected an Object, but got {formatvalue.GetType().Name} in key 'format'");
                            }
                            SaveFormat.Add("format", ParseSaveFormatObject(((JObject)formatvalue).ToObject <Dictionary <string, object> >()));
                            break;

                        default:
                            throw new Exception($"Unknown key '{formatkey}' inside save format definition");
                        }
                    }
                    break;

                default:
                    throw new Exception($"Unknown key '{Key}' inside window definition.");
                }
            }
            if (!string.IsNullOrEmpty(this.Title))
            {
                Window.SetTitle(this.Title);
            }
            if (this.Width != -1)
            {
                Window.SetWidth(this.Width);
            }
            if (this.Height != -1)
            {
                Window.SetHeight(this.Height);
            }
            Window.Center();
        }