3

I'm currently trying to open a window with the System.Windows. I have included three assembly references:

<ItemGroup>
  <Reference Include="PresentationFramework">
    <HintPath>C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\PresentationFramework.dll</HintPath>
  </Reference>
  <Reference Include="WindowsBase">
    <HintPath>C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\WindowsBase.dll</HintPath>
  </Reference>
  <Reference Include="PresentationCore">
    <HintPath>C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\PresentationCore.dll</HintPath>
  </Reference>
  </ItemGroup>

Upon attempting to run it with dotnet run, I get the error, error CS7069: Reference to type 'DependencyObject' claims it is defined in 'WindowsBase', but it could not be found. I'm not sure what I'm doing wrong, since I've added an assembly reference to the needed file.

I've tried referencing the respective files in C:\Windows\Microsoft.NET\assembly, but it still didn't allow me to run the program.

Please note I am using Visual Studio Code, and not Visual Studio.

Any help is appreciated, since I'm very new to working with C# outside of a game engine.

2

1 Answer 1

6

So, judging by how you're trying to use dotnet run, you're using a modern version of .NET, not .NET Framework, which is the legacy version. That's good, but it does mean a few things. Those assembly references? They're for .NET Framework 4.7.2, they're not compatible with whatever .NET version you're running right now.

But that's beside the point. In modern .NET, you don't need to reference WPF assemblies by hand in your .csproj file. Since .NET 5, you only need to put <UseWPF>true</UseWPF> in a property group, and the the build tools will do the rest.

Here's an example for .NET 7:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net7.0-windows</TargetFramework> <!-- or whatever version you're using -->
    <OutputType>WinExe</OutputType> <!-- you're probably making an executable -->
    <UseWPF>true</UseWPF> <!-- this right there -->
  </PropertyGroup>

  <!-- other stuff goes here, like package references -->

</Project>

You also need to target a Windows-specific framework moniker (that's the -windows suffix in the TargetFramework), because WPF only works on Windows.

Then, you can just get rid of all those <Reference> elements. You don't need them.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much, I probably would have spent at least another 18 hours looking online for solutions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.