Save WinForm or Controls to File

后端 未结 6 1989
旧巷少年郎
旧巷少年郎 2021-01-24 09:52

I have been working on an application which allows the user to make a label template for printing purposes by adding label controls to a panel(which I use as a container). I ha

6条回答
  •  面向向阳花
    2021-01-24 10:32

    Personally, I would serialize it as JSON. When bringing it back you can use a generic method that loops through and sets the properties through reflection. Also take notice that the library I've linked to will automatically serialize objects that you pass to it.

    JSON

    JSON.NET

    [{ "Label": [{"Top": 102}, {"Left": 105}, {"Text": "blah, blah"}] }]
    

    From JSON.NET

    Product product = new Product();
    product.Name = "Apple";
    product.Expiry = new DateTime(2008, 12, 28);
    product.Price = 3.99M;
    product.Sizes = new string[] { "Small", "Medium", "Large" };
    
    string json = JsonConvert.SerializeObject(product);
    //{
    //  "Name": "Apple",
    //  "Expiry": new Date(1230422400000),
    //  "Price": 3.99,
    //  "Sizes": [
    //    "Small",
    //    "Medium",
    //    "Large"
    //  ]
    //}
    
    Product deserializedProduct = JsonConvert.DeserializeObject(json);
    

提交回复
热议问题