Reference ".NET MAUI App" to ".NET MAUI Class Library" - c#

For my project I need to be able to download the widgets (the View is a DataTemplate in xaml + ViewModel in .cs) from a server and load it during runtime (I use reflection). I am aware this is quite risky way, but this is the requirement.
I did working proof of concept. And now I am implementing a clean version. The problem is that main application is ".NET MAUI App" and the project that would contains the additional widgets I guess should be ".NET MAUI Class Library". In my opinion there is no sense in doing a second project as an app that just contains few classes and few data templates.
The main application contains many useful classes. So I need to project reference it to the additional "widgets" project. But I got an error:
Error NU1201 Project XYZ is not compatible with net6.0 (.NETCoreApp,Version=v6.0). Project XYZ supports:
- net6.0-android31.0 (.NETCoreApp,Version=v6.0)
- net6.0-ios15.4 (.NETCoreApp,Version=v6.0)
- net6.0-maccatalyst15.4 (.NETCoreApp,Version=v6.0)
- net6.0-windows10.0.19041 (.NETCoreApp,Version=v6.0)
Of course I can do a third... library project that would be referenced to main and the one with widgets... But in real the situation is even more complex and I would like to omit further complications...
Is there any way to reference ".NET MAUI App" that is all net6.0-..., to a project that is ".NET MAUI Class Library", so the only net6.0 ?

Related

How to reference .Net 6.0 project from WinUI 3 project?

I have a .NET 6.0 C# class library project that contains platform-independent code (let's call this BusinessLogic). In my same solution, I would like to create a project for a WinUI 3 app that references this class library (let's call this WindowsApp). I would also like to create a class library specific to the Windows platform (so I can access the Windows.Storage namespace from within that class library, for example... let's call this WindowsOS).
I get an error when attempting to set this up. I have tried two techniques:
First technique
Create a .NET 6.0 C# class library WindowsOS.
In WindowsOS project, add reference to BusinessLogic. No problem.
In WindowsOS project, install NuGet packages Microsoft.Windows.SDK.BuildTools and Microsoft.WindowsAppSDK. This gives me an error about numeric comparisons on the target platform, similar to the one described in this GitHub issue. Afterwards, the project becomes unloadable in Visual Studio.
Second technique
Create a Class Library (Universal Windows) project WindowsOS.
In WindowsOS project, add reference to BusinessLogic. This gives me an error immediately, simply refusing to allow the reference to be added.
I suspect there appears to be some compatibility issue going on. I reviewed the Microsoft docs on .NET Standard versions, as well as this helpful StackOverflow question about .NET Core vs .NET Standard class libraries, and from what I can tell UWP may not be capable of referencing .NET class libraries.
My end goal is to create a WinUI 3 desktop app that references these cross-platform class libraries. My common code is contained in these libraries, and I may make an Android app or other platform app in a separate project that also references these same cross-platform class libraries. How do I do this?
EDIT: Here is a screenshot of the error from technique #1:
I figured out the answer. In the Visual Studio project properties (screenshot below), there is a Target OS property. That property defaults to (None).
Given the names of the projects in the question, set the property accordingly:
Set the property value to Windows in the WindowsOS project. This will give access to Windows-platform specific namespaces (such as Windows.Storage). WindowsOS can still have a project reference to BusinessLogic (and any .NET 6.0 C# library) as before.
No change necessary to the BusinessLogic project properties.
The WindowsApp (WinUI 3) project also requires no changes, and can reference BOTH the WindowsOS project (which now has a Target OS of Windows) AND the BusinessLogic project (which still has a Target OS property of (None).
Something to keep in mind: the WindowsApp project and the WindowsOS project will now both have Target OS version and Supported OS version properties. If you set these to different values in each project, you will get compiler warnings about a potential conflict (a user could install the app with a lower version of Windows, but that app then references the library which may require a higher version of Windows than the user has, for example). This does not matter if you are only using APIs supported in BOTH versions of Windows, but to be safe make sure these are consistent between your projects.

Unable to add a reference to project - UWP to Class Library project?

A WPF project and a UWP project share the same solution. Can both projects reference a common project?
"Common" - .NET 6 target framework, class library
"UWP" - targets Windows 10 Creators Update (Build 15063)
"WPF App" - .NET 6 target framework, references "WPF2"
"WPF2" - .NET 6 target framework
The "WPF" project can reference "Common" no problem, but the "UWP" project says "Unable to add a reference to project 'Common'".
Annoyingly, the error message does not give a reason why.
How to use a class from one C# project with another C# project
Common controls for UWP and WPF
Reference a class library from UWP and ASP.NET 5 (solution here)
UWP is not compatible with .NET Core or .NET 6.
If you want to share code between a WPF app and a UWP app, your shared project should target .NET Standard 2.0.
Please refer to the docs for more information about .NET Standard and the supported .NET implementations for each version.

Reference UWP library from .net standard class library

I have a solution with some projects:
client app - UWP project
device sdk - UWP project (because of bluetooth APIs)
client app - ASP.Net Core 1.1 targeting .Net Core 2.0
client app plugins
some projects with shared model and interface classes.
I would like to use .net standard projects as much as possible (for the shared projects and preferably for the plugins) because they are more lightweight and can be edited without unloading them in Visual Studio for example.
Referencing .net standard 1.4 libraries FROM an UWP project goes fine, but referencing an UWP project FROM a .net standard 1.4 project results in an error:
Project DeviceSDK is not compatible with netstandard1.4 (.NETStandard,Version=v1.4).
Project DeviceSDK supports: uap10.0.15063 (UAP,Version=v10.0.15063)
Is this is there any workaround for this?
That's simply impossible. A cross platform library won't be able to reference a platform specific one.
I refactored the interfaces and data models of the device SDK out of that project and create a .net standard DeviceSdk.Core project. This project can then be referenced from application logic projects and the DeviceSdk main project.
So the structure becomes roughly:
ClientApp - UWP project, referencing all projects but the ClientApp.Frontend project
DeviceSdk - UWP project (because of bluetooth APIs), referencing DeviceSdk.Core
DeviceSdk.Core - .net standard 1.4
ClientApp.Frontend - ASP.Net Core 1.1 targeting .Net Core 2.0, referencing ClientApp.Core
ClientApp.Plugins - .net standard 1.4, referencing ClientApp.Core and DeviceSdk.Core
ClientApp.Core (some projects with shared model and interface classes) - .net standard 1.4
.net standard class library can't reference UWP libs. That would break the whole concept of .net Standard.
The idea is that a class library that targets specific .net standard version can only reference limited set of APIs defined by the standard.
For example, .net standard v1.4 only allows API-s listed within this file:
https://github.com/dotnet/standard/blob/master/docs/versions/netstandard1.4_ref.md
This list is frozen and is supposed to remain intact.

What kind of class library works with both Universal and Winform projects?

In visual Studio 2015, in my solution, I have a Logic tier (class library) project that is added to the references of a WinForm project.
No, I am going to add a new Windows Universal project and added the same logic tier (class library) into it.
However, visual studio doesn't allow me to add the class library as a reference to the universal app, I get an error message.
The question is, What kind of class library that can be added to the references of Universal project as well as to the Winform projects withen the same solution?
Update: the message is "Enable to add a reference to project 'project-name' ."
You need to use the Class Library (Portable) choice for the class library project:
Once you add that, you'll have to pick your target platforms:
To be able to use it with WinForms, select the .NET framework matching the target framework you have selected in your WinForms project. If your WinForms targets .NET 4.5, make sure you select that; if you changed it to target .NET 4.6, you are able to select that as the target for your portable library, and so on. Otherwise, you'll get an error saying:
Unable to add a reference to project 'Your library name'. The current
project's target is not one of or compatible with the targets of
Portable Library project 'Your library name'.
Note that you can change these options later, by going into project properties, under Library tab.

Why create an ASP.NET 5 Class Library project?

I'm trying to figure out what an "ASP.NET 5 Class Library" (vNext) C# project has to do with ASP.NET. Why create a project with this template rather than just a regular C# "Class Library" project?
I like the new features, such as project.json file rather than .csproj file etc, but it doesn't seem right to create an "ASP.NET" class library when the project has nothing to do with ASP.NET or IIS etc. It's just a project for the business logic layer. A new WebApi ASP.NET web site will eventually reference this project, but that's not relevant at this point.
Is it just badly named? Should it just be called "vNext Class Library" and not use an icon that looks like a web app?
Why create an ASP.NET 5 Class Library project?
There are a number of benefits of ASP.NET 5 Class Library projects (.kproj) over Class Library projects (.csproj):
ASP.NET 5 class libraries easily support cross-compiling projects to multiple targets, such as aspnet50, aspnetcore50, net45, and various other portable class library variations. This includes rich Visual Studio support for Intellisense to notify you which APIs are available for which targets.
NuGet packages are automatically created, which is an extremely common thing to do with class libraries.
Better productivity when it comes to things like automatically refreshing Solution Explorer when the file system changes. Fewer conflicts in source control when trying to merge conflicting changes in the *.csproj file.
Can be compiled cross-platform (in part because it doesn't depend on MSBuild)
You can reference a *.csproj project from a *.kproj project (this was just made a lot easier with the new preview of Visual Studio 2015), but it was always possible with some manual steps.
Why does the name have "ASP.NET" in it?
As far as the names goes, it's a relic of history that will soon be addressed. The new project type is useful far beyond ASP.NET 5 applications. Expect to see new names in a future preview of Visual Studio:
.NET Console Application (Cross-platform)
.NET Class Library (Cross-platform)
Update 5/13/2015
With the release of Visual Studio 2015 RC you can see the updated project template names:
Class Library (Package)
Console Application (Package)
These use the project.json file and the .NET Execution Environment (DNX) to build, run, and package (into a NuGet package) the project.
These project templates continue to show up in the New Project dialog under the "Web" node, but now also show up in the main "Visual C#" node as well.
This is an interesting observation, the current template will generate a class library compatible with ASP.NET 5 runtime. You don't get that from the normal C# class library.
I filed the following issue for tracking this design question - https://github.com/aspnet/Home/issues/281
From what I understand one benefit is that the end product of ASP.NET 5 Class Library project is a NuGet package (rather than just the .dll assembly).
You can generate the NuGet package by right clicking the project in Visual Studio 2015 and choosing the "Publish..." option. Or you can use "KPM pack" at the command line.
Also, you have the ability to use the ASP.NET 5 Core runtime so that your library can run cross-platform .

Categories

Resources