UWP Template10 Frame.Navigate与参数导致错误 [英] UWP Template10 Frame.Navigate with parameter causes Error

查看:257
本文介绍了UWP Template10 Frame.Navigate与参数导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当时我正在使用Intense Templates(SplitView示例)开发UWP项目,然后我切换到Template10(Hamburger).当我尝试传递参数

Was developing UWP project by using Intense Templates(SplitView sample) then I switched to Template10(Hamburger). And seems there is error when I try to pass parameter

param is string with value something like this - (d5d05a3b-546a-4ca6-a345-74e333fa00fb) Frame.Navigate(typeof(EditPage),param);

param is string with value something like this - (d5d05a3b-546a-4ca6-a345-74e333fa00fb) Frame.Navigate(typeof(EditPage),param);

错误是这样的: ErrorPicture 然后,如果我的编辑"页面不能正确设置 param ,它将返回用户(Frame.GoBack()),现在我遇到了

Error is something like this: ErrorPicture And then if my Edit page cant get right param, It will send user back (Frame.GoBack()), now I'm getting error like

An exception of type 'System.Runtime.InteropServices.COMException' occurred in App6.exe but was not handled in user code
Additional information: Error HRESULT E_FAIL has been returned from a call to a COM component.

推荐答案

问题的根本原因是,如果我要猜测(并基于异常字符串)是您要向页面传递一种复杂的类型,而该类型不能被序列化.模板10想要序列化参数的原因是,这样它可以在您的应用暂停后正确恢复导航状态.我意识到如果您必须传递一个不可序列化的对象,这会带来不便,那么您会发现BootStrapper.SessionState(一个Dictionary)允许您保存任何类型的对象,只需要将密钥传递给可以使用SessionState +的页面即可该键以检索对象.

the root cause of your problem, if I were to guess (and based on the exception string) is that you are passing to your page a complex type that cannot be serialized. The reason Template 10 wants to serialize your parameter is so it can correctly restore your navigation state after your app is suspended. I realize this is inconvenient if you MUST pass a non serializable object, and so you will discover BootStrapper.SessionState (a Dictionary) allows you do hold objects of any kind, requiring you to only pass the key to the page that can use SessionState + that key to retrieve the object.

顺便说一句,这是一个很好的模式,当然不是模板10所独有的.我要添加的唯一内容是,假定字典中缺少该对象,并包含防御性代码来处理这种情况.也.原因是,如果您的应用已被暂停,则可能会发生这种情况.

This is a great pattern, by the way, and certainly not one unique to Template 10. The only thing I would add to this would be you assume the object is missing from the dictionary and include the defensive code to handle that situation too. The reason is, this situation could occur if your app has been suspended.

我希望这是有道理的.而且,不要感到难过.您当然不是第一个提出这个问题的人.

I hope this makes sense. And, don't feel bad. You are certainly not the first to ask this question.

var key = nameof(MyObject);
SessionState.Add(key, MyObject);
NavigationService.Navigate(typeof(Views.MainPage), key);

这篇关于UWP Template10 Frame.Navigate与参数导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆