Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
.NET Standard 2.0 Cookbook

You're reading from   .NET Standard 2.0 Cookbook Develop high quality, fast and portable applications by leveraging the power of .NET Standard Library

Arrow left icon
Product type Paperback
Published in May 2018
Publisher
ISBN-13 9781788834667
Length 394 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
 Ismail Ismail
Author Profile Icon Ismail
Ismail
Arrow right icon
View More author details
Toc

Table of Contents (18) Chapters Close

Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
1. Back to Basics FREE CHAPTER 2. Primitives, Collections, LINQ, and More 3. Working with Files 4. Functional Programming 5. XML and Data 6. Exploring Threading 7. Networking 8. To iOS with Xamarin 9. To Android with Xamarin 10. Let’s Fine-Tune Our Library 11. Packaging and Delivery 12. Deploying 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. 

You have been reading a chapter from
.NET Standard 2.0 Cookbook
Published in: May 2018
Publisher:
ISBN-13: 9781788834667
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime
Visually different images