Is there a way to use that UWP's Windows namespace from Winform?
Asked Answered
Y

2

1

I'm using VS2022, .NET7.0.

I'm trying to implement a function that automatically connects the SerialPort using USB's vid and pid.

I found that it was easier to implement by refering to this article rather than this article.

But, I can't use the Windows namespace in Winform.

I tried the Nuget\Install-Package Windows using PM, but it failed.

I add the <TargetPlatformVersion>8.0</TargetPlatformVersion> in .csproj of project and looked at the References Manager, but it hasn't changed.

Tried reinstalling Windows SDK 10, same thing.

How to solve this problem?

Yeasty answered 2/2, 2023 at 0:1 Comment(1)
U
1

For a .NET 7, you do not need the NugetPackage, you need to edit the properties of the Project, and set the Target OS to 10.0.17763.0 or above. You can also modify the project file like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net7.0-windows10.0.17763.0</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWindowsForms>true</UseWindowsForms>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

</Project> 
Upperclassman answered 2/2, 2023 at 1:53 Comment(3)
you're right. It actually doesn't compile and I've been looking for a solution for the past hour.Yeasty
The NuGet package is for .NET 4.8.Upperclassman
.NET 5, 6, and 7 use the TargetFramework solution. .NET 4.X, uses the NuGet package. You can take a look at the example here MediaCapture API in WPF or Windows Forms or download code from repository.Upperclassman
Y
1

As per @radian's comment, I solved it like this:

Opened NuGet Package Management and installed Microsoft.Windows.SDK.Contracts.

I can now add Windows namespace normally.


EDIT

When I do the above method, there is a problem that it does not compile.

To fix this, you need to act on this post or the accepted answer.

Yeasty answered 2/2, 2023 at 1:6 Comment(0)
U
1

For a .NET 7, you do not need the NugetPackage, you need to edit the properties of the Project, and set the Target OS to 10.0.17763.0 or above. You can also modify the project file like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net7.0-windows10.0.17763.0</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWindowsForms>true</UseWindowsForms>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

</Project> 
Upperclassman answered 2/2, 2023 at 1:53 Comment(3)
you're right. It actually doesn't compile and I've been looking for a solution for the past hour.Yeasty
The NuGet package is for .NET 4.8.Upperclassman
.NET 5, 6, and 7 use the TargetFramework solution. .NET 4.X, uses the NuGet package. You can take a look at the example here MediaCapture API in WPF or Windows Forms or download code from repository.Upperclassman

© 2022 - 2024 — McMap. All rights reserved.