【问题标题】:Saving controls (UIElements) in list in IsolatedStorage在 IsolatedStorage 的列表中保存控件 (UIElements)
【发布时间】:2014-01-19 21:22:12
【问题描述】:

我正在编写一个应用程序,我在其中创建了一些自己的控件。这些控件之一正在根据用户的意愿动态添加到 ListBox。

该控件包含 4 个文本框,每个文本框在用户输入时都从用户那里获取数据。但我无法开始工作的是如何拯救他们。我试图将列表保存为普通列表 - 这没有用。查看我的代码:

声明:

List<scheduleControl> scheduleList;
IsolatedStorageSettings settings;

设置初始化:

settings = IsolatedStorageSettings.ApplicationSettings;

将控件动态添加到列表中:

        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);
    }

控制 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>

完成后保存列表:

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

我得到了一个 InvalidDataControlException,这是因为日志中提到了 UIElement 被序列化。

请帮忙!:)

【问题讨论】:

    标签: c# serialization windows-phone-8 windows-phone


    【解决方案1】:

    保存控件的内容,而不是控件。

    IsolatedStorageSettings.ApplicationSettings 可以保存您自己的对象列表,但它需要您身边的额外代码。这是good example

    【讨论】:

    • 嗯,好吧 - 这会增加一点工作 - 最好的方法是什么?
    • 将您的控件存储在类中并将它们(类)序列化到文件中。您可以使用XMLserializer 执行此任务。如果您想将列表保存到独立存储中,您可以try this code。我已经编辑了原始答案,因为 isolatedstorage.settings 可以保存对象列表,但不能直接保存。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-11
    • 1970-01-01
    相关资源
    最近更新 更多