如何在Windows Phone 8中保存聊天

时间:2015-05-12 13:06:02

标签: windows-phone-8

大家好我正在开发一个Windows手机聊天应用程序,我已经完成了两端聊天现在我正面临问题,当我重新打开我的应用程序时,我之前的所有聊天记录都被删除了。我如何保存以前的聊天记录请帮助我。 感谢

我正在使用以下代码

  <ListBox Name="listChat"  Grid.Row="0" ItemsSource="{Binding Path=Instance.Messages,Source={StaticResource Binder}}">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <Grid Width="430">
                                    <cc:ChatBubble Width="380" HorizontalAlignment="{Binding Converter={StaticResource MType},ConverterParameter=align}"   Opacity="{Binding Converter={StaticResource MType}}" ChatBubbleDirection="{Binding Converter={StaticResource MType},ConverterParameter=direction}" Margin="0,0,0,10"  >
                                        <Grid>
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="Auto"></RowDefinition>
                                                <RowDefinition Height="Auto"></RowDefinition>
                                                <RowDefinition Height="40"></RowDefinition>
                                            </Grid.RowDefinitions>

                                            <TextBlock Grid.Row="0" HorizontalAlignment="Left"  FontSize="17" Text="{Binding Name}"></TextBlock>
                                            <TextBlock  Grid.Row="0" HorizontalAlignment="Right" FontSize="17" Text="{Binding SendingDate}"></TextBlock>

                                            <TextBlock Grid.Row="1" Name="txt_Msg" Text="{Binding Text}" TextWrapping="Wrap" Width="430"></TextBlock>


                                        </Grid>
                                    </cc:ChatBubble>
                                </Grid>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

1 个答案:

答案 0 :(得分:1)

最简单的方法是隔离存储

例如,我有这个助手类

public static class SettingsManager
{
    private static IsolatedStorageSettings appSettings;

    public static IsolatedStorageSettings AppSettings
    {
        get { return SettingsManager.appSettings; }
        set { SettingsManager.appSettings = value; }
    }

    public static void LoadSettings()
    {
        if (appSettings == null)
            appSettings = IsolatedStorageSettings.ApplicationSettings;

        if (!appSettings.Contains(SettingValues.LoadedData))
            appSettings[SettingValues.LoadedData] = false;

        appSettings.Save();
    }

    public static void SaveValue(string key, object value)
    {
        appSettings[key] = value;
        appSettings.Save();
    }
}

然后你可以按如下方式使用它

SettingsManager.SaveValue("myname", someVariableYouWantToStore);

开始后,您可以使用

加载它
SettingsManager.AppSettings["myname"]