Share Library between UWP App and .NET Core - c#

What is the best practice to share a library (also C# project) between Windows UWP app and a .NET Core web app?
I have the following solution setup starting from scratch:
Solution
UWP App
Lib (With functionality I want to use in both other projects.)
.NET Core WebApp (2.2 or maybe 3.0)
As far as I could figure out, the only solution is, to create two projects for the lib, which share the same .cs files. But this feels a bit like a workaround and I am looking for a "clean(er)" way.

You can share a .NET Standard 2.0 library between those projects, see Microsoft's documentation.
Note, that you cannot use any classes specific to .NET Core or the UWP in such a project.

Related

Referencing .Net Core project from .NET MAUI

I have 2 projects in my solution - .Net Maui(GUI) and ASP.Net Core Web API(API)
After adding a project reference to my GUI I get the following errors:
And I can find very little information on that, although I'm probably looking in wrong places. Can you give a hand with that?
Edit:
To complete/expand my question, I'm looking for a way to structer my solution a little bit better, I don't want to have everything in .NET Maui project.
I was following this tutorial https://www.youtube.com/watch?v=LrZwd-f0M4I
The author does create .NET Core project for API and Db connection
(EF Core) and then he uses the Api in .NET Maui
What am I missing?

adding a web app project to my solution, and the other projects in the solution already use .net framework, can I use .net core for the new project?

Ive build out a service as a console application, it exists across 2 different projects in the solution, both are in .NET framework. I am wanting to add in an api level to this and am wondering if I should continue to use .NET framework for the web application project, or if I can use .NET Core without messing anything up.
It all depends on the APIs you use in your application. If you only use APIs included in .NET Standard then no problem to use them in an application under .NET 6. Otherwise I join Kirk Woll to migrate your code under .NET 6 especially since the unification of the platform. I think it will remain only .NET 6 aka .NET core in the future.

Which type of Class Library project should I use for .NET Core + Xamarin Android App?

I am planning to build a Mobile app in Xamarin Android & it's API in ASP.NET Core. Now I will also create few Class Library projects for Services, Repository & DTOs.
Now, there are two types of Class Library projects.
.NET Core
.NET Standard
Now, My question is that, which project will be supported by Xamarin Android app?
If it is .NET Standard Class Library, then will it work with .NET Core Web API Project?
I think .NET Standard would be a good choice if the library is going to be used for mobile app and/or other types of app.

.net core cross platform desktop app

I'd like to develop a cross platform desktop app by using .net Core. The app needs to be executed on linux, mac os, and windows. For that purpose, should i create a console app and put below lines in settings xml?
<PropertyGroup>
<RuntimeIdentifiers>win10-x64;osx.10.12-x64;debian.8-x64</RuntimeIdentifiers>
</PropertyGroup>
Will it be sufficient? If so, should i write all platforms and separate by comma e.g.
win7-x32;win10-x64;...
If it is not the answer, how can i generate cross platform desktop app with .net Core?
As the other answer alludes to, .NET Core itself is cross-platform, but it doesn't offer any GUI solution. You can run console/terminal applications, as well as web applications, but not desktop applications.
As of right now, there is no official Microsoft tech that solves a multi-platform GUI. The two that I know of are Avalonia and Eto.Forms. Both have their pros and cons, and both are kinda buggy. Not in the sense that it's unusable, but in the sense that it's evolving tech, don't expect them to blow your mind right off the bat.
Guides to get you started :
Avalonia - https://docs.avaloniaui.net/
Eto.Forms - https://dotnetcoretutorials.com/2018/03/19/cross-platform-winforms-kinda/
1) Console apps in .NET core are already cross-platform.
2) For those working with a GUI, .NET core finally has a cross-platform GUI option officially supported by Microsoft called .NET MAUI
This builds on the same APIs as Xamarian Forms.
Official .NET MAUI Github Repo
If I understand your question correctly, it could be devided into two questions.
1) Howto create a cross platform .net core app
2) Howto create a cross platform .net core UI app
To answer the second (2) question: It's not possible to create cross platform apps using only .net core and microsoft libraries. What you need is a cross platform UI library with binding for all of your your target platforms. The first search result I get is Avalonia https://github.com/AvaloniaUI/Avalonia
I doubt it will be an easy task to get it working as all the alternatives seems to be experimental and/or beta. But you can certainly prove me wrong.
Good luck!
You can try electron.js instead . At present its quite difficult to build a cross platform desktop application only with .NET Core
You have many solutions for making a cross platform application with .NET Core:
Electron.NET
Avalonia
...
If you want detailed information about how to do that you should check a dedicated book who provide project samples for .NET Core 3 Here . With that you will find the most adapted UI for your app.

Can I use PCL library in standard .Net application?

I have PCL Library and I want to add it to standard (.net 4.6) C# console application. Everything is fine as long as I don't use any PCL specific classes inside the library. And if I do, I get an error "unsupported PCL profile". This error is not googlable. But the same library works fine in UWP application. I am searching for a solution or official explanation why I can't use PCL in non UWP application.
Yes you can. PCL is basically intersection of available API's across different platforms. The disadvantage is that the more target platforms you choose the smaller is the intersection:
Another disadvantage of PCL is that it generates separate assembly for each platform.
That's why Microsoft comes with .NET Standard - a replacement of PCL that uses different approach.
Think about .NET Standard as an interface, that defines set of API's. Then the platforms like .NET Framework, .NET Core, Xamarin.iOS, Xamarin.Android will implement the .NET Standard.
interface NetStandard1_0 {
}
interface NetStandard1_1 : NetStandard1_0{
}
interface NetStandard1_2 : NetStandard1_1{
}
net46: NetStandard1_6 {
}
dnxcode46: NetStandard1_6 {
}
As a result, you won't target specific platforms, but a version of .NET Standard instead. When your library targets .NET Standard, it can be used in any platform that implements the .NET Standards. Another advantage is you wont need separate assemblies for different platforms anymore. There will be single assembly that runs just everywhere.
However, I recommend you to wait until April 2017 when .NET Standrard 2.0 should be released. Microsoft promised that all platforms (.NET Framework, .NET Core, Xamarin.iOS, Xamarin.Android) will support this version of .NET Standard and it will have official support in Visual Studio. Also, Visual Studio projects using project.json will be converted to .csproj, so all Visual Studio projects will use the same format and it will solve a lot of compatibility issues. Cleanning of the mess that appeared in .NET last years was absolutelly necessary
Sure you can.
Just add .NET 4.6 to selected platforms:
It's appears at time when you create PCL.
More information here:
Cross-Platform Development with the Portable Class Library
Or you can change platforms in existing PCL. Just go to properties page and you will see:
Here is a good blog post about how to call UWP API from desktop App:
How to call UWP APIs from a desktop VB/C# app

Categories