silverlight中如何方便在多个场景即Xaml文件之间随意切换?
生活随笔
收集整理的這篇文章主要介紹了
silverlight中如何方便在多个场景即Xaml文件之间随意切换?
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
注:以下方法是百度上搜索得來的,整理一下轉發于此
步驟1.先在silverlight項目中新建一個接口文件IContent.cs,內容如下(namespace請各位根據自己的實際情況修改):
Codeusing?System.Windows;
namespace?BookStore
{
????public?interface?IContent
????{
????????UIElement?Content?{?get;?set;?}
????}
}
?
步驟2.建二個Xaml文件Test.xaml和Test2.Xaml
Test.Xaml完整內容如下:
Code<UserControl?x:Class="BookStore.Test"
????xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"?
????xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"?
????Width="600"?Height="400">
????<Grid?x:Name="LayoutRoot"?Background="White"?>
????????<StackPanel?Orientation="Vertical"?HorizontalAlignment="Center"?Margin="50"?Background="AliceBlue"?Width="200"?Height="100">
????????????<TextBlock?TextAlignment="Center">
????????????????這是Test.Xaml文件
????????????</TextBlock>
????????????<Button?Height="25"?Width="150"?Content="轉到Test2.xaml"?Click="Button_Click"></Button>
????????</StackPanel>
???????
????</Grid>
</UserControl>
?
Test.Xaml.Cs完整內容如下:
Codeusing?System.Windows;
using?System.Windows.Controls;
namespace?BookStore
{
????//手動增加,?IContent?,讓Test實現IContent接口
????public?partial?class?Test?:?UserControl,?IContent?
????{
????????public?Test()
????????{
????????????InitializeComponent();
????????}
????????private?void?Button_Click(object?sender,?RoutedEventArgs?e)
????????{
????????????//實現切換(點擊test.xaml上的按鈕將切換到Test2"場景")
????????????(Application.Current.RootVisual?as?IContent).Content?=?new?Test2();???????????????????????
????????}
????????///?<summary>
????????///?增加一個Content屬性
????????///?</summary>
????????public?new?UIElement?Content
????????{
????????????get
????????????{
????????????????return?base.Content;
????????????}
????????????set
????????????{
????????????????base.Content?=?value;
????????????}
????????}?
????}
}
?
Test2.Xaml完整內容如下:
Code<UserControl?x:Class="BookStore.Test2"
????xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"?
????xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"?
????Width="600"?Height="400">
????<Grid?x:Name="LayoutRoot"?Background="White"?>
????????<StackPanel?Orientation="Vertical"?HorizontalAlignment="Center"?Margin="50"?Background="Beige"??Width="200"?Height="100">
????????????<TextBlock?TextAlignment="Center">
????????????????這是Test2.Xaml文件
????????????</TextBlock>
????????????<Button?Height="25"?Width="150"?Content="轉到Test.xaml"?Click="Button_Click"></Button>
????????</StackPanel>
????</Grid>
</UserControl>
?
Test2.Xaml.cs完整內容如下:(其實跟Test.Xaml.cs幾乎一樣)
Codeusing?System.Windows;
using?System.Windows.Controls;
namespace?BookStore
{
????//手動增加,?IContent?,讓Test2實現IContent接口
????public?partial?class?Test2?:?UserControl,?IContent
????{
????????public?Test2()
????????{
????????????InitializeComponent();
????????}
????????private?void?Button_Click(object?sender,?RoutedEventArgs?e)
????????{
????????//就這一行有點一不樣(點擊test2.xaml上的按鈕將還回到Test"場景")
????????????(Application.Current.RootVisual?as?IContent).Content?=?new?Test();????????????
????????}
????????///?<summary>
????????///?增加一個Content屬性
????????///?</summary>
????????public?new?UIElement?Content
????????{
????????????get
????????????{
????????????????return?base.Content;
????????????}
????????????set
????????????{
????????????????base.Content?=?value;
????????????}
????????}?
????}
}
?
運行效果圖如下:
歡迎轉載,但請注明來自"菩提樹下的楊過"
轉載于:https://www.cnblogs.com/yjmyzz/archive/2009/02/06/1385189.html
總結
以上是生活随笔為你收集整理的silverlight中如何方便在多个场景即Xaml文件之间随意切换?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 山寨版 颈椎病治疗秘籍
- 下一篇: 椭圆长轴是a还是2a