C# WPF Serialization of Button Dynamic StackPanel



  • The project has a problem with the seriesing of the dynamic Buttonov's, which the user produces on the course of the programme, inside buttono's, as appropriate, contains the information necessary for the work of the programme. Here's one example of filling the button.

    XmlReader readxml = XmlReader.Create(textBox.Text);
    var feed = SyndicationFeed.Load<SyndicationFeedEx>(readxml);
    feed.SourceUri = textBox.Text;
    

    int Z = Stackpnelleft.Children.Count +1;
    if (Z == 0) { Z++; };

    TextBlock TX = new TextBlock();
    Button ButtonTitle = new Button();
    ContextMenu contextmenu = new ContextMenu();
    MenuItem mi = new MenuItem();

    TX.TextWrapping = TextWrapping.Wrap;
    TX.Text = feed.Title.Text;
    TX.TextAlignment = TextAlignment.Center;
    ButtonTitle.Content = TX;

    //ButtonTitle.Uid = "BBB" + Z + "";
    ButtonTitle.Tag = feed;

    ButtonTitle.ToolTip = ""+ feed.SourceUri + "";

    ButtonTitle.ContextMenu = contextmenu;
    mi.Header = "Удалить";
    mi.Tag = ButtonTitle;
    mi.Click += Delete_Channel;

    l = TX.Text;
    ButtonTitle.Click += OnFeedClick;

    contextmenu.Items.Add(mi);
    Stackpnelleft.Children.Add(ButtonTitle);

    That's a lot of buttons, right? The question is simply how to teach the program to remember these buttons at the end of the session, and at the beginning of the new session, to reproduce those that were closed.



  • There are two options. There's no lung and simple among them.

    Option one: Serialization. It's like VladD already wrote. It's a class where the object shows up and deseries. Works on the following principle: The designer of the class is given a visual object that is based. Next, in the designer, the class extracts all the properties that you need to preserve and, in consequence, re-establish the object (length, height, position, content, etc.). After you set up the facility, you're feeding it to one of the standard scouts. About:

    public void SaveTemplate(string filename)
        {
            VisualElementList.Clear();
            AddNewVisualElement(CurentField);
    
        foreach (UIElement curent in CurentField.Children)
        {
            AddNewVisualElement((FrameworkElement)curent);
        }
    
        Stream stream = File.Open(filename, FileMode.Create);
        SoapFormatter formatter = new SoapFormatter();
    
        //BinaryFormatter formatter = new BinaryFormatter();
        //XmlSerializer serializer = new XmlSerializer(typeof(VisualElement));
    
    
    formatter.Serialize(stream, VisualElementList.ToArray());
        stream.Close();
    }
    

    At your disposal, 3 standard series: BinaryFormatter, SoapFormatter and XmlSerializer. MSDN'e has their detailed description.

    The reverse process (recovery of objects) is approximately as follows: You open the file and feed it to that shower you've been recording data there. About:

        public void LoadTemplate(Stream FileStream)
    {
    try
    {
    using (FileStream)
    {
    SoapFormatter formatter = new SoapFormatter();
    VisualElementList.Clear();
    VisualElementList.AddRange((VisualElement[])formatter.Deserialize(FileStream));
    }
    }
    catch (Exception ex)
    {
    /Сериализаторы очень капризны и придирчивы к формату. Добавите в сериалиуемый объект новое поле и всё, старые файлы перестанут сериализоваться./
    }
    }

    Further important nuances: Your facilities, on the basis of their condition (the values of all their properties), should be able to create visual objects. I mean, if you did MySButton class, he should have a method like GetButton that returns a class Button object identical to the one that was transferred to MySButton.
    You're in the cycle bypassing the entire collection of desserialized objects, each pulling its basic visual object and adding it to your form. That's it.

    Option two: XamlReader and XamlWriter.
    As understood by the class names, they work with the Xaml markings (respectively the varian only works for WPF and others using this foam) Read MSDN'e.

    XamlWriter can generate a mark on the visual object that he has been given.
    XamlReader, respectively, can create objects on the marking given to him.

    About:

      FileStream fs = new FileStream(filename, FileMode.Create);
    XamlWriter.Save(VEObj, fs);//VEObj -объект класса UIElement

    Stream fs = new FileStream(filename, FileMode.Open);
    VEObj = XamlReader.Load(fs) as UIElement;

    The second option seems easier, but there are many nuances in the XamlReader and XamlWriter classes. Not every control and not all of their properties, they'll be able to preserve and restore correctly.

    PS: All work with visual facilities should go to STA in the flow (graphic flow) of your UI, respectively, during deseration and rebuilding, fries.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2