简体   繁体   中英

How have test project reference Maui project?

I've recently jumped into cross-platform development and wanted to just bite the bullet and go straight into the newest framework that the do.net team has started working on, MAUI.

The issue I've run into is trying to add another test project in the solution and referencing the MAUI project. As I'm pretty new to Visual Studio as well, this may just be a rookie mistake and I just don't know what I'm doing, but here I am asking away.

What I've done:

  • Created MAUI project with the default template in Visual Studio 2022 Preview 17.2.0, with target frameworks .net6.0,.net6.0-android &.net6.0-ios)
  • Added XUnit test project on the side with target framework .net6.0)
  • Added UseMaui=true as a property of the test project

I've also tried to add all the target frameworks into the project, but that causes more issues than it solves. I've tried to follow this open source project where they got it to work, but I'm unsure how to achieve it.

Some screenshots

Test Project.csproj

Alt+Enter of class not being imported

Manually added import because suggestions did not add them

Error in Test Project

Test Project

This link may be helpful. https://www.softwaremeadows.com/posts.net_maui_progressing_from_a_default_project_part_6_-_revisiting_unit_testing/

Summary:

  1. In your .csproj file, add .net6.0' to TargetFrameworks as below.

     <TargetFrameworks.net6.0.net6.0-android.net6.0-ios.net6.0-maccatalyst</TargetFrameworks>
  2. Modify OutputType to use Condition.

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

Its probably because of mismatch in project target frameworks. Easy fix is to add Target Framework to your maui project that matches the one used in test project. For example if your Test poject is base on net6.0, just add net6.0 as a target framework to your maui app project.

Edit 1: Also sometimes you need to add empty start point for netX.X build, looking like this

public static void Main(string[] args)
{

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM