1,先在Silverlight项目中新建一个接口文件IContent.cs,内容如下:
using System.Windows;namespace BookStore { public interface IContent { UIElement Content { get; set; } }}
2,建立两个xaml文件:Second.xaml和SilverlightControl1.xaml
SilverlightControl1.xaml的完整内容如下:
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using BookStore;namespace SilverlightApplication1{ public partial class SilverlightControl1 : UserControl,IContent { public SilverlightControl1() { InitializeComponent(); } public new UIElement Content { get { return base.Content; } set { base.Content = value; } } private void BtnFirst_Click(object sender,EventArgs e) { (Application.Current.RootVisual as IContent).Content = new Second(); } }}
Second.xaml完整内容如下:
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using BookStore;namespace SilverlightApplication1{ public partial class Second : UserControl,IContent { public Second() { InitializeComponent(); } public new UIElement Content { get { return base.Content; } set { base.Content = value; } } private void BtnSecond_Click(object sender,EventArgs e) { (Application.Current.RootVisual as IContent).Content = new SilverlightControl1(); } }}