I. Windows Phone 7 framework (Phoneapplicationframe) And page (Phoneapplicationpage)
In a WP7 ApplicationProgramWhen running, the entire UI architecture of the program will bePhoneapplicationframe and one or morePhoneapplicationpage.Phoneapplicationframe is a top-level container containingPhoneapplicationpage, which contains only one programPhoneapplicationframe. The rootframe we see in APP. XAML. CS is the framework of the current program.
The following method initializes the 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;
}
About (Phoneapplicationframe) And (The relationship of phoneapplicationpage can be represented by the following figure.
Ii. Page (Phoneapplicationpage)
The logic for redirecting between WP7 pages is to use a stack-structured container to manage these pages. For example
When the page in the application callsNavigateThe current page will be placed on the back stack, and the system will create and display the new instance on the target page. When you navigate between pages of an application, the system adds multiple entries to this stack. When the page is calledGobackOr when the user presses"Back"When you press the button, the current page is discarded and the page at the top of the stack pops up from the back stack for display. The back navigation will continue to pop up and display until there are no entries in the stack. Click"Back"Press the key to terminate the application.
We can use this stack containerPhoneapplicationpage Output Control, The related methods and attributes of the operation are as follows:
Attribute
Backstack obtains an ienumerable that is used to enumerate entries in the back navigation history.
Cangoback gets a value indicating whether at least one entry exists in the back navigation history.
Cangoforward gets a value indicating whether at least one entry exists in the forward navigation history.
Method
Goback navigate to the latest entry in the back navigation history. If there are no entries in the back navigation history, an exception is thrown.
Goforward navigate to the latest entry in the forward navigation history. If there are no entries in the forward navigation, an exception is thrown. For Windows Phone, this method always raises an exception because there is no Forward Navigation stack. (Inherited from frame .)
Removebackentry this method is used to remove the most recent entry from the back stack. If no entry is to be removed, invalidoperationexception is thrown. If you want to remove multiple projects, call this method multiple times. This API is synchronous and must be called from the UI thread.
Event
Navigated occurs when the navigation content is found and the content is available. (Inherited from frame .)
Navigating occurs when a new navigation is requested. (Inherited from frame .)
Navigationfailed encounters an error while navigating to the requested content. (Inherited from frame .)
Navigationstopped occurs when the navigation is terminated by calling the stoploading () method or when a new navigation is requested during the current navigation. (Inherited from frame .)
Obscured occurs when shell chrome contains frames.
Orientationchanged occurs when the orientation attribute is changed.
3. The following uses a demo to show the framework for obtaining a program (Phoneapplicationframe) And page (Phoneapplicationpage)
Extended method class
Extensions. CS
Using System. windows;
Using System. Windows. Media;
Using System. Collections. Generic;
Using System. LINQ;
Namespace Pagedemo
{
Public Static Class Extensions
{
/// <Summary>
/// Obtains all the child elements in the visible tree of this element.
/// </Summary>
/// <Param name = "element"> Visible Element </Param>
Public Static Ienumerable <dependencyobject> getvisualdescendants ( This Dependencyobject element)
{
Return Getvisualdescendantsandselfiterator (element). Skip ( 1 );
}
/// <Summary>
/// Obtain all the child elements in the visible tree of the element and the element itself.
/// </Summary>
/// <Param name = "element"> Visible 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>
/// Obtain the sub-elements at the lower level in the visible tree of this element.
/// </Summary>
/// <Param name = "element"> Visible Element </Param>
Public Static Ienumerable <dependencyobject> getvisualchildren ( This Dependencyobject element)
{
Return Getvisualchildrenandselfiterator (element). Skip ( 1 );
}
/// <Summary>
/// Obtain the sub-elements in the visible tree of the element and the element itself.
/// </Summary>
/// <Param name = "element"> Visible 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 and obtain the program page class
Test. CS
Using System. windows;
Using Microsoft. Phone. controls;
Using System. LINQ;
Using System. Collections. Generic;
Namespace Pagedemo
{
Public Class Test
{
/// <Summary>
/// Obtain the display page of the current program
/// </Summary>
Public Static Phoneapplicationpage
{
Get { Return (Application. Current. rootvisual As Phoneapplicationframe). getvisualdescendants (). oftype <phoneapplicationpage> (). firstordefault ();}
}
/// <Summary>
/// Retrieve pages under all frameworks
/// </Summary>
Public Static List <phoneapplicationpage> pages
{
Get {Return (Application. Current. rootvisual As Phoneapplicationframe). getvisualdescendants (). oftype <phoneapplicationpage> (). tolist <phoneapplicationpage> ();}
}
/// <Summary>
/// Obtains all the UI elements of a program.
/// </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" Name = "Button1" Verticalalignment = "TOP" Width = "160" Click = "Button#click" />
</ Grid >
Private VoidButton#click (ObjectSender, routedeventargs E)
{
If(Test. Page! =Null)
{
MessageBox. Show (test. Page. tostring ());
}
}
Click Effect