.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 Chapter2.Collections solution. 
  3. Now, click on the Chapter2.Collections 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. 
  1. Select Windows Classic Desktop and then WPF App (.NET Framework) in the right-hand pane:
  1. In the Name: text box, type Chapter2.Collections.WPFLittleShop as the name of the project and leave the Location: text box as it is:
  1. Click OK.
  2. Now, the Solution Explorer (press Ctrl + Alt + L to open) should look like this:
  1. Now, double-click on the MainWindow.xaml to open the designer view. 
  2. Open the Tool Box by pressing Ctrl + Alt + X and drag and drop two Buttons and two List Boxes to the main window. 
  3. Place them as shown: 
  1.  Now, open the Properties window, or press F4, and change the following properties: 
  2.  After applying the previous properties, the MainWindow should look like this:
  1. Now, in the Solution Explorer (or press Ctrl + Alt + L), expand the Chapter2.Collections.WPFLittleShop project tree. 
  2. Right-click on the References label and select Add Reference.
  3. In the Reference Manager dialog box, expand the Projects node in the left-hand pane. 
  4. Click on Solution
  1. Now, check Chapter2.Collections.CollectionsLib in the right-hand pane:
  1. Click OK.
  2. Double-click on the Get Fruits button. 
  3. You will see the FruitsButton_Click() method. 
  4. Scroll up the code window and add the following using directive at the end of all the using directives: 
      using Chapter2.Collections.CollectionsLib;
  1. Again, scroll down and inside the FruitsButton_Click() method, write the following code: 
      var littleShop = new LittleShop();
var fruits = littleShop.GetFruitsList();

foreach (var fruit in fruits)
{
FruitsList.Items.Add(fruit);
}

FruitsList.Items.Add("--------");
FruitsList.Items.Add($"Item Count: {fruits.Count}");
FruitsList.Items.Add($"Capacity: {fruits.Capacity}");
  1. Switch back to the MainWindow.xaml design view by clicking on its tab or just double-clicking on the MainWindow.xaml label in the Solution Explorer
  2. Select the Get Items button and double-click on it. 
  1. Now type the following code inside the ItemsButton_Click() event method:
      var littleShop = new LittleShop();
var items = littleShop.GetShopItems();

for (int i=0; i<items.Count; i++)
{
ItemsList.Items.Add(items[i]);
}

ItemsList.Items.Add("--------");
ItemsList.Items.Add($"Item Count: {items.Count}");
ItemsList.Items.Add($"Capacity: {items.Capacity}");
  1. Let's press F5 and debug the code.
  2. Press the Get Fruits and Get Items buttons.
  3. You should see output like this: