【问题标题】:Where to SaveState on App Suspension应用程序暂停时保存状态的位置
【发布时间】:2014-07-23 03:39:42
【问题描述】:

(注意:以下指的是适用于 Windows Phone 8.1 的 Hub 应用程序)

当 Windows Phone 挂起时,所有未保存的数据都会丢失。添加/更改数据时是否应保存数据?还是应该在暂停时保存?还是两者兼而有之?

具体来说:

需要永久维护的重要数据,我认为保存它没有问题。但如果它只是需要几秒钟的数据,除非应用程序暂停,否则保存它似乎有点过头了。

用一种方法一次保存所有内容似乎也更简洁,但这可能只是我个人的偏好。

如果数据应该在暂停时保存,它在以下方法中的位置是否重要? (这在OnSuspending() 方法中调用。)

    public static async Task SaveAsync()
    {
        try
        {
            // Save the navigation state for all registered frames
            foreach (var weakFrameReference in _registeredFrames)
            {
                Frame frame;
                if (weakFrameReference.TryGetTarget(out frame))
                {
                    SaveFrameNavigationState(frame);
                }
            }

            // Serialize the session state synchronously to avoid asynchronous access to shared
            // state
            MemoryStream sessionData = new MemoryStream();
            DataContractSerializer serializer = new DataContractSerializer(typeof(Dictionary<string, object>), _knownTypes);
            serializer.WriteObject(sessionData, _sessionState);

            // Get an output stream for the SessionState file and write the state asynchronously
            StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(sessionStateFilename, CreationCollisionOption.ReplaceExisting);
            using (Stream fileStream = await file.OpenStreamForWriteAsync())
            {
                sessionData.Seek(0, SeekOrigin.Begin);
                await sessionData.CopyToAsync(fileStream);
            }
        }
        catch (Exception e)
        {
            throw new SuspensionManagerException(e);
        }
    }

还有一个附带的问题,恢复数据应该只是在NavigationHelper_LoadState() 方法中预先加载,对吧?

【问题讨论】:

    标签: c# windows-phone-8.1 savestate suspend


    【解决方案1】:

    如何处理您的数据取决于几个因素:

    • 您的数据有多重要 - 如果您有一些重要数据,请立即保存,
    • 您的数据有多大 - 如果您的数据非常大,那么在应用暂停时可能无法保存 - 时间有限,
    • 想想你是否能够在恢复后重现数据 - 如果你有一些在应用程序内计算的数据,并且你可以在恢复后重现该过程,那么可能不需要保存数据。

    如果有几个地方可以保存 - 设置(本地/漫游),本地存储(文件/数据库),还有缓存。

    请注意,NavigationHelper 包含一个帮助方法,您可以使用它来保存/恢复您的数据:

    // in your Page
    private void NavigationHelper_SaveState(object sender, SaveStateEventArgs e)
    {
        e.PageState.Add("yourKey", data);
    }
    

    它的工作方式类似于 Dictionary,它被序列化为文件 sessionStateFilename

    您也可以手动对文件执行此操作 - 这将非常相似。

    【讨论】:

      【解决方案2】:

      保存方法的位置重要吗?

      • 您只能信任“Window.Current.Activated”

        AddHandler Window.Current.Activated, AddressOf WindowActivated
        
        Private Sub WindowActivated(sender As Object, e As Windows.UI.Core.WindowActivatedEventArgs)
          If e.WindowActivationState = Windows.UI.Core.CoreWindowActivationState.Deactivated Then
            If TypeOf rootFrame.Content Is MyPage Then
                Dim MyPage1 = DirectCast(rootFrame.Content, MyPage)
                MyPage1.SaveAsync()
            End If
          End If
        End Sub
        

      在一个方法中一次保存所有内容似乎更简洁。

      • 这也是我个人的喜好。

      是否应该在 NavigationHelper_LoadState 中恢复数据?

      • 我更喜欢 App.OnLaunched

      【讨论】:

        猜你喜欢
        • 2016-04-27
        • 1970-01-01
        • 1970-01-01
        • 2013-05-11
        • 2015-05-01
        • 1970-01-01
        • 2011-08-14
        • 1970-01-01
        • 2014-11-24
        相关资源
        最近更新 更多