Skip to content
This repository has been archived by the owner on Dec 29, 2023. It is now read-only.

This package contains the interop wrappers for WinRT-APIs, that depend on CoreWindow, and other interop helpers for WinUI Window (e.g. to set an icon). There are also some interop components that may help from inside an AppContainer like when using UWP.

License

Notifications You must be signed in to change notification settings

ShortDevelopment/WinUI.Interop

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WinUI.Interop

Warning

This package is no longer maintained, as there are better 1st and 3rd party libraries.

This package contains the interop wrappers for WinRT-APIs, that depend on CoreWindow, and other interop helpers for WinUI Window (e.g. to set an icon).
There are also some interop components that may help from inside an AppContainer like when using UWP.

This package is based on the work of AdamBraden/WindowsInteropWrappers.


Examples

WinUI3: window icon

Tip

Use WinUIEx instead

The BuildAction of the icon file has to be set to Embedded resource in the Properties window!

using Microsoft.UI.Xaml;
using WinUI.Interop.NativeWindow;

namespace NAMESPACE
{
    public sealed partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();
            this.SetIcon("NAMESPACE.FILENAME.ico");
        }
    }
}

Win32: share content

Tip

Use DataTransferManagerInterop with CsWinRT instead

using WinUI.Interop.CoreWindow;
IntPtr hwnd = Process.GetCurrentProcess().MainWindowHandle;
DataTransferManager dataTransferManager = DataTransferManagerInterop.GetForWindow(hwnd);
dataTransferManager.DataRequested += DataTransferManager_DataRequested;
private async void DataTransferManager_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
{
    DataRequestDeferral deferral = request.GetDeferral();
    ... // Implementation: https://docs.microsoft.com/en-us/windows/uwp/app-to-app/share-data
    deferral.Complete();
}
DataTransferManagerInterop.ShowShareUIForWindow(Process.GetCurrentProcess().MainWindowHandle);

Win32: UWP dialog

Tip

Use IInitializeWithWindow with CsWinRT instead

TargetFramework >= net5.0

using WinRT;
using WinUI.Interop.CoreWindow;
FileSavePicker picker = new FileSavePicker();
picker.As<IInitializeWithWindow>().Initialize(Process.GetCurrentProcess().MainWindowHandle);
...
StorageFile file = await picker.PickSaveFileAsync();
...

TargetFramework < net5.0

using WinUI.Interop.CoreWindow;
FileSavePicker picker = new FileSavePicker();
(picker as object as IInitializeWithWindow).Initialize(Process.GetCurrentProcess().MainWindowHandle);
...
StorageFile file = await picker.PickSaveFileAsync();
...

RuntimeInformation

Tip

Use ShortDev.Uwp.FullTrust instead

using WinUI.Interop;
RuntimeInformation.IsUWP;
RuntimeInformation.IsPackagedWin32;
RuntimeInformation.IsUnpackagedWin32;

RuntimeInformation.HasCapability("extendedExecutionBackgroundAudio");

UWP: Lowlevel audio control

Tip

Use ActivateAudioInterfaceAsync with CsWin32 instead

using Windows.Media.Devices;
using WinUI.Interop.AppContainer;

IAudioEndpointVolume is not part of this package! You have to implement the audio Interfaces yourself or use implementations from other libraries like naudio! docs.microsoft.com

public IAudioEndpointVolume VolumeManager { get; set; }
async void ...()
{
    string deviceId = MediaDevice.GetDefaultAudioRenderId(AudioDeviceRole.Default);
    VolumeManager = await AudioInterfaceActivator.ActivateAudioInterfaceAsync<IAudioEndpointVolume>(deviceId);
}

UWP: Handle of CoreWindow

Tip

Use ICoreWindowInterop with CsWinRT instead

using System;
using WinUI.Interop.CoreWindow;
IntPtr hWnd = CoreWindowInterop.CoreWindowHwnd;

or

IntPtr hWnd = CoreWindow.GetForCurrentThread().GetHwnd();

or

IntPtr hWnd = Window.Current.GetHwnd();

UWP: Handle of AppWindow

using System;
using WinUI.Interop.AppWindow;
AppWindow window = ...;
IntPtr hWnd = window.GetHwnd();

About

This package contains the interop wrappers for WinRT-APIs, that depend on CoreWindow, and other interop helpers for WinUI Window (e.g. to set an icon). There are also some interop components that may help from inside an AppContainer like when using UWP.

Topics

Resources

License

Stars

Watchers

Forks

Languages

  • C# 100.0%