How to Interop with Windows Runtime in .NET 4.5 - c#

I see this namespace:
System.Runtime.InteropServices.WindowsRuntime
Which provides interop between .NET and WindowsRuntime.
For instance, you can invoke a method in WindowsRuntime when you create a Metro application, as Metro uses WindowsRuntime, like
Windows.System.UserProfile.UserInformation
But when you create a normal .NET console application or WPF application, you can no longer directly reach WindowsRuntime namespaces such as Windows.System
I wonder if it were possible to invoke WindowsRuntime methods by using interop in the above-mentioned namespace.
A lot of thanks in advance!

A .NET console mode app or a WPF app are not Metro applications. They can only run on the 'regular' version of Windows. The traditional desktop in the case of Windows 8. So can't interop with WinRT, it isn't loaded in the process. Targeting WinRT requires selecting a specific Metro project template when you start your project.

You need to add this to the project file:
<PropertyGroup>
<TargetPlatformVersion>8.1</TargetPlatformVersion>
</PropertyGroup>
For more details, see How to use specific WinRT API from Desktop apps.

Related

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

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.

Using WinUI 3 in a Visual Studio desktop MFC class library project

To extend a third party MFC application I must create a desktop static MFC class library (i.e. dll). Using C++ I successfully completed the logic/backend portion of the project. With the aid of C++/winrt nuget package, I had a wealth of APIs that helped. Note my preferred language is C# with Visual Studio should that be relevant.
For the user interface I would like to completely use WINUI 3 instead of MFC APIs. Currently a desktop win32 WINUI solution must include a packaging project (i.e. MSIX) to get the required APP ID. This seems like a fundamental problem since I must produce a MFC dll and not a full blown application/exe install. If necessary I can wait (Not preferred) for Windows APP SDK 1.0, but I need to confirm this solves my problem and a release is coming.
Alternatively I may need to use WinRT XAML hosting API (XAML islands). This has limitation, but is this still the best/only viable option for WINUI in a C++ MFC desktop unpackaged app?
Basically how can I best utilize WINUI 3 in my MFC class library project?

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.

.net native for Desktop applications

Previously .NET Native Toolchain was only available for Windows Store Apps and only for UWP applications. From what I've read about VS2017 with .NET Native 1.6 some or all of these restrictions may no longer to be in place but the information is a bit confusing, so:
Can I now use .Net native toolchain for an existing C# Desktop (non-Windows Store) application with Visual Studio 2017 (or earlier)?
Does the application have to use UWP or can it also be an existing WPF application?
If it cannot be WPF, could I still create a .Net Native non-GUI assembly that can then be used by a regular JIT WPF application? I suppose I would need to make sure that no reflection is used in either direction but I rather doubt this would be possible at all.
Can I now use .Net native toolchain for an existing C# Desktop (non-Windows Store) application with Visual Studio 2017 (or earlier)?
No, .NET Native applies to UWP apps only.
2) Does the application have to use UWP or can it also be an existing WPF application?
It has to be a UWP app. WPF does not support .NET Native.
3) If it cannot be WPF, could I still create a .Net Native non-GUI assembly that can then be used by a regular JIT WPF application?
No. WPF requires the full .NET Framework with all that it entails.

Can a Cordova plugin make calls to a .Net Framework .dll?? Not WinRT Component (.Net Core)?

I have a .dll that was written to target the .NET Framework. I need to create a Cordova plugin that can make calls to this .dll.
Our Cordova App runs on a Windows 10 Tablet.
Because this .dll targets .NET Framework and not .NetCORE, I cannot take the approach of a WinRT component that's suggested in all of the guidance I've seen.
Is it possible to have a Cordova plugin that makes calls to a .NET Framework .dll and if so can you recommend a correct approach?
Disclaimer: I'm in the weeds with Cordova. I'm learning as I go. An EXAMPLE would be overwhelmingly appreciated.
Thanks
No on both form and function. You'll need to use a .Net library that works with .Net Core for UWP.
Form:
Cordova apps on UWP run in the native JavaScript environment, not in a WebView hosted in a C# app. The only way for a UWP JavaScript app to call .Net functions is via a Windows Runtime Component (WRC).
While you could (in theory) write a version of Cordova for Windows Runtime that uses a .Net hosted WebView I'm not aware of any existing ones. (The Windows Phone 8 versions Hassan Ali linked do use a Silverlight WebBrowserControl, but don't run on Windows 10 tablets).
Function:
Even if you did change the Cordova host to .Net so it could call .Net directly without a WRC that would still run in the UWP context.
UWP apps can use only NetCore, not the full .Net Framework, whether they are direct .Net apps or call .Net inside a WRC.
Apache Cordova is a platform for building native mobile applications
using HTML, CSS and JavaScript
So no, you can't call .NET assemblies directly on the client. You have to put them in a plugin. You can find some samples for Windows Phone here. Another sample in this SO question.

Categories

Resources