1

We are migrating .net framework 4.7 to .net 5. The project in which we are working is a WPF Project, and we need to use the Dispatcher class for WPF.

In .net framework 4.7 we included using System.Windows.Threading; and the reference WindowsBase, but we cannot do the same in .net 5.

Do you know which is causing this strange behaviour?

Thanks in advance.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <OutputType>Library</OutputType>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <CodeAnalysisRuleSet>..\.sonarlint\ccsharp.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Productive|AnyCPU'">
    <OutputPath>bin\Productive\</OutputPath>
    <DefineConstants>PRODUCTIVE;TRACE</DefineConstants>
    <Optimize>true</Optimize>
    <CodeAnalysisRuleSet>..\.sonarlint\ccsharp.ruleset</CodeAnalysisRuleSet>
    <UseWPF>true</UseWPF>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="C">
      <HintPath>..\Files\C.dll</HintPath>
    </Reference>
  </ItemGroup>
  <ItemGroup>
    <None Include="..\.sonarlint\ccsharp.ruleset">
      <Link>ce-terminalcsharp.ruleset</Link>
    </None>
    <None Include="..\Files\Log4Net.config">
      <Link>Log4Net.config</Link>
    </None>
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="log4net">
      <Version>2.0.8</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
    <PackageReference Include="Microsoft.Diagnostics.Tracing.TraceEvent" Version="2.0.70" />
    <PackageReference Include="Microsoft.Windows.Compatibility" Version="5.0.2" />
    <PackageReference Include="RestSharp">
      <Version>106.6.10</Version>
    </PackageReference>
    <PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
    <PackageReference Include="Microsoft.DotNet.UpgradeAssistant.Extensions.Default.Analyzers" Version="0.2.233001">
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>
  <ItemGroup>
    <AdditionalFiles Include="..\.sonarlint\SonarLint.xml">
      <Link>SonarLint.xml</Link>
    </AdditionalFiles>
  </ItemGroup>
  <ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
  </ItemGroup>
</Project>
rosa.tarraga
  • 145
  • 1
  • 2
  • 10
  • 2
    Did you declare `true` in your csproj file? - https://learn.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props-desktop#usewpf – Rand Random Jul 08 '21 at 11:43
  • Could you provide your project file? – Selvin Jul 08 '21 at 11:44
  • I have the same problem than this ticket: https://stackoverflow.com/questions/68016460/how-to-access-dispatcher-from-a-net-5-winforms-app but in WPF – rosa.tarraga Jul 08 '21 at 11:48
  • why usewpf in a condition? and as mm8 is showing in the answer you are missing -windows in the targetframework – Rand Random Jul 08 '21 at 11:53
  • TargetFramework should be `net5.0-windows` and the first `` should include `true`. See my answer for an example of a valid project file. – mm8 Jul 08 '21 at 11:56

1 Answers1

4

System.Windows.Threading.Dispatcher is certainly available in .NET 5.

If you are creating a class library, the project file should look like this:

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

  <PropertyGroup>
    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

</Project>
mm8
  • 163,881
  • 10
  • 57
  • 88