How to use UWP functionality from a .Net 6 WPF application? - c#

I have a WPF application built for .Net Framework.
It references a UWP library for doing things like using Start-Tiles etc.
I'm now converting this WPF application to .Net 6, but can't seem to find a way to call UWP methods from it.
How can this be achieved?

An application that targets and runs on the .NET Framework cannot consume a library that is compiled for UWP, like for example a library that was created using the Class Library (Universal Windows) template in Visual Studio.
You may use Windows Runtime APIs in your .NET Framework app though but that's another thing.
If you want to share code between a .NET Framework application and a UWP app, your project that contains the shared code should target .NET Standard. It can then be consumed from all applications that target platforms that is compatible with the version of the .NET Standard you are targeting in your class library.

Related

Adding Windows.Services.Store lib to WPF

Currently I'm working on an WPF app that will be packaged as UWP app so I can publish it in Windows Store. I'm following this video in making this happened (https://www.youtube.com/watch?v=fJkbYPyd08w)
Now, I want my application to implement Subscription Base service. So I found this link: https://learn.microsoft.com/en-us/windows/uwp/monetize/enable-subscription-add-ons-for-your-app
But it seems that I can't add UWP Windows.Services.Store Namespace library into my project. I try to seek in Nuget without success.
Where can I get this library? How to use it in WPF?
Thanks
You only need to add the Microsoft.Windows.SDK.Contracts NuGet package to your project. For details, see this blog post.
tl;dr: WPF runs on .NET Framework, UWP on .NET Core. .NET Framework and .NET Core cannot directly share code with each other, for this you require .NET Standard class libraries.
I see that you're quite new to C#, so I'll try to keep it short and simple.
.NET is a lot more than people think it is, what most people mean by .NET is the .NET Framework. What it actually is, is the Common Language Infrastructure (CLI) and the Common Intermediate Language (Historically MSIL now CIL), on top of which you have the .NET Standard, this is a base collection of classes that all .NET implementations must have and is the way of sharing code between different Frameworks.
Another layer up you get the Frameworks. .NET Framework being the most popular but also the oldest, it features ASP.NET, WinForms, WPF and more.
The .NET Core is the Framework form Microsoft featuring Cross-Platform support, so your programs can run on Windows, Linux and OS X, UWP runs on the .NET Core.
Here is a great diagram visualizing this
Note: In the upcoming release of the .NET Core 3.0, you will be able to create WPF applications running on windows (WPF will not be supported on Linux or OS X)

Can I use a net core library in a wpf application?

I am trying to develop a very simple WCF application. The WCF server will be in a windows 10 computer and I would like to consume this service with a WPF application in a windows 10 computer and also I would like to have another application for android, using Xamarin.
In visual studio I see that I can create a Net Core library and also I see that Net core is compatible with WCF, I can see in this web.
Also I know that Net Core can be run in Windows, Linux and Mac.
So I am wondering if I could develop a Net core Library to develop the client so I could use it in my WPF and Android applications.
My doubt it is when I add a reference in my WPF application to the net Core library project, I see a yellow triangle, a warning, so I guess that WPF can't use the Net Core library.
So I was thinking that perhaps, for the WCF layer, I would have to create a Net Standard project that can be use by the WPF application and the Android application. But I would like to know if this is the unique way or there are some way that a WPF application can use a Net Core library without needed to use a middle project like Net Standard.
For the server, I was thinking to use Net Core too, because this project will not be referenced by any other project and Net Core can be used in windows.
Well, you should create a .Net Standard Class Library (So, not .Net Core), put in your WCF clientside code in there and you can reference it from a .Net Core or .Net Full project. a .Net Standard Class Library will give you most flexibility so it is the recommended project type for shared libraries.
But according to the docs, see last line you will need to use .Net Full for the server side:
WCF service applications should still be created with the full .NET Framework version.
But I would like to know if this is the unique way or there are some way that a WPF application can use a Net Core library without needed to use a middle project like Net Standard.
No. A WPF application targeting .NET Framework cannot reference a .NET Core project.
.NET Core 3, which is currently in preview, does however support WPF which means that you can reference a .NET Core project from a WPF application that targets .NET Core. But an application targeting .NET Framework cannot reference a library that targets .NET Core and vice versa.
You should prefer to target .NET Standard in your class library projects since these can then be consumed from applications targeting either .NET Framework or .NET Core.
WPF / Winforms applications are not supported on .Net Core platform yet.
Present:
By yet, I mean .Net core 2.x versions.
So, if your application is NOT based on 2.x standard of .net core, you will NOT be able to reference the .net core assembly.
Future:
.Net core 3 preview is available and Winforms / WPF applications are now available on the .Net core platform.
Hope this helps.

How to use C# dll in UWP

I have a C# dll compiled in .net framework 2.0 and the library is not been updated by the vendor till now. In one of my desktop application C# i was using that and when i migrated the application to WPF, the same libraries where referred and working fine. Now, the application required new diversion, as it should be upgraded to UWP and the same dll i want to make use in that also. What is the best and easiest methods available in UWP to import.Net Framework 2.0 dll ?
Regards,
Lal
You can create a .NET Standard 2.0 library project that can reference the legacy library and you can then reference this .NET Standard 2.0 project from you UWP app.
In the .NET Standard library, you can write custom types that access the legacy library and provide a "middleware layer" between the UWP app and the legacy library.
What is the best and easiest methods available in UWP to import.Net Framework 2.0 dll ?
You can not direct consume any full .NET Class Library from UWP app, there is some security restrictions. UWP project can reference libraries such as Universal Windows Class Library,.NET Standard library, Windows Runtime Component and so on.
For solving this, since your app is for desktop devices and you already have a WPF app, I recommend you to convert the WPF app to UWP app by using Desktop Bridge. Or you could try to convert the old dll into one of above the libraries that can be referenced by UWP app. If you do only want to consume the .Net framework dll, you may try to create COM Proxy Stubs and work with them as communication layer, here is the official sample.
One solution for this is to make a windows service and use that dll in service. You can reference that service from UWP application in order to make all the API calls.

Can the same class library be used by a UWP app and MVC web application?

Is there a type of c# class library that can be used/referenced by both a UWP (Universal Windows) app and a ASP.NET MVC web application?
I currently have an MVC web application that references a bog standard class library.
When I try to add a reference to that class library from my UWP or Background Application (IoT) projects it complains. I'm sure they would rather have references to Class Library (Universal Windows) project types.
Ideally i'd like a class library that can be referenced by all these project types. I presume this is not possible yet, but I thought i'd ask the question.
Is it possible? I'd rather avoid code duplication.
Do I need some sort of hacky 'include files from another folder' workaround?
Note: I have no intention of moving the web app to ASP.NET core.
I think a portable class library that targets the .NET Framework and UWP should do the job - as long as you don't want to put anything platform specific in it.
2017/18/19 UPDATE:
Creating Portable Class Libraries is now discouraged by Microsoft.
The current preferred method of sharing code between projects targeting different platforms is .NET Standard.
Take a look at the implementation support table shown on the page above. If for example your ASP.NET project is on .NET Framework 4.6.1 or above (preferably on 4.7.1-2), and you are targeting Windows 10.0.16299 with your UWP app, you can make your DLL target .NET Standard 2.0. You can simply reference that class library in both of your projects, just like you'd reference any other library.

using .net 4 projects with 4.5 target?

I am new to .NET and c#.
There are so many good examples out there made for .NET 4.0, but when I do NUGET it complains that the target .net version mismatches.
I am developing for Windows 8 and Windows Phone 8, so they use .NET 4.5.
Is there any way to just convert the old projects and force them to use .NET 4.5?
Or do I need to wait for the author to update his project?
You can develop for Windows 8 with .NET 4.0, that's not a problem. Also you can load .NET 4.0 projects into Visual Studio 2012 and then change the properties of the project to .NET 4.5 if you want to. Also all APIs should be usable by themselves without modification.
So do you have a project that is for .NET 4.5 and you're trying to add .NET 4.0 stuff from NUGET or what?
Naturally WinRT projects are totally different from "normal" .NET projects, so they will need converting.
If you're developing Windows Store or phone applications, you'll need updated projects, as it's a separate runtime. It's "4.5", but it's not the standard 4.5 runtime. If you're developing desktop applications using .NET 4.5, you should be able to use .NET 4 (and earlier) assemblies without issue.
The best option here, when possible, is to have the dependencies by a Portable Class Library usable by all target platforms, but this would require the other projects to be designed this way.
Depends on what references you use in your code, if they are untouched by 4.0 to 4.5 conversation then you are in luck, however if that is not case you will need author to update code of project to match 4.5.

Categories

Resources