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

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

Code
using 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完整内容如下:

Code
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
<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
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;
            }
        } 
    }
}

运行效果图如下:


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

转载于:https://www.cnblogs.com/yjmyzz/archive/2009/02/06/1385189.html

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

  1. Unity中自带的第一人称与第三人称视角之间的切换问题

    第一人称[_Characters],第三人称主角[_3rdPersonViewpoint]: 第一人称视角与第三人称的差别在于鼠标旋转,经过测试在第三人称切换至第一人称时只要将3rd的localpos ...

  2. Silverlight中服务通信方式的选择(WCF、Data Service、Ria Service)

    Silverlight中服务通信方式的选择(WCF.Data Service.Ria Service) 转自 http://www.cnblogs.com/024hi/archive/2011/06/ ...

  3. 一步一步学动画[1]:Silverlight中Animation的应用

    1.Animation简介 动画是快速播放一系列图像(其中每个图像与下一个图像略微不同)给人造成的一种幻觉.大脑感觉这组图像是一个变化的场景.在电影中,摄像机每秒钟拍摄许多照片(帧),便可使人形成这种 ...

  4. 在Silverlight中进行图片下载

    本篇 QuickStart 示例演示如何使用 Microsoft .NET Framework for Silverlight来下载一个图片内容,并将其嵌入到XAML Image object中.这个 ...

  5. Silverlight游戏设计(Game Design):(二)场景编辑器让游戏开发更美好

    如果哪天光荣告诉我:<三国志>系列将终结,我会义无返顾的用余下那点青春继续诠释这部中国历史经典题材游戏,已无法细数它占据了我多少童年的回忆,就好比曾有那么一群满腔热血的<梦幻模拟战& ...

  6. Silverlight中OneTime,OneWay,TwoWay及INotifyPropertyChanged 接口的理解

    今天有时间把Silverlight中OneTime,OneWay,TwoWay及INotifyPropertyChanged 接口的理解等数据绑定方面的东西理解学习了下!下面是我的笔记. (一)前台代 ...

  7. Silverlight中文件的生成操作与其对应的获取方法

    文件生成操作: Silverlight里的资源文件(图片.视频.字体.XML.XAML等) 生成操作属性选择不同选项时,文件的生成方式和存储位置会有相应变化,下面说一下几个常用的选项: 1. Page ...

  8. Silverlight中使用CompositionInitializer宿主MEF

    MEF可以在传统应用程序中使用(包括桌面的Winform.控制台程序和Web的ASP.NET),也可以在RIA的Silverlight中使用.在Silverlight中只是宿主的方式有所不同,实际上在 ...

  9. 机器学习中的方法技术与应用场景

    https://www.toutiao.com/a6646374880138756615/ 2019-01-15 05:31:00 关于机器学习我们到底能做什么?怎么做?有哪些方法和技术?下面的一些解 ...

最新文章

  1. mysql中3张表如何关联查询_mysql三张表关联查询
  2. JDK5.0新特性系列---目录
  3. Rushcrm:客户关系管理适合的才是好的
  4. python网上编程课程-什么是Python编程课程
  5. spring 概念理解(资料)
  6. 一个宝妈如何完成逆袭做到月入5万+,单品利润破20万的
  7. 提高电脑反应速度_如何组装一台4000元左右的台式电脑?
  8. 为什么每次开机第一次启动程序会很慢?
  9. 不可能不爱的 XCODE 9:最新功能详尽介绍
  10. 解决Win10磁盘占用100%
  11. qt.qpa.plugin: Could not load the Qt platform plugin “xcb“ in问题
  12. 苹果电脑各型号支持的macOS版本列表
  13. 数仓建模—数据驱动业务
  14. 快速了解区块链六大特点
  15. kernel panic分析
  16. 超详细解决office2016和visio2016同时安装出错问题
  17. 乌鸦飞过flash素材下载_会声会影仿AE文字动画特效教程-会声会影中文官网
  18. HTML <kbd> 标签
  19. 人工智能换脸python_Python史诗级P图换脸小程序,AI换脸的简易版
  20. c语言有多难?一个新手刚学c语言的无奈

热门文章

  1. 高等数学-傅里叶级数与傅里叶变换
  2. The procedure WAL relies on the ability to hsync for proper operation during component failures
  3. authenticate总是返回None
  4. 修改文件后git只用两步push文件
  5. python调用mysql中的自定义function并且返回结果
  6. python2.7调用mysql存储过程并且返回结果
  7. mysql练习用的数据集下载(转载+自己补充步骤)
  8. python2.x环境下unicode乱码转中文显示的2种解决方案总结
  9. 64位ubuntu16.04下pycharm无法切换fcitx输入法和无法输入中文的问题
  10. python数学表达式3+(a+b)2_python3的基础学习之数学(2)