Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1046156
  • 博文数量: 403
  • 博客积分: 10272
  • 博客等级: 上将
  • 技术积分: 4407
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 14:22
文章分类

全部博文(403)

文章存档

2012年(403)

分类: 嵌入式

2012-03-29 20:59:39

一、Windows Phone 7 框架(PhoneApplicationFrame)和页面(PhoneApplicationPage)

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

下面的方法会对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页面的互相跳转的逻辑是用一个堆栈结构的容器来管理这些页面。如下图

BackStack 堆栈表示

当应用程序中的页面调用 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
{
///
/// 获取该元素的可见树里面所有的子元素
///

/// 可见元素
public static IEnumerable GetVisualDescendants(this DependencyObject element)
{
return GetVisualDescendantsAndSelfIterator(element).Skip(1);
}
///
/// 获取该元素的可见树里面所有的子元素以及该元素本身
///

/// 可见元素
public static IEnumerable GetVisualDescendantsAndSelfIterator(DependencyObject element)
{
Queue remaining = new Queue();
remaining.Enqueue(element);

while (remaining.Count > 0)
{
DependencyObject obj = remaining.Dequeue();
yield return obj;

foreach (DependencyObject child in obj.GetVisualChildren())
{
remaining.Enqueue(child);
}
}
}
///
/// 获取该元素的可见树里面下一级的子元素
///

/// 可见元素
public static IEnumerable GetVisualChildren(this DependencyObject element)
{
return GetVisualChildrenAndSelfIterator(element).Skip(1);
}
///
/// 获取该元素的可见树里面下一级的子元素以及该元素本身
///

/// 可见元素
public static IEnumerable 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
{
///
/// 获取当前的程序展示的页面
///

public static PhoneApplicationPage Page
{
get { return (Application.Current.RootVisual as PhoneApplicationFrame).GetVisualDescendants().OfType().FirstOrDefault(); }
}
///
/// 获取所有的框架下的页面
///

public static List Pages
{
get { return (Application.Current.RootVisual as PhoneApplicationFrame).GetVisualDescendants().OfType().ToList(); }
}
///
/// 获取程序所有的UI元素
///

public static List Elements
{
get { return (Application.Current.RootVisual as PhoneApplicationFrame).GetVisualDescendants().ToList(); }
}
}
}
<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());
}
}

单击的效果

欢迎购买我的书籍 感谢大家的支持!!

阅读(1339) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~