前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >UNO WinUI 已知问题 在 XAML 条件构建里将 win 平台加入 Ignorable 将构建失败

UNO WinUI 已知问题 在 XAML 条件构建里将 win 平台加入 Ignorable 将构建失败

作者头像
林德熙
发布2024-03-05 10:44:15
1060
发布2024-03-05 10:44:15
举报
文章被收录于专栏:林德熙的博客

如果在 UNO 项目里面,为了进行 XAML 条件构建,将 win 平台加入到 mc:Ignorable 里面,将会在构建时提示 Xaml Internal Error error WMC9999: Unexpected ‘NONE’ in parse rule ‘Element ::= . EmptyElement ( StartElement ElementBody ).’. 错误

如以下的代码,将会在构建时失败,提示 Xaml Internal Error error WMC9999: Unexpected 'NONE' in parse rule 'Element ::= . EmptyElement | ( StartElement ElementBody ).'. 错误

代码语言:javascript
复制
<Page x:Class="KernarjeheeboLawbeeferedai.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:local="using:KernarjeheeboLawbeeferedai"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:win="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:not_win="http://uno.ui/not_win"
      Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
      mc:Ignorable="d win not_win">
  <StackPanel
        HorizontalAlignment="Center"
        VerticalAlignment="Center">
    <win:TextBlock AutomationProperties.AutomationId="HelloTextBlock"
                  Text="Hello Uno Platform"
                  HorizontalAlignment="Center" />
    <not_win:TextBlock AutomationProperties.AutomationId="HelloTextBlock"
                       Text="Hello"
                       HorizontalAlignment="Center" />
  </StackPanel>
</Page>

以上的失败仅仅只会在 WinUI 3 平台构建失败,换句话说就是即使新建一个 WinUI 3 项目,直接抄以上的代码也是会出现完全相同的错误信息

失败核心代码在于 mc:Ignorable="d win not_win" 将 win 平台加入到忽略列表里面。本质原因是 win 平台指向的 http://schemas.microsoft.com/winfx/2006/xaml/presentation 刚好就是当前的页面的默认命名空间

换句话说就是 xmlns:win="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 默认命名空间是相同的,这就导致了 Root 元素,即以上代码的 <Page 被忽略掉。因为 <Page 的命名空间就是 http://schemas.microsoft.com/winfx/2006/xaml/presentation 刚好就和加入到 mc:Ignorable 的 win 是相同的

根据 XAML 的规定,加入到 mc:Ignorable 为忽略列表,这也就是 d 设计时可用的原因。加入到 mc:Ignorable 忽略列表里面的元素可以被其他逻辑进行特殊处理,比如说 UNO 的 XAML 处理模块将会处理这些内容作为 XAML 条件构建

在 UNO 的官方文档 里面也有这样一段话,说明了不应该将 win 加入到 Ignorable 里面,内容如下

For prefixes which will be excluded on Windows (e.g. android, ios), the actual namespace is arbitrary, since the Uno parser ignores it. The prefix should be put in the mc:Ignorable list. For prefixes which will be included on Windows (e.g. win, not_android) the namespace should be http://schemas.microsoft.com/winfx/2006/xaml/presentation and the prefix should not be put in the mc:Ignorable list.

根据上文所述的 XAML 规范可知,除了 win 前缀之外,其他的任何使用了 http://schemas.microsoft.com/winfx/2006/xaml/presentation 命名空间的前缀,都不能加入到 mc:Ignorable 里面,比如 win not_android not_ios not_wasm not_macosnot_skia 前缀。更多请看 UNO 的官方文档 里面的列表

本文代码放在 githubgitee 上,可以使用如下命令行拉取代码

先创建一个空文件夹,接着使用命令行 cd 命令进入此空文件夹,在命令行里面输入以下代码,即可获取到本文的代码

代码语言:javascript
复制
git init
git remote add origin https://gitee.com/lindexi/lindexi_gd.git
git pull origin eee7d7898c92da0bffd1cba4fdacaa64b3f79c01

以上使用的是 gitee 的源,如果 gitee 不能访问,请替换为 github 的源。请在命令行继续输入以下代码,将 gitee 源换成 github 源进行拉取代码

代码语言:javascript
复制
git remote remove origin
git remote add origin https://github.com/lindexi/lindexi_gd.git
git pull origin eee7d7898c92da0bffd1cba4fdacaa64b3f79c01
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档