Home > Net >  Trying to get example from Microsoft Docs on WPF to work in local machine
Trying to get example from Microsoft Docs on WPF to work in local machine

Time:09-29

I'd like to get this example running from the Microsoft Docs.

In order to do so I made a new directory called inotifyproperty-changed-example. In this project I generated a WPF solution by running the following commands:

dotnet new wpf
dotnet new sln
dotnet sln add inotifyproperty-changed-example.csproj

Then I removed all XAML and CS files by running rm *.cs; rm*.xaml as it is my understanding the example code is self-contained.

Then I copied the example code in the link to a file called App.cs and changed the namespace to match my own, namely inotifyproperty_changed_example.

The contents of the CSPROJ file are as follows:

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

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <RootNamespace>inotifyproperty_changed_example</RootNamespace>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

</Project>

Unfortunately when I try to run the solution using dotnet run I get errors:

error CS0234: The type or namespace name 'Forms' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?) The type or namespace name 'Form' could not be found (are you missing a using directive or an assembly reference?) ...

I'm really at a loss as to why this won't build. More generally speaking, I am asking this question because I'm unable to reproduce many of the examples provided in the WPF documentation, so I need a place to start.

How can I get the example to work?

CodePudding user response:

You are linking to a Windows Forms example, not a WPF one.

Create the WPF project using the default template and don't remove any files. Then add the DemoCustomer class from the example to the project. You cannot use the Form1 class as it's not WPF. Instead you could modify the MainWindow class as per your requirements.

As far as the INotifyPropertyChanged interface is concerned, there is no difference between WPF and Windows Forms. This interface is not platform specific.

  • Related