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

如何为.NET 8.0 MAUI应用使用条件编译实现单元测试?

.NET 8 MAUI单元测试环境搭建:DevExpress视图代码编译排除问题

我正在为.NET 8.0 MAUI应用搭建单元测试环境。此前的常规做法是添加net8.0目标框架,通过条件编译禁用特定功能来适配测试,以下配置可在多数场景下排除特定平台包:

<OutputType Condition="'$(TargetFramework)' != 'net8.0'">Exe</OutputType>

<ItemGroup Condition="'$(TargetFramework)' != 'net8.0'">
    <PackageReference Include="DevExpress.Maui.CollectionView" Version="23.1.6" />
    <PackageReference Include="DevExpress.Maui.Controls" Version="23.1.6" />
    <PackageReference Include="DevExpress.Maui.Editors" Version="23.1.6" />
</ItemGroup>

但引入DevExpress库后遇到了问题:对于包含该库引用的XAML页面,我希望像Platforms文件夹那样在Visual Studio中排除编译但保留文件可见性,尝试了多种配置组合:

第一种配置:

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">    
    <Compile Remove="**\MyPage.xaml.cs" />
    <MauiXaml Remove="**\MyPage.xaml" /> 
</ItemGroup>

第二种配置:

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
    <None Update="**\MyPage.xaml;" /> 
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' != 'net8.0'">
    <None Update="**\MyPage.xaml.cs;" />
</ItemGroup>

这些配置要么导致文件在VS中不再显示,要么引发正常应用或单元测试构建失败。

目前已知的解决方案:DevExpress官方不支持包含视图代码的单元测试,建议通过拆分项目的方式,将视图层代码与可测试的业务逻辑代码分离。


内容的提问来源于stack exchange,提问作者Christopher Richmond

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 05:20:04