silverlight中如何方便在多个"场景"即Xaml文件之间随意切换?(转)
2011-07-04 10:42
435 查看
步骤1.先在silverlight项目中新建一个接口文件IContent.cs,内容如下(namespace请各位根据自己的实际情况修改):
using System.Windows;
namespace BookStore
{
public interface IContent
{
UIElement Content { get; set; }
}
}
步骤2.建二个Xaml文件Test.xaml和Test2.Xaml
Test.Xaml完整内容如下:
<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完整内容如下:
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完整内容如下:
<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几乎一样)
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;
}
}
}
}
转自:/article/4607669.html
using System.Windows;
namespace BookStore
{
public interface IContent
{
UIElement Content { get; set; }
}
}
步骤2.建二个Xaml文件Test.xaml和Test2.Xaml
Test.Xaml完整内容如下:
<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完整内容如下:
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完整内容如下:
<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几乎一样)
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;
}
}
}
}
转自:/article/4607669.html
相关文章推荐
- silverlight中如何方便在多个"场景"即Xaml文件之间随意切换?
- silverlight中如何方便在多个"场景"即Xaml文件之间随意切换?
- silverlight中如何方便在多个"场景"即Xaml文件之间随意切换
- silverlight中如何方便在多个"场景"即Xaml文件之间随意切换?
- Silverlight中多个Xaml("场景"? or "窗口"? )之间的切换/调用/弹出/传参数问题小结
- Silverlight中多个Xaml("场景"? or "窗口"? )之间的切换/调用/弹出/传参数问题小结
- Silverlight中多个Xaml("场景"? or "窗口"? )之间的切换/调用/弹出/传参数问题小结
- struts中多个模块时,使用多个struts-config.xml文件之间时如何切换的!
- Silverlight中多个Xaml之间的切换/调用/弹出/传参数(转)
- Silverlight中多个Xaml之间的切换/调用/弹出/传参
- Silverlight中多个Xaml之间的切换/调用/弹出/传参数问题小结
- 如何在linux和windows之间进行文件的随意拷贝
- silverlight内部XAML之间切换的方法
- silverlight3.0 怎么调试XAML对应的"XAML.CS"文件
- SilverLight学习笔记--如何在xaml文件中操作用户在后台代码定义的类(2)--示例篇:创建一个登录控件(原创)(转载本文请注明出处)
- Silverlight中多个Xaml之间的切换/调用/弹出/传参数问题小结
- Silverlight中多个Xaml之间的切换/调用/弹出/传参数问题小结
- 如何解决如"未能加载文件或程序集“Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或
- 导航视图控制器中如何定义按钮以及视图之间的切换
- 如何在Silverlight中切换“页面”