導覽列清單

2024年9月22日 星期日

C# 解決無法引入System.windows.Media

 

在WPF無法引用System.windows.Media。

 一開始往System.windows.Media方向找,但找不到解決方式,後來往Presentionframework dll,終於找到解決方法。



將專案檔設定加入

      <UseWPF>true</UseWPF>
      <!-- and/or -->
      <UseWindowsForms>true</UseWindowsForms>

 

程式碼

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

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

      <UseWPF>true</UseWPF>
      <!-- and/or -->
      <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>

</Project>

 

參考stackoverflaw上的解決方式:https://stackoverflow.com/questions/77627454/why-is-it-not-possible-to-reference-presentationcore-dll-without-usewpftrue-u

參考msdn說明:https://learn.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props-desktop#enable-net-desktop-sdk