在Windows 8.1运行时应用程序中动态加载Xaml

时间:2014-05-12 10:01:56

标签: c# xaml windows-phone-8 windows-phone-8.1

我正在编写一个Windows 8.1手机运行时应用程序。我的资产中有一个文件,其中包含Xaml内容。按下按钮时,我想制作文件中Xaml定义的Stackpanel的内容。

我搜索了一下,发现我们可以读取字符串中的Xaml文件并将其传递给XamlReader类,然后可以使用该类将新的Xaml分配给StackPanel。我所指的方法是here,代码也写在下面。

string xaml =
"<Ellipse Name=\"EllipseAdded\" Width=\"300.5\" Height=\"200\" 
Fill=\"Red\" \"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"/>";
object ellipse = XamlReader.Load(xaml);

//stackPanelRoot is the visual root of a Page in existing XAML markup already loaded by the appmodel
stackPanelRoot.Children.Add(ellipse as UIElement);

//walk the tree using XLinq result and cast back to a XAML type to set a property on it at runtime
var result = (from item in stackPanelRoot.Children
  where (item is FrameworkElement) 
  && ((FrameworkElement) item).Name == "EllipseAdded"
  select item as FrameworkElement).FirstOrDefault();

((Ellipse) result).Fill = new SolidColorBrush(Colors.Yellow);

我的问题是 XamlReader类在Windows 8.1手机运行时应用程序中不可用。我正在使用Visual Studio 2013.我找到了一个名为Windows.UI.Xaml的命名空间,它位于Windows 8.1手机运行时应用程序中。

任何人都可以指导我如何实现在Windows Phone 8.1运行时加载新Xaml的功能。

1 个答案:

答案 0 :(得分:3)

Windows Phone 8.1 Silverlight应用和Windows Phone 8.1商店应用均提供XamlReader