您的位置:首页 > 其它

windows phone关于页面跳转的总结

2014-12-19 17:53 281 查看
导航常用的是 在code Behind (.cs 文件)中 this.NavigationService.Navigate(new Uri(...., UriKind.Relative)); 如果在一个类中呢? 没有navigatserver
那么就可以采用 (Application.Current.RootVisual as Microsoft.Phone.Controls.PhoneApplicationFrame).Navigate( new Uri( ..... , UriKind.Relat

(Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/YLGame_SDK;component/FindPW.xaml", UriKind.Relative)); 

在一个wp7应用程序运行的时候,程序的整个UI架构会由会有一个PhoneApplicationFrame和一个或者多个PhoneApplicationPage组成。PhoneApplicationFrame是一个顶级容器,里面容纳了PhoneApplicationPage,一个程序里面只有一个PhoneApplicationFrame,我们在App.xaml.cs里面看到的RootFrame就是当前程序的框架了。本文主要介绍了Windows Phone 7 框架(PhoneApplicationFrame)和页面(PhoneApplicationPage)。

  下面的方法会对RootFrame完成初始化操作

        private void InitializePhoneApplication()

        {

            if (phoneApplicationInitialized)

                return;

            RootFrame = new PhoneApplicationFrame();

            RootFrame.Navigated += CompleteInitializePhoneApplication;

            RootFrame.NavigationFailed += RootFrame_NavigationFailed;

            phoneApplicationInitialized = true;

        }

        private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)

        {

            if (RootVisual != RootFrame)

                RootVisual = RootFrame;

            RootFrame.Navigated -= CompleteInitializePhoneApplication;

        }

 

  关于(PhoneApplicationFrame)和(PhoneApplicationPage)的关系可以用下面的一张图来表示

  


  二、页面(PhoneApplicationPage)的导航

  wp7页面的互相跳转的逻辑是用一个堆栈结构的容器来管理这些页面。如下图

  


  当应用程序中的页面调用 Navigate 时,当前页面会被放到后退堆栈上,并且系统将创建并显示目标页的新实例。当你在应用程序的页面之间进行导航时,系统会将多个条目添加到此堆栈。当页面调用 GoBack 时,或者当用户按手机的“返回”按键时,将放弃当前页面,并将堆栈顶部的页面从后退堆栈中弹出并进行显示。此后退导航会继续弹出并显示,直到堆栈中不再有条目。此时,点按手机的“返回”按键将终止应用程序。

  这个堆栈容器我们是可以通过PhoneApplicationPage出操控的,操控的相关方法和属性如下:

  属性

  BackStack 获取一个 IEnumerable,它用于枚举后退导航历史记录中的条目。

  CanGoBack 获取一个值,该值指示在后退导航历史记录中是否至少存在一个条目。

  CanGoForward 获取一个值,该值指示在前进导航历史记录中是否至少存在一个条目。

  方法

  GoBack 导航到后退导航历史记录中的最新条目;如果后退导航时没有条目,则引发异常。

  GoForward 导航到前进导航历史记录中的最新条目,如果前进导航时没有条目,则引发异常。对于Windows Phone,该方法始终引发异常,因为没有前进导航堆栈。 (从 Frame 继承。)

  RemoveBackEntry 此方法用于从后退堆栈中移除最近的条目,如果没有要移除的条目,则引发InvalidOperationException。如果您想移除多个项目,则多次调用此方法。此 API 是同步的,因此必须从UI 线程调用。

  事件

  Navigated 当已找到导航的内容并且内容可用时发生。 (从 Frame 继承。)

  Navigating 当请求新的导航时发生。 (从 Frame 继承。)

  NavigationFailed 在导航到请求的内容过程中遇到错误时发生。 (从 Frame 继承。)

  NavigationStopped 在通过调用 StopLoading()()()() 方法终止导航时发生,或者在当前导航进行过程中请求新的导航时发生。 (从 Frame 继承。)

  Obscured 当 shell chrome 包含帧时发生。

  OrientationChanged 当 Orientation 属性发生更改时发生。

  三。下面用跟一个Demo来显示一下获取程序的 框架(PhoneApplicationFrame)和页面(PhoneApplicationPage)

  扩展方法类

  Extensions.cs

using System.Windows;

using System.Windows.Media;

using System.Collections.Generic;

using System.Linq;

namespace PageDemo

{

    public static class Extensions

    {

        /// <summary>

        /// 获取该元素的可见树里面所有的子元素

        /// </summary>

        /// <param name="element">可见元素</param>

        public static IEnumerable<DependencyObject> GetVisualDescendants(this
DependencyObject element)

        {

            return GetVisualDescendantsAndSelfIterator(element).Skip(1);

        }

        /// <summary>

        /// 获取该元素的可见树里面所有的子元素以及该元素本身

        /// </summary>

        /// <param name="element">可见元素</param>

        public static IEnumerable<DependencyObject> GetVisualDescendantsAndSelfIterator(DependencyObject
element)

        {

            Queue<DependencyObject> remaining = new Queue<DependencyObject>();

            remaining.Enqueue(element);

            while (remaining.Count > 0)

            {

                DependencyObject obj = remaining.Dequeue();

                yield return obj;

                foreach (DependencyObject child in obj.GetVisualChildren())

                {

                    remaining.Enqueue(child);

                }

            }

        }

        /// <summary>

        /// 获取该元素的可见树里面下一级的子元素

        /// </summary>

        /// <param name="element">可见元素</param>

        public static IEnumerable<DependencyObject> GetVisualChildren(this
DependencyObject element)

        {

            return GetVisualChildrenAndSelfIterator(element).Skip(1);

        }

        /// <summary>

        /// 获取该元素的可见树里面下一级的子元素以及该元素本身

        /// </summary>

        /// <param name="element">可见元素</param>

        public static IEnumerable<DependencyObject> GetVisualChildrenAndSelfIterator(this
DependencyObject element)

        {

            yield return element;

            int count = VisualTreeHelper.GetChildrenCount(element);

            for (int i = 0;
i < count; i++)

            {

                yield return VisualTreeHelper.GetChild(element, i);

            }

        }

    }

}

 

  测试获取程序页面类

  Test.cs

using System.Windows;

using Microsoft.Phone.Controls;

using System.Linq;

using System.Collections.Generic;

namespace PageDemo

{

    public class Test

    {

        /// <summary>

        /// 获取当前的程序展示的页面

        /// </summary>

        public static PhoneApplicationPage Page

        {

            get { return (Application.Current.RootVisual as PhoneApplicationFrame).GetVisualDescendants().OfType<PhoneApplicationPage>().FirstOrDefault();
}

        }

        /// <summary>

        /// 获取所有的框架下的页面

        /// </summary>

        public static List<PhoneApplicationPage> Pages

        {

            get { return (Application.Current.RootVisual as PhoneApplicationFrame).GetVisualDescendants().OfType<PhoneApplicationPage>().ToList<PhoneApplicationPage>();
}

        }

        /// <summary>

        /// 获取程序所有的UI元素

        /// </summary>

        public static List<DependencyObject> Elements

        {

            get { return (Application.Current.RootVisual as PhoneApplicationFrame).GetVisualDescendants().ToList<DependencyObject>();
}

        }

    }

}

  <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

            <Button Content="Button" Height="72" HorizontalAlignment="Left" Margin="139,176,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />

        </Grid>

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            if (Test.Page != null)

            {

                MessageBox.Show(Test.Page.ToString());

            }

        }

 

  单击的效果

  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  windows phone 8