.NET Standard 2.0 Cookbook
上QQ阅读APP看书,第一时间看更新

How to do it...

  1. Open Visual Studio 2017.
  2. Now open the solution from the previous recipe. Click File | Open | Open Project/Solution, or press Ctrl + Shift + O, and select the Chapter1.Library solution. 
  3. Now click on the Chapter1.Library solution label. Click File | Add | New Project.
  4. In the Add New Project template dialog box, expand the Visual C# node in the left-hand pane.
  5. Select Windows Classic Desktop and select WPF App (.NET Framework) in the right template pane. 
  1. Now, in the Name: text box, type a name for the new project. Let's type Chapter1.Library.HelloWPF and leave the Location: as it is and the defaults as well. Click OK to create the new project.
  1. Now the Solution Explorer (if it's not visible, press Ctrl + Alt + L) should look like this: 
  1. Now click on the MainWindow.xaml tab and make sure you are in the Design mode. 
  2. Now, drag and drop a Button and a TextBlock from the tool box (to view the tool box, press Ctrl + Alt + X). You can find these components under Common WPF Controls
  3. The main window should look like this:
  1. Let's name our controls and change some properties as follows: 
  1. Let's add our class library as a reference to the WPF project we have just created. Expand the Chapter1.Library.HelloWPF project node and expand the References node in the Solution Explorer (if you don't see the Solution Explorer press Ctrl + Alt + L).
  2. Right-click on the References label and select Add Reference....
  3. Under the Reference Manager dialog box, click on the Projects label in the left-hand pane. In the middle pane, check the Chapter1.Library.HelloLib project:
  1. Click OK.
  2. In the MainWindow.xaml tab, double-click on the SayHello button. 
  3. In the MainWindow.xamal.cs tab, scroll up till you see the using code block. Add this code as the last line of the using code block: 
      using Chapter1.Library.HelloLib;
  1. Now scroll down till you reach the HelloButton_Click method. Type the following code block in between the curly brackets of the HelloButton_Click method:
      var yourName = "Fiqri Ismail";
var helloMessage = new HelloWorld();

MessageLabel.Text = helloMessage.SayHello(yourName);
  1. Now we are ready to test. Press F5 to debug our code: 
  1. Click on the Say Hello button to see the message from the class library:
  1. Congratulations!!! You have just used a library created with a WPF application.