前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >silverlight中如何方便在多个"场景"即Xaml文件之间随意切换?

silverlight中如何方便在多个"场景"即Xaml文件之间随意切换?

作者头像
菩提树下的杨过
发布2018-01-23 10:19:05
6110
发布2018-01-23 10:19:05
举报

注:以下方法是百度上搜索得来的,整理一下转发于此

步骤1.先在silverlight项目中新建一个接口文件IContent.cs,内容如下(namespace请各位根据自己的实际情况修改):

Code

代码语言:js
复制
using System.Windows;

namespace BookStore
{
 public interface IContent
    {
        UIElement Content { get; set; }
    }
}

步骤2.建二个Xaml文件Test.xaml和Test2.Xaml

Test.Xaml完整内容如下:

Code

代码语言:js
复制
<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完整内容如下:

Code

代码语言:js
复制
using 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

代码语言:js
复制
<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几乎一样)

Code

代码语言:js
复制
using 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;
            }
        } 
    }
}

运行效果图如下:

欢迎转载,但请注明来自"菩提树下的杨过"

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2009-02-06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档