我无法从用户控件中调用导航服务.
即使我在主页面上创建一个事件处理程序来调用老挝无效的导航服务.
即使我在主页面上创建一个事件处理程序来调用老挝无效的导航服务.
你能帮我么?
解决方法
我想我已经看到了这个问题,但就像Austin所说的那样,你的初步描述并没有多少进展.听起来您正试图从放置在该页面上的UserControl中访问NavgationService(这是一个
PhoneApplicationPage属性).
与这些API中的许多内容一样,您有几个选择.首先,您可以访问PhoneApplicationFrame(包含您的页面并管理导航)并将其用于导航:
var frame = App.Current.RootVisual as PhoneApplicationFrame; frame.Navigate(new Uri("/TargetPage.xaml",UriKind.Relative));
或者,您可以使用VisualTreeHelper遍历控件的可视树,直到到达包含页面:
var page = GetParentOfType<PhoneApplicationPage>(this); // this is your user control private static T GetParentOfType<T>(DependencyObject item) where T : DependencyObject { if (item == null) throw new ArgumentNullException("item"); T result; var parent = VisualTreeHelper.GetParent(item); if (parent == null) return null; else if (parent.GetType().IsSubclassOf(typeof(T)) { result = (T)parent; } else result = GetParameterOfType<T>(parent); return result; }
如您所见,VisualTree方法涉及更多代码,但可以获取包含页面对象,您可以在其中访问NavigationContext等内容.
希望这是你的问题(和你的答案.)