微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

步骤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
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="600" Height="400">



                这是Test.Xaml文件




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();                       
        }
///


/// 增加一个Content属性
///

public new UIElement Content
        {
get
            {
return base.Content;
            }
set
            {
base.Content = value;
            }
        } 
    }
}

Test2.Xaml完整内容如下:

Code
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="600" Height="400">



                这是Test2.Xaml文件




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();            
        }
///

/// 增加一个Content属性 /// public new UIElement Content         { get             { return base.Content;             } set             { base.Content = value;             }         }      } }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐