本文来安利大家 XamlTreeDump 库,通过这个库可以将 XAML 树上的元素转换为 json 字符串,可以用来进行 UI 单元测试

开始之前先通过 NuGet 工具安装 XamlTreeDump 库

假定咱有 UI 界面代码如下

<Page
    x:Class="WholebaycarNowawnawka.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WholebaycarNowawnawka"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

  <Grid>
    <TextBlock>欢迎访问我博客 https://blog.lindexi.com 里面有大量 UWP WPF 博客</TextBlock>
  </Grid>
</Page>

可以在后台代码使用 VisualTreeDumper.DumpTree 方法获取 json 字符串

        public MainPage()
        {
            InitializeComponent();
            Loaded += MainPage_Loaded;
        }

        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            var xaml = VisualTreeDumper.DumpTree(this, null, Array.Empty<string>(), new AttachedProperty[0]);
        }

上面代码拿到的 xaml 字符串如下

{
  "XamlType": "WholebaycarNowawnawka.MainPage",
  "Background": "#FFFFFFFF",
  "BorderBrush": null,
  "BorderThickness": "0,0,0,0",
  "Clip": null,
  "CornerRadius": "0,0,0,0",
  "FlowDirection": "LeftToRight",
  "Foreground": "#FF000000",
  "HorizontalAlignment": "Stretch",
  "Margin": "0,0,0,0",
  "Padding": "0,0,0,0",
  "RenderSize": [1200, 900],
  "VerticalAlignment": "Stretch",
  "Visibility": "Visible",
  "children": 
  [
  {
    "XamlType": "Windows.UI.Xaml.Controls.Grid",
    "Background": null,
    "BorderBrush": null,
    "BorderThickness": "0,0,0,0",
    "Clip": null,
    "CornerRadius": "0,0,0,0",
    "FlowDirection": "LeftToRight",
    "HorizontalAlignment": "Stretch",
    "Margin": "0,0,0,0",
    "Padding": "0,0,0,0",
    "RenderSize": [1200, 900],
    "VerticalAlignment": "Stretch",
    "Visibility": "Visible",
    "children": 
    [
    {
      "XamlType": "Windows.UI.Xaml.Controls.TextBlock",
      "Clip": null,
      "FlowDirection": "LeftToRight",
      "Foreground": "#FF000000",
      "HorizontalAlignment": "Stretch",
      "Margin": "0,0,0,0",
      "Padding": "0,0,0,0",
      "RenderSize": [1200, 900],
      "Text": "欢迎访问我博客 https://blog.lindexi.com 里面有大量 UWP WPF 博客",
      "VerticalAlignment": "Stretch",
      "Visibility": "Visible"
    }
    ]
  }
  ]
}

详细请看 asklar/XamlTreeDump: A UWP library to produce and compare XAML tree dumps useful for visual end-to-end testing

本文所有代码放在 githubgitee 欢迎小伙伴访问


本文会经常更新,请阅读原文: https://blog.lindexi.com/post/win10-uwp-%E4%BD%BF%E7%94%A8-XamlTreeDump-%E8%8E%B7%E5%8F%96-XAML-%E6%A0%91%E5%85%83%E7%B4%A0%E5%86%85%E5%AE%B9.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

如果你想持续阅读我的最新博客,请点击 RSS 订阅,推荐使用RSS Stalker订阅博客,或者前往 CSDN 关注我的主页

知识共享许可协议 本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接: https://blog.lindexi.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系

无盈利,不卖课,做纯粹的技术博客

以下是广告时间

推荐关注 Edi.Wang 的公众号

欢迎进入 Eleven 老师组建的 .NET 社区

以上广告全是友情推广,无盈利