动态加载XAML文件

一时失言乱红尘 2022-04-22 03:54 420阅读 0赞

Silverlight 2 提供了 System.Windows.Markup.XamlReader.Load(string xaml) 来动态的创建 XAML , 但这差不多限于短小的 xaml 片段创建,若要是从 xaml 文件直接读取创建,则写一个函数 LoadXaml 比较现实。

XAML 文件本身也是资源,因此资源的 Build Action (生成操作)不同,则引用该文件的 uri 也不同。例如 build action 为 “Resource” 时,资源文件嵌入到程序集中;为 “Content” 时,只打包进 .xap 中等等,可参考《 Silverlight Resource 概 览 》。

f54debf8f37eaa15d9f9fd33.jpg

代码比描述更能表 达意思,参考解决方案资源管理器和运行结果图示。 btn1.xaml 文件里其实也仅仅拿了个 button 作为示例: 。 btn2.xaml 和 btn3.xaml 只是用 Content 的不同来显示不同的 Build Action 。 btnRed 是用 string 方式创建的, btnDef 则是因为错误的引用 xaml 文件。 page.xaml 里有一个名称为 sp 的 StackPanle 来承载这些 button ,别的没有。今天比较烦躁, Over ! Silverlight 2 昨晚 6 点正式发布,这周末再考虑升级。

  1. public partial class Page : UserControl
  2. {
  3. public Page()
  4. {
  5. InitializeComponent();
  6. this.Loaded += new RoutedEventHandler(Page_Loaded);
  7. }
  8. private void Page_Loaded(object sender, RoutedEventArgs e)
  9. {
  10. //Silverligh2 提供 XamlReader.Load(string xaml) 片段式 创建xaml。
  11. //命名空间"http://schemas.microsoft.com/client/2007" 不能少。
  12. string buttonXAML = "<Button xmlns='http://schemas.microsoft.com/client/2007' Width=\"60\" Height=\"50\" Content=\"string\" Margin=\"10\" Foreground=\"Red\"></Button>";
  13. Button btnRed = (Button)System.Windows.Markup.XamlReader.Load(buttonXAML);
  14. sp.Children.Add(btnRed);
  15. //从文件创建,该文件 的Build Action 为 "Page", 与"Resource"类似,被嵌入程序集内
  16. Button btn1 = (Button)LoadXaml("/LoadXaml;component/btns/btn1.xaml");
  17. sp.Children.Add(btn1);
  18. //从文件创建,该文件 的Build Action 为 "Resource",被嵌入程序集内
  19. //注意格式:"/命名空间;component/文件位置"
  20. Button btn2 = (Button)LoadXaml("/LoadXaml;component/btns/btn2.xaml");
  21. sp.Children.Add(btn2);
  22. //从文件创建,该文件 的Build Action 为 "Content",仅打包进.xap中,不在任何程序集内
  23. Button btn3 = (Button)LoadXaml("btns/btn3.xaml");
  24. sp.Children.Add(btn3);
  25. //从错误的文件创建,最终生成的是默认的对象
  26. Button btnDef = (Button)LoadXaml("btns/btn4.xaml");
  27. sp.Children.Add(btnDef);
  28. }
  29. /// <summary>
  30. /// 从xaml文件中 创建 xaml对象
  31. /// </summary>
  32. /// <param name="file">xaml文件的"地址"</param>
  33. /// <returns>返回xaml文件定义的对象</returns>
  34. public static object LoadXaml(string file)
  35. {
  36. Uri fileUri = new Uri(file, UriKind.Relative);
  37. System.Windows.Resources.StreamResourceInfo streamInfo = System.Windows.Application.GetResourceStream(fileUri);
  38. if ((streamInfo != null) && (streamInfo.Stream != null))
  39. {
  40. using (System.IO.StreamReader reader = new System.IO.StreamReader(streamInfo.Stream))
  41. {
  42. return System.Windows.Markup.XamlReader.Load(reader.ReadToEnd());
  43. }
  44. }
  45. //若文件不存在,则返回默认创建的对象
  46. return CreateDefaultObject();
  47. }
  48. //创建默认对象,以一个简单的button为例
  49. public static object CreateDefaultObject()
  50. {
  51. Button btn = new Button();
  52. btn.Width = 60;
  53. btn.Height = 50;
  54. btn.Margin = new Thickness(10);
  55. btn.Foreground = new SolidColorBrush(Colors.Blue);
  56. btn.Content = "Default";
  57. return btn;
  58. }
  59. }

xaml代码:

  1. <UserControl x:Class="SilverlightApplication2.MainPage"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5. mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
  6. <Grid x:Name="LayoutRoot">
  7. <StackPanel x:Name="sp">
  8. </StackPanel>
  9. </Grid>
  10. </UserControl>

发表评论

表情:
评论列表 (有 0 条评论,420人围观)

还没有评论,来说两句吧...

相关阅读