找不到 System.Windows.Media 命名空间? ?

我正在使用一个来自第三方 API 的对象,它的属性类型为 System.Windows.Media.ImageSource,但我似乎找不到 System。窗户。任何媒体名称空间。如果我试图添加一个引用到我的项目,我没有看到 System.Windows.Media在选项列表中。我的项目也是针对。净收入3.5。

为了能够访问这个名称空间,我还需要做些什么?

126425 次浏览

The System.Windows.Media.Imaging namespace is part of PresentationCore.dll (if you are using Visual Studio 2008 then the WPF application template will automatically add this reference). Note that this namespace is not a direct wrapping of the WIC library, although a large proportion of the more common uses are still available and it is relatively obvious how these map to the WIC versions. For more information on the classes in this namespace check out

http://msdn2.microsoft.com/en-us/library/system.windows.media.imaging.aspx

You should add reference to PresentationCore.dll.

Add PresentationCore.dll to your references. This dll url in my pc - C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\PresentationCore.dll

For Visual Studio 2017

Find "References" in Solution explorer

Right click "References"

Choose "Add Reference..."

Find "Presentation.Core" list and check checkbox

Click OK

You can add PresentationCore.dll more conveniently by editing the project file. Add the following code into your csproj file:

<ItemGroup>
<FrameworkReference Include="Microsoft.WindowsDesktop.App" />
</ItemGroup>

In your solution explorer, you now should see this framework listed, now. With that, you then can also refer to the classes provided by PresentationCore.dll.

I my case I needed to specify Platforms tag - for some reason it did not work otherwise.

  <PropertyGroup>
<!-- Must be here for this example, otherwise 'using System.Windows.Media.Media3D' does not work for intellisense -->
<Platforms>x64</Platforms>
</PropertyGroup>

Visual studio 2019 v16.9.1.

I found an answer on CodeProject which worked for me.

Open your project file, the *.csproj file so you can edit it as a text file. If like me you were targeting net6.0 you'll see something like this:

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


<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputType>WinExe</OutputType>
</PropertyGroup>


</Project>

Now, instead of net6.0 you need to target net6.0-windows and you also have to set the UseWpf-flag. Then your *.csproj file should like something like this:

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


<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWpf>true</UseWpf>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputType>WinExe</OutputType>
</PropertyGroup>


</Project>