How have test project reference Maui project? - c#

I've recently jumped into cross-platform development and wanted to just bite the bullet and go straight into the newest framework that the dotnet 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:
In your .csproj file, add 'net6.0' to TargetFrameworks as below.
<TargetFrameworks>net6.0;net6.0-android;net6.0-ios;net6.0-maccatalyst</TargetFrameworks>
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)
{
}

Related

How do I define a C# Unit Test Project using CMake?

I'm converting some existing C# projects to be defined in CMake -- moving from the previous include_external_msproject() directive to the newer full support for C#.
But I'm not seeing how to convert projects of the Visual C# Unit Test Project type. I'm able to build them as libraries, and compile them successfully -- but Visual Studio doesn't show them as unit test projects, just as regular libraries. Most crucially, the tests aren't visible to the Test Explorer.
Things I've already tried include:
Adding TestProjectType=UnitTest as a target property:
<TestProjectType>UnitTest</TestProjectType>
Adding a reference path, as follows, as a target property:
<ReferencePath>$(ProgramFiles)/Common Files/microsoft shared/VSTT/$(VisualStudioVersion)/UITestExtensionPackages</ReferencePath>
Adding Microsoft.VisualStudio.QualityTools.UnitTestFramework as a project reference (using CMake's VS_DOTNET_REFERENCES property).
I'm using Microsoft Visual Studio Professional 2015, CMake 3.13.2, .NET Framework 4.5.2 (but I suspect the issue isn't specific to my particular version combination).
Manually adding <TestProjectType>UnitTest</TestProjectType>, made my unit test project show up as unit test. Trying to figure out how to do that using cmake.
Update: the following shows the project as unittest
set_target_properties(${target_name}
PROPERTIES
VS_GLOBAL_PROJECT_TYPES "{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"
)
Documentation for VS_GLOBAL_PROJECT_TYPES is here.
I found this documentation for Project Guid: Visual Studio project type guids
Thanks #bhardwajs, your answer helped me.
You can also add TestProjectType to global properties:
set_target_properties(${target_name} PROPERTIES
VS_GLOBAL_TestProjectType "UnitTest"
VS_GLOBAL_PROJECT_TYPES "{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}")

Errors in Unity console but not in Visual Studio?

I recently upgraded my Unity C# project's .NET version from 3.5 to 4.5.2. Everything compiles beautifully in my Visual Studio project, but i have errors in my Unity Console which are not showing in VS and these errors are prohibiting me from running my project. It seems as though my project didn't actually upgrade to 4.5.2, is there any way to solve this?
EDIT: i've tried upgrading unity's framework version by going into Edit -> Project Settings -> Player -> Configuration -> Scripting Runtime Version and changing that value to .NET 4.x Equivalent but it changed nothing
Do you have a copy of the MySql.Data.dll in your Assets folder? References in Unity work differently than in Visual Studio. You add a reference in Unity by dropping the .dll into the Assets folder (preferably in a directory that makes sense, not at the root so that it is easy to find and doesn't clutter the root). You should always get your code working in Unity first and then compile in Visual Studio, not the other way around.

Visual Studio 2017 error: Cannot find project info for “” This can indicate a missing project reference - with Console App .NET Core 2.1

VS 15.7.5
I have a .NET Standard 2.0 class library:
It exists in its own solution but I have now added it to a different solution so that I can use it with my .NET Core 2.1 Console test runner:
However as soon as I do this I get the error. If I remove the console application then everything compiles without error.
So what am I missing here? Why can a .NET Core 2.1 Console application not find my .NET Standard 2.0 class library?
I just tried to replicate what you did and it worked for me !
My .net 2 standard
That has only one class
public class School
{
public string Name => "Mango Hill";
}
And my .net core 2.1 project
that calls the class from the library
you can see the output.
So I suggest you check the common project to see if it references any full library and then try to isolate the references.
The issue here is that for whatever reason .NET Core Console apps will fail with the error I received if the following are true:
You create the standard project within a folder under the original solution that contains it. So say you have a solution called slnStandard in the path D:\Rubbish\slnStandard.
You then add a new standard class library project Standard.Project.cproj in the path:
a) D:\Rubbish\slnStandard\Standard.Project <== Will Compile in slnStandard
b) D:\Rubbish\slnStandard\SubFolder\Standard.Project <== Will Compile in slnStandard
Create second solution slnSecond and add a solution folder called MySolutionFolder. Add Standard.Project.cproj from 1 above (both versions). slnSecond will compile
Add a MSTest .NET Core Console application to slnSecond. Then "Add Reference" > Project using what has been added in 2.
If the reference is 1b) it will NOT COMPILE. So basically you need to keep the folder structure flat. Hopefully this will be fixed moving forward.
Just try running "build" or "restore" command using Dotnet CLI 2.1 version. from command window
c:\path> dotnet restore [.net core 2.1 console].csproj
this may be required once if you switch TargetFramework using visual studio.
either from .net core 2.0 to 2.1 or vice versa
If you could build the console project from dotnet CLI command "build". and you are getting this error only in visual studio.
Please try to create solution using dotnet "sln" and "add" command to refer the projects.
This happened to me when the path was cloned from git with spaces "%20". After I removed the spaces, it built without issues in Visual Studio 2017. Interestingly, I had the same issue in JetBrains Ryder, but not the command line (dotnet build).

How to control NuGet packages in Android Class Library?

The setup is like this:
A Xamarin.Android application, which depends on Android class library
(at least that's what the template is called in VS)
Said class
library, the purpose of which (not entirely relevant, but FYI) is
interfacing with a REST service and has a dependance on the famous
Newtonsoft.Json NuGet package.
An NUnit test project for said
library.
IDE is Visual Studio 2017, latest version.
If you build and deploy the app on the phone, everything is fine.
However, if you try to run tests from the tests project, it says that it can't find the Newtonsoft library.
I've even managed to find a sort of reason: when the library gets built, it's dependancies aren't packed inside, and they are not copied to build directory.
When .apk is built for the phone, the dependancies ARE getting packed inside.
However, when NUnit project builds itself, it only takes the library, and the dependancies are nowhere to be found.
However, there's no interface to control the behaviour of NuGet "Package Reference" type dependencies (blue ones), the properties window is empty for them. And I found no way to add NuGet packages to this kind of project as a ".config" type of dependancy (grey one).
There is a workaround - you can add the Newtonsoft package to the NUnit test project, then it gets copied to the build directory and the Android library works with it, however it doesn't feel right to me. Tests don't need that reference and it has no business in that project.
How to control NuGet packages in Android Class Library?
Your workaround is the correct solution, you don't need to worry too much about it.
That is because the Newtonsoft package is not used directly in your NUnit test project, so Visual Studio / MSBuild doesn't know if your test project needs this Newtonsoft library. In order to avoid reference pollution in NUnit test project, Visual Studio / MSBuild tries to only bring references over into NUnit test project that it detects as being required by project Xamarin.Android application.
So, to resolve this issue, we often add Newtonsoft to the test project or give a copy task to copy it to the test project.
See This Thread for some more details.
Looking for an answer to another question I now found info that my described behaviour is a known problem, described by .NET developers here:
https://github.com/dotnet/standard/issues/481

How to use C# dll in F# project? Getting yellow triangle with an exclamation mark [duplicate]

I wish to test the core class of a plugin by directly referencing the plugin project and instantiating the plugin class. When I create a test Console App project and add a project reference to the plugin project, I get a warning icon (yellow triangle with exclamation mark) next to the reference in the References list.
When I instead add a reference to the dll, the assembly build output of the plugin, I get no such warning. What could this warning be trying to tell me?
As mentioned in the question's comments, differing .NET Framework versions between the projects can cause this. Check your new project's properties to ensure that a different default version isn't being used.
Encountered the same issue with a ASP.Net Web App and two library class projects which needed to be referenced within the Web App. I had no information provided on why the build failed and the references were invalid.
Solution was to ensure all projects had the same Target Framework:
In Visual Studio 2015- Right Click project > Properties > Application > Target Framework
Save, Clean and Rebuild solution. The project references should no longer appear as yellow warnings and the solution will compile.
My Web App was targeting .Net 4.5 whereas the other two dependent library class projects targeted .Net v4.5.2
Make sure all versions are same for each projects click each projects and see the version here Project > Properties > Application > Target .NET framework
a. Go to Tools > Nuget Package Manager > Package Manager Console Type Update-Package -Reinstall (if not working proceed to 2.b)
b. THIS IS CRITICAL BUT THE BIGGEST POSSIBILITY THAT WILL WORK. Remove < Target > Maybe with multiple lines < /Target > usually found at the bottom part of .csproj.
Save, load and build the solution.
For both of (or all of) the projects that you want to use together:
Right click on the project > Properties > Application > Target .NET framework
Make sure that both of (or all of) your projects are using the same .NET framework version.
Reinstall all packages in all projects of the current solution:
Update-Package -Reinstall
Try closing and opening VS.
Seems silly but after 1 hour of following the above and finding everything lined up OK. I restarted VS 2017 and the problems were gone.
Make sure you have the projects targeting the same framework version. Most of the times the reason would be that current project ( where you are adding reference of another project ) points to a different .net framework version than the rest ones.
For me, I ran into this issue when referencing a .NET Standard 2.0 class library in a .NET Framework 4.7.1 console application. Yes, the frameworks are different, but they are compatible (.NET Standard is supposed to jive with both .NET Core and .NET Framework.) I tried cleaning, rebuilding, removing and readding the project reference, etc... with no success. Finally, quitting Visual Studio and reopening resolved the issue.
Check NETFramework of the referred dll & the Project where you are adding the DLL.
Ex:
DLL ==> supportedRuntime version="v4.0"
Project ==> supportedRuntime version="v3.0"
You will get warning icon.
Solution : Make dll version consistence across.
It's been a long time since this question was asked but if someone is still interested - I recently ran into similar icons. I was compiling a C#.net project using VS 2008. I found VS could not locate the assemblies for those references. When I double clicked VS refreshed the references and removed the icons on some of those[EDIT: which it could NOW locate]. For remaining references, I had to compile the respective assemblies.
Adding my 2 cents to the #kad81 answer,
Go to Visual Studio -> BUILD -> Configuration Manager
In the "Active Solution Platform" drop down in top right hand corner (mine is VS 2012), if it is "Mixed Platforms", change it to the appropriate platform based upon your reference third party assemblies.
Then in each of the project in the list, make sure you select same platform for all the project. (if x86 not exist, then select "", then you can select "x86".)
Rebuild the library projects first and then referencing projects.
Hope this helps.
In Asp.net core sometime it shows alert if you changes the project name space or name. To remove this kind of alerts you just Unload Project and load it again.
If issue is still there means you it can not find your Assembly reference.
Using Visual Studio 2019 with all projects targeting .Net Core 3.1 the solution was to:
Clean / Build / Rebuild.
Restart Visual Studio 2019
Also happens if you explicitly reference a project that was already implicitly referenced.
i.e
project a references project b
project c references project a (which adds implicit ref. Expand and see)
project c references project b
you will see an exclamation mark next to b under project references.
I had these icons for a different reason. We have one big solution for all our projects (nearly 100). I made a subselection of the projects I was interested in and made a new solution. However the references where project references instead of references to the compiled dll's....
After some research I found this link on GitHub which explains this is new behaviour in VS2015.
On the GitHub page they explain a workaround for converting project references to binary references.
To fix some not working stuff it has sense to remove some libraries sometimes, how would not that sound weird.
Anyways, I believe the problem is too wide and might be caused by different factors, so want to share my situation/solution.
I had a project (brought by customer) with Xamarin Forms and Telerik libraries. The thing was in general related to the components, which libraries are not included into packages folder, nor available via Nuget (paid ones).
The whole project References were "yellow", it looked horribly and scary.
The solution was just to remove those Telerik references (including a few controls in code which were using that). Right after that all the references magically got their common normal grey color and the errors (mostly) disappeared.
"Mostly" - because "all red around" error messages about "the element is not defined anywhere" sometimes happen still. That's weird, and brings inconvenience, but I still able to compile and run the project(s): just need to clean solution, restart Visual Studio, pray a little bit, clean again, remove obj/bin folders, restart again, and it works well.
The key thing is remove not available libraries references, as the error messages say absolutely another stuff. (For instance, something like "Xamarin.Build.Download.XamarinDownloadArchives not found or cannot find something" etc., but that just might mean you don't have some references available.
Then remove packages folder, reload/reopen the project/solution, go to "Manage Nuget Packages" and click "Restore" button.
In a multi-project solution, If every other thing failed... On the startUp project, check.
Dependencies->Assemblies and see if the erring referenced project is there. Remove it and re-build.
I had the same issue in a solution with projects targting .NET Core 3.1, .NET Standard 2.0 and .NET Framework 4.8. The issue was on this last one.
The trick that solved the issue for me, was to change the target framework to .NET Framework 4.5, then back to .NET Framework 4.8.
I have absolutely no idea why this fixed the issue, but it did.
The IDE was Visual Studio 2019.
Open the YOURPROJECT.csproj file with a text editor then at the end of the file remove these lines inside the target tag, and then build the project again!
be sure the Package folder is in the correct path which mentioned in < Reference > < HintPath >
<Error Condition="!Exists('.......
Enjoy it ;)
I also faced the same problem but my case was a bit different the ones above. I tried to open a project created in a different computer. I found that the path to package folder is not updated when you add a reference so restarting VS, changing .NET version, or any mentioned recommendation does not solve the problem. I opened the csproj file in notepad++ and corrected all the relative paths to packages folder. Then; all the warnings are gone. Hope it helps.
in VS 2017 Do a Clean then Build
Thank you all for the help. Here is a breakdown of how I fixed my problem:
Right click on your project > Properties
Under Application change the target Framework. In my case ImageSharp was using .Net 4.6.1. You can find this in your packages.config.
Go to your project references. You'll notice SixLabors has a yellow triangle. You have to update the NuGet package.
Right click on References > Manage NuGet Packages.
Update SixLabors.
You might have slight code updates (see below) but this fixed my problem.
Convert ImageSharp.Image to ImageSharp.PixelFormats.Rgba32?
In Visual Studio 2019, one of my projects target framework was .net core but it was referencing another project whose target framework was .net standard. I changed all of the projects to reference .net standard and the icons went away. To see what your project is right click it and click properties and look at Target framework. You can also normal click the project itself and look at the < TargetFramework > tag under < PropertyGroup >
I had created a new .sln which was put in a subfolder. The .nuget folder was missing from where that .sln file was added. Moving the .nuget folder from the root into the subfolder where my new .sln file was solved the issue for me.
I came back later and added the .sln file to the root and deleted the subfolder. Doing this originally would have solved the issue as well.
Based on the answer from #AljohnYamaro (sorry, couldn't comment on your answer, new account without enough reputation yet, but upvotaded you), I've checked the .csproj file.
On my file, besides the standard project reference:
<ProjectReference Include="..\ProjectA\ProjectA.csproj">
<Private>true</Private>
<CopyLocalSatelliteAssemblies>true</CopyLocalSatelliteAssemblies>
</ProjectReference>
There were also a directy link to the compiled dll from the referenced project:
<ItemGroup>
<Reference Include="ProjectA">
<HintPath>..\ProjectA\bin\Debug\netcoreapp3.1\ProjectA.dll</HintPath>
</Reference>
</ItemGroup>
Removing this second reference solved the issue.
One of the reasons to get this annoying yellow triangle is that you are adding a reference to a project twice, meaning:
Reference one: MyProjectOne (which contains already a reference to MyProjectTwo)
Reference two: MyProjectTwo
By deleting the Reference two, the yellow triangle will disappear.
If you're using the newer style Sdk projects add OutputType to the ProjectGroup element with a value of Library in the project you're referencing.
It'll also give you grief if it's in the project you're referencing and it references a project without the setting.
find your .csprojc file and open it.
find your packages path. I fixed my project by this issue.
..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.dll
this .. means vs will find this dll in parent directory.
you should confirm your package path, and your will fixed this issue.
one more simple answer : exit visual studio and re-open it, it has solved for my problem.

Categories

Resources