You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WinUI3创建Grid时抛出System.Runtime.InteropServices.COMException问题求助

WinUI3中Grid添加重复RowDefinition实例引发COMException(0x800F1000)的解决方法

问题现象

在WinUI3中创建Grid控件时,尝试将同一个RowDefinition实例重复添加到RowDefinitions集合,会抛出System.Runtime.InteropServices.COMException异常,错误码0x800F1000,提示信息为未检测到已安装的组件。

复现代码

var definition = new RowDefinition {Height = GridLength.Auto};
var grid = new Grid { RowDefinitions = { definition, definition } };

异常堆栈跟踪

System.Runtime.InteropServices.COMException (0x800F1000): No installed components were detected.

   at WinRT.DelegateExtensions.DynamicInvokeAbi(Delegate del, Object[] invoke_params)
   at ABI.System.Collections.Generic.IVectorMethods`1.Append(IObjectReference obj, T value)
   at Test..ctor()
   ...

排查测试

  • 直接在初始化时创建新的RowDefinition实例添加:var g1 = new Grid{RowDefinitions = { new RowDefinition {Height = GridLength.Auto} } }; 无异常。
  • 单独创建RowDefinition实例:RowDefinition d = new() {Height = GridLength.Auto}; 无异常。
  • 将同一个RowDefinition实例添加到RowDefinitions:var g3 = new Grid{RowDefinitions = {d}}; 抛出COMException。

解决方案

确保每次添加到RowDefinitions的都是独立的RowDefinition实例,以下两种写法均可正常运行:

写法1:直接在集合初始化中创建新实例

var grid = new Grid { RowDefinitions = { new RowDefinition {Height = GridLength.Auto}, new RowDefinition {Height = GridLength.Auto} } };

写法2:通过方法返回新实例

RowDefinition CreateRowDefinition() => new RowDefinition {Height = GridLength.Auto};
var grid = new Grid { RowDefinitions = { CreateRowDefinition(), CreateRowDefinition() } };

原因分析

WinUI3中的RowDefinition属于基于COM的UI元素包装类,每个实例只能被关联到一个父容器(或集合)。当尝试将同一个实例重复添加到RowDefinitions集合时,COM层面会因为实例已被注册而抛出错误,导致出现0x800F1000异常。必须保证每个RowDefinition都是独立的实例,才能正常添加到集合中。

内容的提问来源于stack exchange,提问作者子斗子

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 00:12:52