因为用户控件页面不支持
- NavigationService.Navigate(new Uri("/page2.xaml", UriKind.Relative))
所以从UserControl页面导航到PhonePage页面,需要用到RootVisual根视图容器里的方法来实现页面跳转:
- private void OnControl1ButtonClick(object sender, RoutedEventArgs e)
- {
- PhoneApplicationFrame rootFrame = Application.Current.RootVisual as PhoneApplicationFrame;
- Uri almanacPageUri = new Uri("/page2.xaml", UriKind.Relative);
- rootFrame.Navigate(almanacPageUri);
- }
在UCPage1中:
- private void OnControl1ButtonClick(object sender, RoutedEventArgs e)
- {
- PhoneApplicationFrame rootFrame = Application.Current.RootVisual as PhoneApplicationFrame;
- rootFrame.Navigated -= OnNavigated;
- rootFrame.Navigated += OnNavigated;
- Uri pageUri = new Uri("/page2.xaml", UriKind.Relative);
- rootFrame.Navigate(pageUri);
- }
- private void OnNavigated(object sender, NavigationEventArgs e)
- {
- Page2 page2 = e.Content as Page2;
- if (null != page2)
- {
- // SelectionChanged是在Page2中定义的委托事件,并在UCPage1中注册,
- // 这样当SelectionChanged在page2中被触发时,page1便可以截获这个事件并进行处理
- page2.SelectionChanged += OnSelectionChanged;
- }
- }
- /// <summary>
- /// 处理关于SelectionChanged事件的方法
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e">SelectionChangedEventArgs可以是自定义的参数类型,里面包含了要传递的信息</param>
- private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- m_AlmanacSelected = e.SelectedAlmanac;
- //TODO:这里实现具体的处理逻辑
- }
在Page2中:
- // 定义委托事件SelectionChanged,参数类型为SelectionChangedEventArgs(该参数可以自定义)
- public event EventHandler<SelectionChangedEventArgs> SelectionChanged;
- /// <summary>
- /// 完成并返回
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="args"></param>
- private void OnDoneButtonClick(object sender, EventArgs args)
- {
- SelectionChanged(sender, new SelectionChangedEventArgs(m_SelectedItem));
- NavigationService.GoBack();
- }