Book Image

.NET Standard 2.0 Cookbook

By : Fiqri Ismail
Book Image

.NET Standard 2.0 Cookbook

By: Fiqri Ismail

Overview of this book

The .NET Standard is a standard that represents a set of APIs that all .NET platforms have to implement, making it easy for developers to access and use one common library for their development needs. This book begins with a quick refresher, helping you understand the mechanics of the new standard and offering insight into how it works. You’ll explore the core library concepts, such as working with collections, configurations, I/O, security, and multithreading. You’ll explore the iOS and Android libraries of Xamarin and we’ll guide you through creating a .NET Standard 2.0 library, which you’ll use with both Android and iOS applications. In the final chapters, you’ll learn the various debugging and diagnostics tools to deliver quality libraries and create a NuGet package of the .NET Standard 2.0 library. By the end of this book, you’ll be able to expand your current workflow to various .NET flavors and have the essential skills to create a .NET Standard 2.0 library from scratch to package and deliver it to the world.
Table of Contents (18 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
Index

Creating a WPF-based application to use the library 


Now, in this recipe, let's add a Windows Presentation Foundation (WPF)-based application to the solution and use the class library created in a previous recipe. WPF is the shortened name for Windows Presentation Foundation. The purpose of this recipe is to demonstrate how to share a library within the different .NET-based applications. 

Getting ready

For this recipe, you will require the solution and the class library you built in the previous recipe. Open Visual Studio 2017 and prepare for the project. Click Build | Build Solution, or press Ctrl + Shift + B, and the solution should build successfully. Everything's ready for testing our class library. 

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: 

    Control

    Property

    Value

    TextBlock

    Name

    MessageLabel

    TextBlock

    Layout | Width

    498

    TextBlock

    Layout | Height

    93

    TextBlock

    Text | Font

    Bold

    TextBlock

    Text | Font

    Size 14

    TextBlock

    Common | Text

    Press the button to see the message

    Button

    Name

    HelloButton

    Button

    Layout | Width

    276

    Button

    Layout | Height

    60

    Button

    Common | Content

    Say Hello

  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. 

How it works...

Let's have a look at the bits and pieces and how they are bound together. From steps 1 to 7, we have opened an existing solution and added a WPF project to that solution. In steps 8 to 10, we added a control to the WPF main form, from the toolbox. Since this is a WPF application, we went through an additional element; setting up the UI. In step 11, we have set up the UI elements using the properties window. 

In steps 12 to 15, we added a reference to the WPF project. Referencing the library we have created is the most important part. Without referencing, the WPF project is totally unaware of the library.  After referencing the library only, it will available to the WPF project. Step 17 tells the compiler to use the namespace of the library. Now we don't have to call the full namespace of the class inside the library. In step 18, we created a simple variable and stored a name. The next line creates an instance of the HelloWorld class inside the library. Finally, we used the Text property of the WPF TextBlock control to store the value from the SayHello(string name) method.  

In the final steps – 19 to 20, we have executed the code and tested it.