How to Check the App is running for the First time – in Xamarin Forms (Android/iOS/Windows)

Objective

In one of my Xamarin Forms apps, I wanted to check is the app running for the first time after the installation or the app is already installed and according to that Load different pages in the app

 How to Install Xamarin in Visual Studio 2017…

Furthermore,

  1. If the App is running for the first time, I wanted to a Quick Start Guide to the user.
  2. If all has already installed, app will show the Login Form.

pags

Then I looked for a solution and James Montemagno‘s  SettingsPlugin gave me the solution…

If you want to learn more, Click above links to go to GitHub and  MotzCod.es.

How I did it ?

First I went through the Instruction of the above references and I installed the plugin using NuGet..

Here I’ll explain how I did it.. and Here is my project at the beginning.

1-project

1. First Install the Settings Plugin using NuGet packages.. Search  “Xam.Plugins.Settings”

2-download-settings

2. Apply The package to all projects or selected projects.. (But I’m managing the settings from the PCL project) and Install the package..

3-select-all-projects

3. Then Conform the installation..

4-conform-installa-tion

4. When the packages has installed, It will generate a folder which is “Helpers” and a class onside it “Settings.cs” to manage settings.

It will added to all selected projects.

5-helpers-settings

The default Settings class includes below C# code to manage settings..


using Plugin.Settings;
using Plugin.Settings.Abstractions;

namespace FirstTimeUse.Droid.Helpers
{
///
<summary>
/// This is the Settings static class that can be used in your Core solution or in any
/// of your client applications. All settings are laid out the same exact way with getters
/// and setters.
/// </summary>

public static class Settings
{
private static ISettings AppSettings
{
get
{
return CrossSettings.Current;
}
}

#region Setting Constants

private const string SettingsKey = "settings_key";
private static readonly string SettingsDefault = string.Empty;

#endregion
public static string GeneralSettings
{
get
{
return AppSettings.GetValueOrDefault<string>(SettingsKey, SettingsDefault);
}
set
{
AppSettings.AddOrUpdateValue<string>(SettingsKey, value);
}
}

}
}

To read the more about the Settings class go to:  https://github.com/jamesmontemagno/SettingsPlugin

I’m going to store my value in the SettingsKey property. So I’ll change the default value of the SettingsKey property to “yes”. Which means the app is running for the first time.

6-change-settings-key


private static readonly string SettingsDefault = "yes";

5. Now I’m going to use the Settings in the application..

I’m checking the app status in the App.xaml.cs class. Because the app navigating to the Main page happens in the constructor in App.xaml.cs class.

The default code of the App class is… And there are not properties in this class…


public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new MainPage();
}

protected override void OnStart()
{
// Handle when your app starts
}

protected override void OnSleep()
{
// Handle when your app sleeps
}

protected override void OnResume()
{
// Handle when your app resumes
}
}

Now I’ll add a Property to App class to Manage App Settings. The property name is “IsFirstTime” and type is String.

7-add-isfirsttime


///
<summary>
/// Get and Set value to General settings
/// </summary>

public string IsFirstTime
{
get { return Settings.GeneralSettings; }
set
{
if (Settings.GeneralSettings == value)
return;
Settings.GeneralSettings = value;
OnPropertyChanged();
}
}

6. Now I’ll add another page to use for log in to system if the app has already installed.

Page name is “LoginPage.xaml”.

8-added-2-pages

7. Now Lets Write code in App.xaml.cs to check the app status…

The page is initialize in the constructor of the App class. So I’m going to check the status of the app inside of the constructor.

And Here is the code. In the Settings class, I have set the default value as “yes“. So the app runs for the first time, the value should be “yes“. Then  the MainPage page will open. And when the app ran for the first time, it  sets the “IsFirstTime value to “no“.

Then when the user run the app after the first time, it will load the second page which is Logging Page.

9-check-first-time


public App()
{
InitializeComponent();

// Check is the app running for the first time
if (IsFirstTime == "yes")
{
// if this is the first time, set it to "No" and load the
// Main Page ,which will show at the first time use
IsFirstTime = "no";
MainPage = new FirstTimeUse.MainPage();
}
else
{
// If this is not the first time,
// Go to the login page
MainPage = new FirstTimeUse.LoginPage();
}
}

8. The Build and run the app using emulator. I’m using my Samsung Galaxy J7 as the emulator. And the App was run  successfully.

screenshot_20170219-173522

First time it will open the Main Page

screenshot_20170219-173745

And When I run the app after the first time, It loads the Login page..

 

References…

https://github.com/jamesmontemagno/SettingsPlugin

https://components.xamarin.com/gettingstarted/settingsplugin

 

Thanks !

Advertisements

How to Fix Error “C:\Program Files (Access is Denied)” When Proguard has Enabled while Build a Android app

Why ?

When I was trying to build one of my android apps in “Release” mode to deploy, I got and Error that  “C:\Program Files (Access is Denied)” and the File was “PROGUARD”.

2-errror

Then I looked for a solution to avoid the error.

What is Proguard ?

The Reason For The Error

In the Android project, I have set the Proguard Enabled before I build the app.

The reason for the error is there is a space in the path to the PROGUARD. Proguard is in the Android folder in Program Files (x86) and there is a spaces between “Program Files (x86)”.

C:\Program Files (x86)\Android\android-sdk\tools\proguard

0-program-file-spaces

So PROGUARD may unable to find the path..

So How to Fix This ?

What we have to do is Place the android-sdk folder to a path without spaces..

To do that I created a symbolic Link(sym-link) ..

Here are steps to create a symbolic link..

1 .First we have to download and Install the  Link Shell Extension.

To Download the Link Shell Extension , Click Here .. 

2. After Installed the Extension then we are ready to create the sym-link.

How To Create a Symbolic Link, Click Here…

Here are steps How I created according to my Folder structure..

First I create a Folder in C: drive outside of Program Files x86 which is “AndroidSysLink”. So we have to Link the Android-SDK path to this folder.

First Go to Program Files x86 and Right Click on Android Folder and we can see a new context menu “Pick Link Source”. Then Click it..

1-pick

Then Go back and Select “AndroidSysLink” Folder -> Right Click . Then We can see a new context menu “Drop As” . When we click on it and we will see few options in another context menu. Then Select “Junction”. And then The link will be created.

2-drop-symbolic-link

3. Then We have to change the Android SDK path in Visual Studio.

So go to Tools -> Options -> Xamarin and Click the “change” button inside of the Android SDK location…

Then we can see a dialogue to select the sdk path..

3-existing-sdk-path

4. Go to the Linked folder and copy the path in it..

4-select-new-path

5. Paste the Path in the sdk-path selection dialogue box.

5-new-sdk-path

Then Click OK…

6. Now The Path has changed an Click OK to conform the new SDK path..

6-new-sdk-path

7. Now we are ready to release the app. But before Build the app recheck the configuration in properties.

First Verify Target Android versions..

0-selectandroid-versions

Make Sure the Confutation has set to “Release” and Proguard is Enabled..

1-enable-proguard

8. Then Build The app in Release Mode..

The app will build successfully..

3-build-success

9. Now we are ready to Archive the Android App.

Right Click on the Android project and click “Archive“…

4-archieve

We can see Archiving Progress…

5-archieving

We can see a Settings I con with a Green icon when the app has archived successfully and in the bottom of the dialogue we can see build information.

6-archieved-successfully

Click the “Open” folder and the created .apk file will see in that folder.

7-open-the-path

References…

https://developer.xamarin.com/guides/android/deployment,_testing,_and_metrics/publishing_an_application/part_1_-_preparing_an_application_for_release/

https://developer.xamarin.com/guides/android/application_fundamentals/cpu_architectures/

https://developer.xamarin.com/guides/android/deployment,_testing,_and_metrics/proguard/

https://www.howtogeek.com/howto/16226/complete-guide-to-symbolic-links-symlinks-on-windows-or-linux/

http://schinagl.priv.at/nt/hardlinkshellext/linkshellextension.html

https://forums.xamarin.com/discussion/45210/enable-proguard-error-c-program-files-access-is-denied

Thanks !

How to Fix Error : Xamarin.Forms.Xaml.XamlParseException: Position 39:8. Type local:NoBorderEntry not found in xmlns clr-namespace:LoginApp;assembly=LoginApp

Why ?

I created a sample login form using Xamarin  and I wanted to remove the underline of the Entry controls. So I had to use a custom render to change the Entry Control .

Below Image describes what I was going to do…

2-uis

So I wrote a custom render to customize the Entry Control in Android(Droid) project and when I was trying to run the app , It gave me an exception that..

Xamarin.Forms.Xaml.XamlParseException: Position 39:8.

Type local:NoBorderEntry not found in xmlns clr-namespace:LoginApp; assembly=LoginApp

1-error

How to Solve the issue ?

So I  looked for a solution and I found the issue is in the assembly name that I have defined in the XAML file.

What I had to do is change the assembly name.

 

Now I’ll describe you what was the wrong and How did I solve the issue..

First to learn How to write a custom render for Entry Control, Click here …

Here is the architecture of my app..

3-architectire

My code..

NoBorderEntry  Class


using Xamarin.Forms;

namespace LoginApp
{
public class NoBorderEntry : Entry
{

}
}

NoBorderEntryEntryRenderer Class


using Android.Graphics.Drawables;
using LoginApp;
using LoginApp.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(NoBorderEntry), typeof(NoBorderEntryEntryRenderer))]
namespace LoginApp.Droid
{
public class NoBorderEntryEntryRenderer : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);

if (Control != null)
{
Control.Background = new ColorDrawable(Android.Graphics.Color.Transparent);
}
}
}
}

5-code-csharp-namespace

 

MainPage.Xaml

The XAML code is too large in my file. SO I’ll write only the important parts of the XAML code..


<?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:LoginApp;assembly=LoginApp"
x:Class="LoginApp.MainPage" >

<local:NoBorderEntry Grid.Row="0"  Grid.Column="1"  Placeholder="User Name"  PlaceholderColor="Silver" TextColor="#D6FCE6" />

</ContentPage>

And here you can see the namespace that I have declared and how we can use it to the Entry render..

4-xaml-code-1

You can see the highlighted texts in above images and according to them the namespace in the NoBorderEntryRenderer is “LoginApp.Droid” but in the in the MainPage.Xaml file the assembly name id “LoginApp”.

So “assembly=LoginApp” should me changed to “assembly=LoginApp.Droid” and the Error will be fixed.

6-fix-error

Then the correct XAML namespace declaration should be like below..

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms&quot;
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml&quot;
xmlns:local="clr-namespace:LoginApp;assembly=LoginApp.Droid"
x:Class="LoginApp.MainPage" >

</ContentPage>

Now the app will run without any error…

screenshot_20170215-233357

 

References

https://developer.xamarin.com/guides/xamarin-forms/custom-renderer/entry/

Thanks !

java.lang.UnsupportedClassVersionError: com/android/dx/command/Main : Unsupported major.minor version 52.0 – How To Fix this Error ?

Why ?

When I was trying to compile one of my sample Xamarin Forms Android project, I got the below error …

java.lang.UnsupportedClassVersionError: com/android/dx/command/Main : Unsupported major.minor version 52.0
LoginApp.Droid  

1-error

Reason :

Then I looked for a solution and finally I found the reason is Android API 24 Requires Android JDK 8. But Still I had Android JDK 7 in my machine which came with Xamarin 4.2 with Visual Studio 2015.

Solution :

The solution was to download the Android JDK 8 and change the Android JDK path in Visual Studio to jdk 1.7 to jdk 1.8. It fixed the error and Finally my project built successfully.

Here Are Steps to Install the JDK 1.8 and how to fix the error..

  1. Go to Java SE Development Kit 8 Downloads and download the suitable file which is compatible with your OS. I have windows 10 x64 , So I downloaded the file for windows x64.

2-jdk-1-8

2. Then Install The java Development Kit 8..

3-jdk-installing

3. Select the path to Install the JDK 1.8 and go through the setup wizard…

4-select-destination

4. Then Close the setup when the installation has completed…

5-update-completed

5. Open the Visual Studio 2015 and Then Go to Tools-> Options – > Xamarin -> Android Settings

6-jdk-1-7-path

6. Change the Java Development Kit Location to JDK 1.8 path  instead of JDK 1.7 path

7-select-jdk-8-path

7. Click OK when the JDK 1.8 Path selected…

8-selected-jdk-8-path

8. Now Rebuild the Project and It will work fine… (Here is the built result in my project)

9-built-successfully

9. Now you can run the emulator to ensure that the project has build successfully…

(I’m using my Samsung galaxy J7 phone as the Emulator and it worked fine…)

10-emulator-successfull

The app opened in the phone successfully…

screenshot_20170210-213844

 

References…

https://forums.xamarin.com/discussion/67720/unsupported-major-minor-version-52-0

Thanks !

Fix Error : “Could not find a part of the path” While Publishing ASP.NET Core MVC Application To the Target Platform of Windows 7 x64/x86

Reason :

I’m working on a ASP.Net Core MVC application and I Published one of my ASP.Net Core MVC app into a Server which is running Windows Server 2008 R2.

But when I released the application live, it didn’t work on the server but it works fine on my local machine. My Local machine has Windows 10.

Then I looked for a solution and I got the reason. The reason Is I have published the app for the target platform of Windows 10. But the Server that the site has hosted has Windows Server 2008 R2 (Windows 7 Configurations)

Then what I had to do I was : Publish the app for the target platform of windows 7 (Windows Server 2008 R2).

Then I added target platforms to to the project.json file in my .Net Core MVC app. I published it again.

But I wasn’t published and it gave me an error..

Could not find a part of the path ‘F:\Projects\TestWebCore\src\TestWebCore\bin\Release\netcoreapp1.0\win7-x64\TestWebCore.dll’.

Then I looked for a solution and I found it is an error in tooling.

http://stackoverflow.com/questions/39023224/error-could-not-find-a-part-of-the-path-while-publishing-net-core-application

Solution :

The Solution is we have to publish the .Net Core applicaion using command prompt. 

I did it and my app was published successfully.

 

Here I’ll show you how the error occurred and How I did fix it.

First I’ll create a sample .Net Core MVC app with a .Net Core Portable Class Library(PCL).

You can See by Clicking..

How to Create a ASP.Net Core MVC app

Here is the project I have created..

The solution consists of one ASP.Net Core MVC project and a .Net Core PCL project.

1-project-flow

Image 1 : Solution Structure

Then I have changed the Index.cshtml file and added a simple html content to show you ..

2-change-index-page

Image 2 : Sample HTML content in Index.html file

Lets run the app to see how it looks like.

The app will run with the localhost and you could see the content I have added to the html body. I have used a Bootstrap template to the project. So you can see it is responsive.

3-index-page

Image 3 : Run The app in localhost

So Its running well and and the next step is Publish the app.

Publish The app

Then you try to publish an app for the first time, you will see a window like below..

5-publish-without-target-framework

Image 4 : Default Publish Window

Here you can see only one menu which is “Profile”. You can’t publish the app without selecting a profile. So we have to create a publish profile.

for that click on “Custom” button and you will see a dialague box to enter the publish profile name.

6-create-a-publish-profile

Image 5 : Create a Publish Profile

I’ll create it as “MyPublishProfile”. And click “OK”. Then the profile will be created and you will see new menus in the left hand side now.

7-selet-profile-menu

Image 6 : New menus will appear when Publish Profile created.

Then go to connection menu and select a folder to publish the web application. And Select the publish method as “File system”.

9-select-a-publish-location

Image 7 : Connection menu(Select Target Publish Folder)

Then go to the settings menu and you can select the target frameworks to publish the app.

8-target-runtme-any-settings

Image 8 : Default Settings Menu.

But according to above image, you can’t see a Target Runtime. 

By default it has set to “Any“. 

So I’m adding some target runtimes to project to show how the error is happening.

We have to add those settings to the project.json file in the web application project.

To see How to add Target Runtimes to project.json  Click Here…

If you have already added target  Runtimes, Now we are ready to publish the app.

So Right Click and Select Publish in the context menu and the publish window will open.

Publish to the Target Runtime of Windows 10

Then Click “Settings” and Select “Win10-x64” from the drop down list.

5-select-target-platform-10

Image 9 : Select Windows 10 as Target Runtime

Then Click Preview menu and verify is the details correct before publish your app.

6-conform-details-before-publish

Image 10 : Verify Publish information before publish the app

Then Click “Publish” and the application will publish to the selected folder. You will see publish details output window.

7-success-win-10

Image 11 : App has publish successfully for the Target Runtime of  Windows 10 x64

Then check the selected publish folder and you will see compiled files has published to the folder.

The “TestWebCore” app has publish to the target runtime of “Windows 10 64bit” successfully.

How The Error Occurred…

Publish to the Target Runtime of Windows 7

This is same as we published the app for Windows 10. But the only difference is we have to change the target runtime to “Win7-x64

11-publishing-to-win-764

Image 12 : Select Windows 7 x64 as Target Runtime

Then Click “Publish”.

When I did it, I got below error.

Could not find a part of the path ‘F:\Projects\TestWebCore\src\TestWebCore\bin\Release\netcoreapp1.0\win7-x64\TestWebCore.dll’.

12-error

Image 13 : Error Publishing app to Windows 7

How to Fix the Error

To publish without an error, what I did is ..

Run the Publish command in Command Prompt.

So Here is the way I did ..

13-publish-command

Image 14 : Run Publish command in Command Line

When you press “Enter” when type the publish command, you will see the application publish successfully.

14-published-to-win7-64

Image 15 : Successfully Published to Windows 7 x64

You can see the published files in the publish folder.

This Error is not happening

References…

To Learn More about Target Runtimes/Frameworks Click…

Publish Commands for .Net Core in Command Line Click…

How to Publish Self Contained Application…

Thanks !

How to add Target Runtimes to project.json in ASP.Net Core Project

To See how to create a .Net core app, Click here …

First Open the project and you can see there is a JSON file called “project.json”

3-app-created

Then click it and you will see the JSON content in that file.

The default JSON content like below.


{
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.1",
"type": "platform"
},
"Microsoft.AspNetCore.Diagnostics": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.0.1",
"Microsoft.AspNetCore.Razor.Tools": {
"version": "1.0.0-preview2-final",
"type": "build"
},
"Microsoft.AspNetCore.Routing": "1.0.1",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
"Microsoft.AspNetCore.StaticFiles": "1.0.0",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0"
},

"tools": {
"BundlerMinifier.Core": "2.0.238",
"Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
},

"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
}
},

"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},

"runtimeOptions": {
"configProperties": {
"System.GC.Server": true
}
},

"publishOptions": {
"include": [
"wwwroot",
"**/*.cshtml",
"appsettings.json",
"web.config"
]
},

"scripts": {
"prepublish": [ "bower install", "dotnet bundle" ],
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}

When we collapse the JSON content, we can see what are the main properties of the file..

12-default-project-json-file

So according to above image, there is no property to declare Target Runtimes.

So We have to add them manually according to our requirement.  I’m using windows based machines, so I’ll add windows operating systems as target runtimes.

13-add-runtimes-to-project-json


"runtimes": {
"win10-x64": {},
"win81-x64": {},
"win8-x64": {},
"win7-x64": {},
"win7-x86": {}
}

When you add this code, you could see the NuGet package references are restoring automatically in the right hand side .

14-restoring-packages

After It has completed, you are ready to publish the app.

Then Right click on the project and click “Publish”.

You will see the Publish window. And Go to the settings menu and you will see those runtimes has added to the target runtimes drop down list.

15-target-runtimes-in-publish

Important.

If you want to delete old published files when you are publishing a new version, Check the

“Delete all existing files prior to publish” check box..

16-delete-existing-files

Thanks !

How To Create .Net Core MVC Project

Open The Visual Studio 2015

Then File->New Project

->Select .Net Core

-> Select ASP.Net Core Web Application

-> And Add a name to the project (I have added “MyCoreApp2”)

1-create-project

Then Click “OK” Button And It will go to another window to select the project template.

Select “Web Application” and click OK.

2-select-web-application

Then Your Project will be created.

3-app-created

You will see the references are restoring and after it completed, Press F5. Then your new project will run with the default template.

4-default-app-runs