.NET Core – Create UWP App ”; Previous Next In this chapter, we will discuss how to create a UWP application using .NET Core. UWP is also known as Windows 10 UWP application. This application does not run on previous versions of Windows but will only run on future version of Windows. Following are a few exceptions where UWP will run smoothly. If you want to run it locally you must have Windows 10, you can also develop on Windows 8 and then you will need to run it on Emulator, but it is encouraged to use Windows 10. For UWP application you will also need Windows 10 SDK. Let us open Visual Studio 2015 setup and then modify Visual Studio. On select features page, scroll down and you will see Universal Windows App Development Tools, check that option as shown below. Here you can see the different versions of SDK and the latest update on Tools as well, click Next. Now, click the Install button. Once the installation is finished, you will need to restart your system. Let us now implement the UWP by following these steps. First, launch Visual Studio 2015. Click on the File menu and select New → Project; a New Project dialog will show up. You can see the different types of templates on the left pane of the dialog box. In the left pane, you can see the tree view, now select Universal template from Templates → Visual C# → Windows. From the center pane, select the Blank App (Universal Windows) template. Give a name to the project by typing UWPFirstApp in the Name field and click OK. The target version/minimum version dialog appears. The default settings are fine for this tutorial, so select OK to create the project. Here, we have a single project which can target all Windows 10 Devices, and you will notice that both .NET Core and UWP are simplification of multi-targeting. When a new project opens, its files are displayed on the right hand side of the Solution Explorer pane. You may need to choose the Solution Explorer tab instead of the Properties tab to see your files. Although the Blank App (Universal Window) is a minimal template, it still contains a lot of files. These files are essential to all UWP apps using C#. Every project that you create in Visual Studio contains the files. To see the running example, let us open MainPage.XAML and add the following code. <Page x:Class = “UWPFirstApp.MainPage” xmlns = “http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x = “http://schemas.microsoft.com/winfx/2006/xaml” xmlns:local = “using:UWPFirstApp” xmlns:d = “http://schemas.microsoft.com/expression/blend/2008” xmlns:mc = “http://schemas.openxmlformats.org/markup-compatibility/2006” mc:Ignorable = “d”> <Grid Background = “{ThemeResource ApplicationPageBackgroundThemeBrush}”> <StackPanel HorizontalAlignment = “Center”> <TextBlock Text = “Hello, world!” Margin = “20” Width = “200” HorizontalAlignment = “Left”/> <TextBlock Text = “Write your name.” Margin = “20” Width = “200” HorizontalAlignment = “Left”/> <TextBox x:Name = “txtbox” Width = “280” Margin = “20” HorizontalAlignment = “Left”/> <Button x:Name = “button” Content = “Click Me” Margin = “20” Click = “button_Click”/> <TextBlock x:Name = “txtblock” HorizontalAlignment = “Left” Margin = “20”/> </StackPanel> </Grid> </Page> Below is the click event of button in C#. using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; // The Blank Page item template is documented at // http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 namespace UWPHellowWorld { /// <summary> /// An empty page that can be used on its own or navigated to within a Frame. /// </summary> public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); } private void button_Click(object sender, RoutedEventArgs e) { if (txtbox.Text != “”) txtblock.Text = “Hello: ” + txtbox.Text; else txtblock.Text = “You have not write your name”; } } } Let us now run the above code on the local machine and you will see the following window. Now type any name in the text box and press the Click Me button. Print Page Previous Next Advertisements ”;
Category: dotnet Core
.NET Core – Home
.NET Core Tutorial PDF Version Quick Guide Resources Job Search Discussion .NET Core is the latest general purpose development platform maintained by Microsoft. It works across different platforms and has been redesigned in a way that makes .NET fast, flexible and modern. .NET Core happens to be one of the major contributions by Microsoft. Developers can now build Android, iOS, Linux, Mac, and Windows applications with .NET, all in Open Source. Audience This tutorial is designed for software programmers who want to learn the basics of .NET Core. Prerequisites You should have a basic understanding of Computer Programming terminologies. A basic understanding of any of the programming languages is a plus. Print Page Previous Next Advertisements ”;
.NET Core – SDK
.NET Core – SDK ”; Previous Next In this chapter, we will understand the upcoming features in .NET Core. We will start with the .NET command line tools by opening the following Url in browser https://github.com/dotnet/cli To know more about the progress, you can download the latest version of .NET Core SDK by scrolling down and you will see the Installer and Binaries section. You can see the latest version of preview tools for different operating systems, let us select the Installer as per your operating system. We are working on preview 1 of .NET Core 2.0. Let us now look at our current tooling by opening the command prompt and execute the following command. dotnet –info You will see information about the currently installed version of .NET Command Line Tools on your system as shown below. You can see that currently we have preview 2 tooling. Let us now run the following command to see about the new command. dotnet help new For new command language of project, you can select like C# and F# and the type of project, etc. Let us now see the changes in the latest version of .NET Core. Once the installer is downloaded, double-click on it to install it. Click on Install. The following screenshot shows the installation process. It will start the installation process. One the installation is finished, Close this dialog. Open the command prompt and execute the following command. dotnet –info You will see information of currently installed version of .NET Command Line Tools on your system as shown below. You can now see that we have preview1 tooling of .NET Core 2. Let us now run the following code in the command prompt to see about the new command in .NET Core 2 preview1. dotnet help new The command helps you download packages as well to the package cache. The command opens the following webpage which contains information about the new command in .NET Core 2 preview1. Let us scroll down, you can now see that we can create the .NET Core application with more templates. We can now create mstest, web, mvc and webapi projects as well using the command line. Print Page Previous Next Advertisements ”;
.NET Core – MSBuild and project.json ”; Previous Next The .NET Core has decided to drop project.json and go back to MSBuild and *.csproj. This is something that’s already happened in the just released .Net Core 2.0 preview1 tooling. This is fairly disappointing, because the project.json was a breath of fresh air. However, it is understandable and have many advantages as well. Let us now discuss the advantages that the change brings in − It would make the transition of the existing Visual Studio solutions to .NET Core straightforward. It is a huge change and it will also enable leveraging existing investment in CI/RM based around MSBuild. During build in MSBuild, we can think of incremental compilation, resolving buildtime dependencies, configuration management, etc. A lot of work is required to ship dotnet cli on time, because it is no longer just about ASP.NET Core, but also console apps, UWP apps, etc. Following are the changes in MSBuild and *.csproj − Project.json file (*.xproj) will be replaced by MSBuild (*.csproj). Features in project.json will start getting merged back into the the *.csproj. It is not yet clear what they are going to do about the packages list, but it was mentioned they might keep it as json under nuget.json or merge it into the *.csproj. Supposedly that transition should be smooth and potentially automatic if using Visual Studio. Advantages of MSBuild MSBuild is open source and available on GitHub and is bound to become fully crossplatform. MSBuild will dramatically simplify and trim the structure of the *.csproj. Microsoft is also introducing a new project system which will enable a lot of scenarios without the need for Visual Studio and the details are given on the this Url https://github.com/dotnet/roslyn-project-system/. The goal is that even with the MSBuild setup, working with builds and project will be as seamless in Visual Studio IDE as outside of it. MSBuild vs project.json Let us now create a new console project with .NET Core preview2 tooling by executing the following command. dotnet new -t console To see all the files created within this project, run the dir command. You can see that two files are created, Program.cs and project.json file. Let us now create a console app with .NET Core 2 preview1 tooling by executing the following command. dotnet new console To see all the files created within this project, run the dir command. You can see that three files are created, Program.cs, NuGet.config and MSBuild.csproj instead of the project.json file. Let us now compare project.json and MSBuild.csproj files side by side. To the left, we have the file in json format while on the right, the file is in XML format. You can see that in the project.json file, inside the dependencies section, there is netcoreapp1.0, while in MSBuild.csproj file, you will see the netcoreapp2.0. Print Page Previous Next Advertisements ”;
Adding References to Library
.NET Core – Adding References to Library ”; Previous Next In this chapter, we will discuss how to add references to your library. Adding references to library is like adding references to your other projects, like console project and UWP project. You can now see that the PCL project has some references by default. You can also add other references as per your application need. In the PCL library, you can also see the project.json file. { “supports”: {}, “dependencies”: { “NETStandard.Library”: “1.6.0”, “Microsoft.NETCore.Portable.Compatibility”: “1.0.1” }, “frameworks”: { “netstandard1.3”: {} } } One method of adding references to your library is by typing it directly in the project.json file. As you can see that we have added some references under the dependencies section as shown in the following code. { “supports”: {}, “dependencies”: { “NETStandard.Library”: “1.6.0”, “Microsoft.NETCore.Portable.Compatibility”: “1.0.1”, “System.Runtime.Serialization.Json”: “4.0.3”, “Microsoft.EntityFrameworkCore”: “1.1.0” }, “frameworks”: { “netstandard1.3″: {} } } Let us now save this file and you will see that references are added to your library now. The other method of adding references to your library is the NuGet Package Manager. Let us now right-click on the StringLibrary (Portable) project and select Mange NuGet Packages… On the Browse tab, you can search any NuGet package; let us say we want to add “System.Runtime.Serialization.Primitives” package. Click the Install button, which will display the following screen. Now, click the OK button. Finally, click the I Accept button to start installation of this NuGet package. Once installation is finished, then you will see that the “System.Runtime.Serialization.Primitives” NuGet package is added to your library. Print Page Previous Next Advertisements ”;
.NET Core – Useful Resources
.NET Core – Useful Resources ”; Previous Next The following resources contain additional information on .NET Core. Please use them to get more in-depth knowledge on this topic. Useful Video Courses Real-world App with ASP.NET CORE 3.1 MVC & MongoDB (NoSQL) Best Seller 41 Lectures 2.5 hours University Code More Detail GraphQL Course with .Net Core For Absolute Beginners 54 Lectures 3 hours Asfend Yar More Detail ASP.NET Core API. From scratch to Master + Azure deployment 58 Lectures 5 hours Jakub Kozera More Detail Learn Parallel Programming with c# and .NET Core 5 Best Seller 23 Lectures 2.5 hours V.D More Detail React JS and .NET Core Web API Full Stack Master Course 21 Lectures 3.5 hours Vinay Kumar More Detail Vue JS and .NET Core Web API Full Stack Web Development Master Course 21 Lectures 4 hours Vinay Kumar More Detail Print Page Previous Next Advertisements ”;
Sharing .NET Core Libraries
.NET Core – Sharing Libraries ”; Previous Next In this chapter, we will discuss how to share your library as NuGet Package so that it can be consumed within another project. Creating a package starts with the code you want to package and share with others, either through the public nuget.org gallery or a private gallery within your organization. The package can also include additional files such as a readme that is displayed when the package is installed, and can include transformations to certain project files. Let us now consider a simple example in which we will create a NuGet package from our library. To do so, open the command prompt and go to the folder where the project.json file of your library project is located. Let us now run the following command. dotnet help At the end, you can see different commands like new, restore and build, etc. The last command is pack; this will create a NuGet package. Let us now execute the following command. dotnet pack You can now see that the NuGet packages are produced in the bin folder; let us open the binDebug folder. Now the question is what is inside the NuGet packages, to see that we can use NuGet Package Explorer. Let us now open the NuGet Package Explorer. Select the first option Open a local package. Select the StringLibrary.1.0.0.nupkg and click Open. You can see that in the Package contents section we have StringLibrary.dll only. In the Package metadata section, you will see a bit of information about this library like Id, Versions and all the of the dependencies. Let us now open the StringLibrary.1.0.0.symbols.nupkg. In this NuGet package, you will see the source files and the *.pdb file as well. If you double-click on the StringLib.cs file, you see the source code as well. Here the question is, how can configure the metadata like version, authors and description, etc. The project.json file is used on .NET Core projects to define project metadata, compilation information, and dependencies. Let us now open the project.json file and add the following additional information. { “authors”: [ “Mark Junior” ], “description”: “String Library API”, “version” : “1.0.1-*”, “supports”: {}, “dependencies”: { “Microsoft.EntityFrameworkCore”: “1.1.0”, “Microsoft.NETCore.Portable.Compatibility”: “1.0.1”, “NETStandard.Library”: “1.6.0”, “System.Runtime.Serialization.Json”: “4.0.3”, “System.Runtime.Serialization.Primitives”: “4.3.0” }, “frameworks”: { “netstandard1.3″: {} } } You can now see additional information like author name, description and version added here. Let us save this file, build the library project, then execute the “dotnet pack” command again. Inside the binDebug folder, you can see that the StringLibrary NuGet packages are produced with version 1.0.1; let us open it in NuGet Package Explorer. You will see the updated metadata. The question now is, how can we use it in another package. We need to start by publishing somewhere in the NuGet feed and then we can consume it in another project. There are two options to publish the updated metadata − Publish it to nuget.org Push the metadata to private NuGet feed Here we will be using the private NuGet feed because it is a lot easier than to setup an account on nuget.org. To learn how to publish your package to nuget.org, you can follow all the guidelines specified here https://docs.microsoft.com/en-us/nuget/create-packages/publish-a-package. Follow these steps to push the updated metadata to private NuGet feed. Step 1 − To start with, we need the nuget commandline utility and we have to install it. Let us now open the NuGet Package Manager and search for nuget.commandline. Step 2 − Select Nuget.Commandline and click Install. Step 3 − Click OK to install Nuget.Commandline. You can also manually install it by downloading it from the following Url https://dist.nuget.org/index.html and then set up the environment variable. Step 4 − Once the installation is finished, let us open the command prompt again and go to the binDebug folder where the NuGet packages are located and specify the following command − nuget add StringLibrary.1.0.1.nupkg -Source D:PrivateNugetPackages Step 5 − In the above command, we add the StringLibrary.1.0.1.nupkg package to our private feed and the location is D:PrivateNugetPackages, -Source specifies the package source. Step 6 − You can see that the StringLibrary is installed; the StringLibrary can further be added to the private feed. Step 7 − Let us go to that folder. Step 8 − Inside the stringlibrary folder, you will see another folder with the version name and here it is 1.0.1. The NuGet package is located here. Print Page Previous Next Advertisements ”;
Windows Runtime and Extension SDKs ”; Previous Next Windows Runtime components are self-contained objects that you can instantiate and use from any language, including C#, Visual Basic, JavaScript, and C++. In addition to the .NET Core meta-package we saw in the previous chapter, UWP app also has a reference by default to a Universal Windows SDK. Universal Windows is the reference to Windows Runtime and it has been factored into a series of APIs contracts. The set of APIs within a device family is broken down into subdivisions known as API contracts. You can find a list of different API contracts here https://msdn.microsoft.com/en-us/library/windows/apps/dn706135.aspx Most of those APIs inside windows runtime are factored into a single contract. Let us now search for the Universal keyword on the API Contracts page. You can see links to various APIs and you can also see the Universal family is so big that it has 12 pages of documentation. You can also search for phone API contract on this page. Let us now click on the Windows.Phone.PhoneContract and scroll down; you will now see the battery information of phone or the mobile device. If you want to add this information on top of what you already have, then you should add the references manually. Let us now go to the Visual Studio and right-click on the References in Solution Explorer. Select Add References… You can now see the new reference category for Universal Windows; under this category there is Core which refers to the core Universal Windows API contracts The Extensions allow us to extend the functionality and you will see different references Mobile, Desktop and other Extensions. There are different SKD extensions and you can add on top to get more APIs. You can also see different versions. So, make sure you get the latest version to get the updated APIs and then click OK. You can now see that Windows Mobile Extensions for the UWP is added as reference. Print Page Previous Next Advertisements ”;
.NET Core – Migrations
.NET Core – Migrations ”; Previous Next In this chapter, we will migrate the console application which contains the project.json file build system instead of MSBuild (*.csproj). So, we have an old project which contains the following files. Now the question is, why do we need migration? This project is created using .NET Core 1.0 preview 2 tooling and now we have installed .NET Core 2.0 preview 1 tooling. Now when you build this application using .NET Core 2.0 command line utility, then you will see the following error. This is because the project.json build system is no longer available in .NET Core 2.0, so we need migration so that it can work properly. To see the available commands, let us run the following command. dotnet help In the commands section, you can see the different commands and you can also see the migrate command which will migrate a project.json based project to a MSBuild based project. Let us now run the following command. dotnet migrate You will see a summary of the migration process and here you can also see that a project is migrated successfully. Let us now see the directory structure by using the following command. tree /f You will now see the *.csproj file along with Program.cs file in the project root directory and project.json is moved to the backup folder. Let us open the console.csproj file. Now you can restore and build this project using the MSBuild system by running the following command. dotnet restore You can now see that all the packages are restored. You can now build your project with the following command. dotnet build You can now see that the project is built successfully using MSBuild and console.dll is also generated in ..binDebugnetcoreapp1.0 folder. The following screenshot shows the directory structure and files. Print Page Previous Next Advertisements ”;
.NET Core – MSBuild
.NET Core – MSBuild ”; Previous Next In this chapter, we will discuss what is MSBuild and how it works with .NET Core. MSBuild is the build platform for Microsoft and Visual Studio. In UWP application if you open the project folder, then you will see both project.json and *.csproj files. But if you open our previous .NET Core Console app, then you will see project.json and *.xproj files. The .NET Core build system or the project.json build system is not sufficient for UWP needs; this is why UWP is still using *.csproj (MSBuild) Build system. But project.json will move out as far as the build system is concerned. Now if you want to add a few existing files to your UWP application as we have added in the Console app, then you need to add those files in the project folder. Further, you will also need to include in your project in Solution Explorer as well. Let us now consider the following files; copy these files to your project folder. Let us go back to Visual Studio and open the Solution Explorer. You can now see that only copying files is not sufficient in case of UWP applications, because in Solution Explorer, we can’t see those files. Now we must include those files as well by clicking on the Show All Files icon as highlighted in the above screenshot and you will see now all files in the project folder. These two files are still not included in our project. To include these files, select these files and right-click on any file and then select Include in Project. Now these files are also included. One good thing that can be foreseen is the project.json approach of dropping files for *.csproj in the future version of the SKD tools and also to Visual Studio. Print Page Previous Next Advertisements ”;