How Can I Add Microsoft Windows SDK for .NET in a WPF Project?

MERUN KUMAR MAITY 596 Reputation points
2024-08-23T05:02:16.65+00:00

Windows App SDK

Hi there, I want to add Microsoft Windows SDK in my existing WPF project. Now comes into the story that why I need it? Actually, I have an existing WPF project from my office, where Windows SDK is already been there and that project is working as expected.

But I am unable to add this Microsoft.Windows.SDK.NET.Ref in my new WPF project. Actually, In my project I need to implement this

using Windows.Devices.Display;
using Windows.Devices.Enumeration;

That's why I want to add Microsoft Windows SDK in my project but don't find a proper way to do this. At first I think I should add it through NuGet Packages but that's not the ultimate solution.

I attach an image of my office WPF project where it's beautifully placed inside "Frameworks" sub folder under Dependencies.

I want to implement that specific way, hope you people understand.

If anyone please know how to add, then please help.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,769 questions
Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
783 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,913 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 85,131 Reputation points
    2024-08-23T06:23:49.98+00:00

    I need to implement this using Windows.Devices.Display; using Windows.Devices.Enumeration;

    Those are WinRT classes

    In .NET Core, you must add Microsoft.Windows.CsWinRT package

    and modify project file by adding SDK version, like :

     <TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
    
    
    2 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Hongrui Yu-MSFT 1,765 Reputation points Microsoft Vendor
    2024-08-23T08:02:31.6333333+00:00

    Hi,@MERUN KUMAR MAITY. Welcome to Microsoft Q&A. 

    If you want to use Windows.Devices.Display and Windows.Devices.Enumeration in WPF Application, you could modify TargetFramework.

    
    <TargetFramework>net8.0-windows10.0.22621.0</TargetFramework>
    
    

    When you rebuild the project, you will find that Microsoft.Windows.SDK.NET.Ref has been referenced

     

    At this point, you could program the code to use

    
    using Windows.Devices.Display;
    
    using Windows.Devices.Enumeration;
    
       
    
    public partial class MainWindow : Window
    
        {
    
            public MainWindow()
    
            {
    
                InitializeComponent();
    
                Fun();
    
            }
    
     
    
            public async void Fun()
    
            {
    
                //Handle each display device
    
                var displayDevices = await DeviceInformation.FindAllAsync(DisplayMonitor.GetDeviceSelector());
    
                foreach (var device in displayDevices)
    
                {
    
                    MessageBox.Show(device.Name);
    
                }
    
            }
    
        }
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.