【问题标题】:How to load a control from .xaml in Silverlight如何在 Silverlight 中从 .xaml 加载控件
【发布时间】:2011-12-07 09:28:28
【问题描述】:

问题是关于将一​​些复杂的控件加载到堆栈面板中。 我有几个类似的 TreeListControls(来自 devexpress),它们有自己的 x:Name 属性,我在 .cs 文件中使用。

我希望将它们保存在单独的 .xaml 中,并在需要时加载。

如何加载它们?我还没有找到任何可行的解决方案。我发现的那个 Application.LoadComponent 不能正常工作...

目前我看到的唯一方法是使用“可见性”属性,但这完全是业余爱好者......

【问题讨论】:

    标签: silverlight xaml


    【解决方案1】:

    您可以为此使用XAML reader

    【讨论】:

    • 这是我发现的第一件事 - Silverlight 中没有 LogicalTreeHelper
    【解决方案2】:

    为什么不直接创建用户控件?您可以从代码中实例化它们,并在内容控件或网格中添加或删除它们。

    RootGrid.Children.Add(new WhateverControl());
    

    或者您是否需要在运行时编辑 xaml?

    Application.LoadComponent 至少可以为我创建非 UI 对象

    Application.LoadComponent(Root, new Uri("/SilverlightClient;component/NavigationTreeConfig.xaml", UriKind.Relative));
    

    当我需要在代码中创建 XAML 时,我会使用带有字符串的 XamlReader。不过还没有尝试使用文件。

    ContentTemplate = (DataTemplate) XamlReader.Load(
                    "<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'><TextBlock>TemplateSelectorError: " + errorMessage + "</TextBlock></DataTemplate>");
    

    【讨论】:

      猜你喜欢
      • 2011-10-28
      • 1970-01-01
      • 1970-01-01
      • 2011-07-16
      • 2011-03-14
      • 1970-01-01
      • 1970-01-01
      • 2011-12-11
      • 1970-01-01
      相关资源
      最近更新 更多