How To Create An App In Visual Studio 2017
Introduction
Here, I will explain how to create an Android app, using C# and Xamarin forms templates, using Visual Studio 2017 and distribute it locally. I know only XAML and C# code, using which I have created an app, which is used to calculate an age by the giving date of birth.
Explanation
Let's start creating a project in Visual Studio by going to Start->New->Project. Now, you can see a pop up asking to select a template to create a project. In it, I have selected Cross Platform->Cross Platform app(Xamain.Forms or Native ) and I gave the name as AgeCalaculate, as shown below.
After clicking OK button, you can see a one more popup, as shown below.
In this popup, I have selected Blank App, under UI Technology Xamarin.Forms and PCL as Code sharing Strategy. After clicking OK button; you can see three projects are created, i.e., portable, Android and iOS and you can see in the left side App.xaml.cs file opens in the first page is mentioned as MainPage i.e. MainPage.xaml in the Portable project. This is the startup page, which is shown below.
Let's do some modification in the MainPage. For this, open the page and remove that default label. Add one stack layout with an orientation vertical. Inside this stack layout, I have added 2 labels,1 datepicker and one button and XAML code is shown below.
- <?xml version= "1.0" encoding= "utf-8" ?>
- <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
- xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
- xmlns:local="clr-namespace:AgeCalculate"
- x:Class="AgeCalculate.MainPage" >
- <StackLayout Orientation="Vertical" >
- <Label Margin="10,10,10,10" Text= "Enter date of birth" />
- <DatePicker x:Name="dtdob"
- Margin="10,10,10,10" />
- <Button Text="Convert to Age"
- HorizontalOptions="Center" Clicked= "bt_click" TextColor= "White" BackgroundColor= "Blue" />
- <Label Margin="10,20,10,10" x:Name= "lblAge" Text= "Age :" />
- </StackLayout>
- </ContentPage>
I had given a click event for the button and date picker name as dtdob. The label name given below is lblAge in order to access these controls from C# code.
In the code backend file, I have created a static function, which accepts a date and returns a string; I have added a logic to get an age from date into this function. In button click event, I am calling this static function by passing the datepicker date value and storing to a string variable. This string variable is assigned to the last added label lblAge. The code is shown below.
- public static string CalculateYourAge(DateTime Dob)
- {
- DateTime Now = DateTime.Now;
- int _Years = new DateTime(DateTime.Now.Subtract(Dob).Ticks).Year - 1;
- DateTime _DOBDateNow = Dob.AddYears(_Years);
- int _Months = 0;
- for ( int i = 1; i <= 12; i++)
- {
- if (_DOBDateNow.AddMonths(i) == Now)
- {
- _Months = i;
- break ;
- }
- else if (_DOBDateNow.AddMonths(i) >= Now)
- {
- _Months = i - 1;
- break ;
- }
- }
- int Days = Now.Subtract(_DOBDateNow.AddMonths(_Months)).Days;
- return $ "Age is {_Years} Years {_Months} Months {Days} Days" ;
- }
- private void bt_click(object sender, EventArgs e)
- {
- string _Age = CalculateYourAge(dtdob.Date);
- lblAge.Text = _Age;
- }
The output is shown below.
Now, as you can see here; the Android app has been created successfully. Next, we will see how will we distribute this app.
For this, let's reduce app size by taking only SDK assemblies by right clicking on an Android project and selecting the properties->Androidoptions->Linker, as shown below.
Subsequently, you can create an Android bundle by right clicking an Android project and selecting archive, as shown below.
After clicking Archive screen, it will display, as shown below.
Wait to finish creating an Android bundle. After finishing screen, it will display, as shown below.
Below this screen, you can see two buttons, where one is an open folder. When you click this folder, then you can see .apk bundle file is created but this is not signed. In order to distribute an app properly, you need to sign this app before distribution. For this, we have to click second button that is Distribute screen, as shown below.
When you click this button, you can see a screen asking how you wanted to distribute by Google play or Ad Hoc. If you go for Google play , then you need some registration process and so on. Here, I will explain Ad Hoc, which is used to distribute locally. Let's see how is that click on Ad Hoc, then you can see a pop up in this click on + symbol to create a new certificate, as I don't have any of the signed certificates, and add all the details in the following pop up and click Create button to create, as shown below.
After creating the pop up, it will look, as shown below.
From this pop up, select the certificate from the grid, which you wanted to apply. As of now, I have only one, which I have created just now that is rntp. Select this and click Save As button and locate, where you wanted to save this signed app. After clicking Save as button, the screen shown below.
Select your desired place and subsequently click Save. After clicking Save, you will be prompting to put a password, which you had given in the certificate and click OK. The screen is shown below.
After clicking OK, the signed Application will be generated at your desired place. You can locate this by clicking Open distribution button from the screen shown below.
Your app is ready to distribute. Now, you can share this app anywhere Android OS is supported. It can be intsalled like any other app.
How To Create An App In Visual Studio 2017
Source: https://www.c-sharpcorner.com/article/creating-and-distributing-android-app-using-visual-studio-2017/
Posted by: charettebegather1962.blogspot.com
0 Response to "How To Create An App In Visual Studio 2017"
Post a Comment