0

I'm writing an app where I have create a few controls of my own. One of these controls are being added to a ListBox dynamically after the user's wish.

This controls contains 4 TextBoxes, and each of these TextBoxes get their data from the user when he/she enters them. But the thing i cannot get to work, is how to save them. I tried to save the list as a normal list - which didn't work. See my code:

Declarations:

List<scheduleControl> scheduleList;
IsolatedStorageSettings settings;

Initialization of settings:

settings = IsolatedStorageSettings.ApplicationSettings;

Add the control dynamically to the list:

        private void addscheduleitemButton_Click(object sender, RoutedEventArgs e)
    {
        // addscheduleitemButton was tapped, add an item to the list holding the information the user entered

        scheduleControl control = new scheduleControl();
        control.fromTextBlock.Text = "From: " + fromTextBox.Text.ToString();
        control.toTextBlock.Text = "To: " + toTextBox.Text.ToString();
        control.detailsTextBlock.Text = "Details: " + detailsTextBox.Text.ToString();
        control.dateTextBlock.Text = "Date: " + dateTextBox.Text.ToString();

        // Create Thickness
        Thickness stackpanelThickness = new Thickness();
        stackpanelThickness.Bottom = 20;
        stackpanelThickness.Top = 0;
        stackpanelThickness.Right = 0;
        stackpanelThickness.Left = 0;

        // Assign the thickness
        control.Margin = stackpanelThickness;

        // Add the control
        scheduleListBox.Items.Add(control);
        scheduleList.Add(control);
    }

Control xaml:

<UserControl x:Class="App_Name.scheduleControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}" Height="132" Width="359">

<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
    <TextBlock x:Name="dateTextBlock" HorizontalAlignment="Left" Margin="10,0,0,0" TextWrapping="Wrap" Text="Date" VerticalAlignment="Top" FontSize="22" Height="32" Width="152"/>
    <TextBlock x:Name="fromTextBlock" HorizontalAlignment="Left" Margin="10,47,0,0" TextWrapping="Wrap" Text="From:" VerticalAlignment="Top" FontSize="22" Height="28" Width="152"/>
    <TextBlock x:Name="toTextBlock" HorizontalAlignment="Left" Margin="10,93,0,0" TextWrapping="Wrap" Text="To:" VerticalAlignment="Top" FontSize="22" Width="152"/>
    <TextBlock x:Name="detailsTextBlock" Margin="167,0,0,0" TextWrapping="Wrap" Text="Details:" FontSize="22" HorizontalAlignment="Left" Width="192"/>

</Grid>
</UserControl>

Saving the list when done:

                // Save the scheduleList
            settings.Add(projectnameTextBox.Text.ToString() + "-scheduleList", scheduleList);
settings.Save();

I get an InvalidDataControlException, which is think is due to this since the log said something about UIElement being serialized.

Please help!:)

Erik
  • 799
  • 2
  • 15
  • 27

1 Answers1

0

Save content of your controls, not the controls.

IsolatedStorageSettings.ApplicationSettings can save Lists of your own objects, but it's require additional code at your side. Here is a good example.

Community
  • 1
  • 1
crea7or
  • 4,421
  • 2
  • 26
  • 37
  • Hmm, okay - that'll add a little bit more work - what would be the best approach for this? – Erik Jan 19 '14 at 13:10
  • Store your controls in classes and serialize them(classes) into files. You can use [XMLserializer](http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(v=vs.110).aspx) for this task. You can [try this code](http://stackoverflow.com/questions/5073925/problem-storing-a-list-of-objects-in-isolated-storage) if you want to save lists into IsolatedStorage. I have edited original answer, because isolatedstorage.settings can save lists of objects, but not directly. – crea7or Jan 19 '14 at 13:18