动态加载XAML文件
Silverlight 2 提供了 System.Windows.Markup.XamlReader.Load(string xaml) 来动态的创建 XAML , 但这差不多限于短小的 xaml 片段创建,若要是从 xaml 文件直接读取创建,则写一个函数 LoadXaml 比较现实。
XAML 文件本身也是资源,因此资源的 Build Action (生成操作)不同,则引用该文件的 uri 也不同。例如 build action 为 “Resource” 时,资源文件嵌入到程序集中;为 “Content” 时,只打包进 .xap 中等等,可参考《 Silverlight Resource 概 览 》。
代码比描述更能表 达意思,参考解决方案资源管理器和运行结果图示。 btn1.xaml 文件里其实也仅仅拿了个 button 作为示例: 。 btn2.xaml 和 btn3.xaml 只是用 Content 的不同来显示不同的 Build Action 。 btnRed 是用 string 方式创建的, btnDef 则是因为错误的引用 xaml 文件。 page.xaml 里有一个名称为 sp 的 StackPanle 来承载这些 button ,别的没有。今天比较烦躁, Over ! Silverlight 2 昨晚 6 点正式发布,这周末再考虑升级。
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Page_Loaded);
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
//Silverligh2 提供 XamlReader.Load(string xaml) 片段式 创建xaml。
//命名空间"http://schemas.microsoft.com/client/2007" 不能少。
string buttonXAML = "<Button xmlns='http://schemas.microsoft.com/client/2007' Width=\"60\" Height=\"50\" Content=\"string\" Margin=\"10\" Foreground=\"Red\"></Button>";
Button btnRed = (Button)System.Windows.Markup.XamlReader.Load(buttonXAML);
sp.Children.Add(btnRed);
//从文件创建,该文件 的Build Action 为 "Page", 与"Resource"类似,被嵌入程序集内
Button btn1 = (Button)LoadXaml("/LoadXaml;component/btns/btn1.xaml");
sp.Children.Add(btn1);
//从文件创建,该文件 的Build Action 为 "Resource",被嵌入程序集内
//注意格式:"/命名空间;component/文件位置"
Button btn2 = (Button)LoadXaml("/LoadXaml;component/btns/btn2.xaml");
sp.Children.Add(btn2);
//从文件创建,该文件 的Build Action 为 "Content",仅打包进.xap中,不在任何程序集内
Button btn3 = (Button)LoadXaml("btns/btn3.xaml");
sp.Children.Add(btn3);
//从错误的文件创建,最终生成的是默认的对象
Button btnDef = (Button)LoadXaml("btns/btn4.xaml");
sp.Children.Add(btnDef);
}
/// <summary>
/// 从xaml文件中 创建 xaml对象
/// </summary>
/// <param name="file">xaml文件的"地址"</param>
/// <returns>返回xaml文件定义的对象</returns>
public static object LoadXaml(string file)
{
Uri fileUri = new Uri(file, UriKind.Relative);
System.Windows.Resources.StreamResourceInfo streamInfo = System.Windows.Application.GetResourceStream(fileUri);
if ((streamInfo != null) && (streamInfo.Stream != null))
{
using (System.IO.StreamReader reader = new System.IO.StreamReader(streamInfo.Stream))
{
return System.Windows.Markup.XamlReader.Load(reader.ReadToEnd());
}
}
//若文件不存在,则返回默认创建的对象
return CreateDefaultObject();
}
//创建默认对象,以一个简单的button为例
public static object CreateDefaultObject()
{
Button btn = new Button();
btn.Width = 60;
btn.Height = 50;
btn.Margin = new Thickness(10);
btn.Foreground = new SolidColorBrush(Colors.Blue);
btn.Content = "Default";
return btn;
}
}
xaml代码:
<UserControl x:Class="SilverlightApplication2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot">
<StackPanel x:Name="sp">
</StackPanel>
</Grid>
</UserControl>
还没有评论,来说两句吧...