当前位置:首 页 > 攻城湿 >Windows Phone > 查看文章

Windows Phone 7框架和页面开发概述

Windows Phone 你是第2154个围观者 1条评论 供稿者: 标签:, , ,

在一个Windows Phone应用程序运行的时候,程序的整个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)的关系可以用下面的一张图来表示

2012032820452366
  
页面(PhoneApplicationPage)的导航

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

IC531004
  
当应用程序中的页面调用 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"/>可见元素
        public static IEnumerable<dependencyobject> GetVisualDescendants(this DependencyObject element)
        {
            return GetVisualDescendantsAndSelfIterator(element).Skip(1);
        }
        /// <summary>
        /// 获取该元素的可见树里面所有的子元素以及该元素本身
        /// </summary>
        /// <param name="element"/>可见元素
        public static IEnumerable</dependencyobject><dependencyobject> GetVisualDescendantsAndSelfIterator(DependencyObject element)
        {
            Queue</dependencyobject><dependencyobject> remaining = new Queue</dependencyobject><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"/>可见元素
        public static IEnumerable</dependencyobject><dependencyobject> GetVisualChildren(this DependencyObject element)
        {
            return GetVisualChildrenAndSelfIterator(element).Skip(1);
        }
        /// <summary>
        /// 获取该元素的可见树里面下一级的子元素以及该元素本身
        /// </summary>
        /// <param name="element"/>可见元素
        public static IEnumerable</dependencyobject><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><phoneapplicationpage> Pages
        {
            get { return (Application.Current.RootVisual as PhoneApplicationFrame).GetVisualDescendants().OfType</phoneapplicationpage><phoneapplicationpage>().ToList</phoneapplicationpage><phoneapplicationpage>(); }
        }
        /// <summary>
        /// 获取程序所有的UI元素
        /// </summary>
        public static List<dependencyobject> Elements
        {
            get { return (Application.Current.RootVisual as PhoneApplicationFrame).GetVisualDescendants().ToList</dependencyobject><dependencyobject>(); }
        }
    }
}
</dependencyobject></phoneapplicationpage>
<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"></button>
</grid>
private void button1_Click(object sender, RoutedEventArgs e)
{
    if (Test.Page != null)
    {
        MessageBox.Show(Test.Page.ToString());
    }
}

单击的效果
2012032900182690

这家伙很懒,什么都没写!

—— zhaorong

zhaorong
你可能也喜欢Related Posts
众说纷纭Comments
大眼 可爱 大笑 坏笑 害羞 发怒 折磨 快哭了 大哭 白眼 晕 流汗 困 腼腆 惊讶 憨笑 色 得意 骷髅 囧 睡觉 眨眼 亲亲 疑问 闭嘴 难过 淡定 抗议 鄙视 猪头
小提示:直接粘贴图片到输入框试试
努力发送中...
  1. 1 楼 访客

    昨天和老婆在机场候机,凑一起亲密。 老婆伸出手,我亲了一下,特么的她大笑,说上厕所没洗手! 当时哥们想也没想,抱老婆一顿亲,大喊,哈哈,爷吃大便没有洗嘴! 当时一阵寒风吹过,机场一片寂静!我恨不得找个地方钻下去!你该吃药了

    2014年01月17日 06:40:44 回复 取消回复
  • 评论最多
  • 最新评论
  • 随机文章
footer logo
未经许可请勿自行使用、转载、修改、复制、发行、出售、发表或以其它方式利用本网站之内容
Copyright © zhaorong All Rights Reserved. 滇ICP备15006105号-1