Windows 8 C#/XAML SuspensionManager failed

本文关键字:SuspensionManager failed XAML Windows | 更新日期: 2023-09-27 18:31:53

我正在为一个图像板编写简单的图像查看器。我在我的应用程序中使用以下 2 个类进行导航(Frame.Navigate()方法的导航参数):

public class KonaParameter
{
    public int page { get; set; }
    public string tags { get; set; }
    public KonaParameter()
    {
        page = 1;
    }
}
public class Post
{
    public int id { get; set; }
    public string tags { get; set; }
    public int created_at { get; set; }
    public int creator_id { get; set; }
    public string author { get; set; }
    public int change { get; set; }
    public string source { get; set; }
    public int score { get; set; }
    public string md5 { get; set; }
    public int file_size { get; set; }
    public string file_url { get; set; }
    public bool is_shown_in_index { get; set; }
    public string preview_url { get; set; }
    public int preview_width { get; set; }
    public int preview_height { get; set; }
    public int actual_preview_width { get; set; }
    public int actual_preview_height { get; set; }
    public string sample_url { get; set; }
    public int sample_width { get; set; }
    public int sample_height { get; set; }
    public int sample_file_size { get; set; }
    public string jpeg_url { get; set; }
    public int jpeg_width { get; set; }
    public int jpeg_height { get; set; }
    public int jpeg_file_size { get; set; }
    public string rating { get; set; }
    public bool has_children { get; set; }
    public object parent_id { get; set; }
    public string status { get; set; }
    public int width { get; set; }
    public int height { get; set; }
    public bool is_held { get; set; }
    public string frames_pending_string { get; set; }
    public List<object> frames_pending { get; set; }
    public string frames_string { get; set; }
    public List<object> frames { get; set; }
    public object flag_detail { get; set; }
}

我面临的问题是暂停不起作用。暂停管理器在调用后抛出"暂停管理器失败"异常await SuspensionManager.SaveAsync();(我用谷歌搜索这是因为使用了复杂类型)。

我尝试使用字符串作为导航参数。它可以工作,但我的参数需要超过 1 个字符串(List<string>不起作用,我尝试使用它)。

如何正确挂起我的应用程序?

Windows 8 C#/XAML SuspensionManager failed

问题是SuspensionManager使用Frame.GetNavigationState()来获取Frame的历史记录。然后,它尝试将导航历史记录序列化为字符串,不幸的是,它无法知道如何将自定义复杂类型序列化为 Post 或 KonaParameter 等参数,并且失败并出现异常。

如果你想使用SuspensionManager,那么你需要将自己限制为简单的类型,如int或string作为参数。

如果您确实需要复杂类型,那么最好将它们存储在带有标识符的某个后台服务/存储库中。然后,可以将标识符作为参数传递。

推荐的方法是序列化"state"对象,以便将其保存/还原为字符串。您可以使用 Json.NET 来执行此操作:

//save state
Post post = //...;
string state = JsonConvert.Serialize(post)
//restore state
Post post = JsonConvert.Deserialize<Post>(state);

如果你想使用两个对象(一个 Post 和一个 KonaParameter),我会考虑创建一个封装这两个对象的"聚合"类,并对其进行序列化/反序列化:

public class State
{
    public Post post {get; set;}
    public KonaParameter param {get; set;}
}
相关文章:
  • 没有找到相关文章