For a tip, see
Http://tech.sina.com.cn/s/2008-07-03/1528718607.shtml
Or
Http://kb.cnblogs.com/page/42897? Page = 1
We will discuss how to use initparams inSilverlight 2 ApplicationProgramSwitch User ControlThis is a stupid but intuitive solution.
But what if the value is transmitted in the control? The above method will be useless!
Today, I saw a clever method on a foreigner's blog, and I am afraid to enjoy it exclusively. I will share it with you:
First, write an interface:
Namespace pageswitchsimple
{
Public interface iswitchable
{
Void utilizestate (Object State );
}
}
Then write a switch class:
Using system. Windows. controls;
Namespace pageswitchsimple
{
Public static class switcher
{
Public static pageswitcher;
Public static void switch (usercontrol Newpage)
{
Pageswitcher. navigate (Newpage );
}
Public static void switch (usercontrol Newpage, object state)
{
Pageswitcher. navigate (Newpage, State );
}
}
}
Pageswitcher member class:
Front-endCode:
<Usercontrol X: class = "pageswitchsimple. pageswitcher"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Width = "800" Height = "600">
</Usercontrol>
Background code:
Using system;
Using system. Windows. controls;
Namespace pageswitchsimple
{
Public partial class pageswitcher: usercontrol
{
Public pageswitcher ()
{
Initializecomponent ();
}
Public void navigate (usercontrol nextpage)
{
This. content = nextpage;
}
Public void navigate (usercontrol nextpage, object state)
{
This. content = nextpage;
Iswitchable S = nextpage as iswitchable;
// This is really clever. It is used to pass object parameters!
If (s! = NULL)
{
S. utilizestate (State );
}
Else
{
Throw new argumentexception ("nextpage is not iswitchable! "
+ Nextpage. Name. tostring ());
}
}
}
}
Then write two user controls that implement the iswitchable interface:
Control 1:
<Usercontrol X: class = "pageswitchsimple. Page"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Width = "400" Height = "300">
<Grid X: Name = "layoutroot" background = "white">
<Textblock text = "Your name:" fontsize = "18"/>
<Textbox X: Name = "name" fontsize = "18" width = "150" Height = "35" verticalignment = "TOP" margin = "5"/>
<Button X: Name = "changepage" content = "change" fontsize = "18" width = "100" Height = "50"/>
</GRID>
</Usercontrol>
Background code:
Using system. windows;
Using system. Windows. controls;
Namespace pageswitchsimple
{
Public partial class page: usercontrol, iswitchable
{
Public page ()
{
Initializecomponent ();
Changepage. Click + = new routedeventhandler (changepage_click );
}
Void changepage_click (Object sender, routedeventargs E)
{
Switcher. Switch (New page2 (), name. Text );
}
}
}
Control 2:
<Usercontrol X: class = "pageswitchsimple. page2"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Width = "400" Height = "300">
<Grid X: Name = "layoutroot" background = "bisque">
<Textblock X: Name = "message" text = "page2" fontsize = "18"/>
<Button X: Name = "changepage" content = "change" fontsize = "18" width = "100" Height = "50"/>
</GRID>
</Usercontrol>
Background code:
Using system. windows;
Using system. Windows. controls;
Namespace pageswitchsimple
{
Public partial class page2: usercontrol, iswitchable
{
Public page2 ()
{
Initializecomponent ();
Changepage. Click + = new routedeventhandler (changepage_click );
}
Public void utilizestate (object state)
{
Message. Text = state. tostring ();
}
Void changepage_click (Object sender, routedeventargs E)
{
Switcher. Switch (New Page ());
}
}
}
Last modified app. CS
Private void application_startup (Object sender, startupeventargs E)
{
Pageswitcher = new pageswitcher ();
This. rootvisual = pageswitcher;
Switcher. pageswitcher = pageswitcher;
Switcher. Switch (New Page ());
}
Cleverly uses the Observer Model to admire the author's ideas.