Monday, 21 May 2018

How to convert WPF project to UWP App

With the help of Desktop App Converter, Now you can easily convert your WPF App to UWP within few clicks. I will walk you through the steps on how to convert your traditional WPF App to UWP App.

Before that I would like to mention below few reasons about why you should migrate to UWP Apps and what benefits you can get by doing so:


✔️ Streamlined deployment. Apps and games that use the bridge have a great deployment experience. This experience ensures that users can confidently install an app and update it. If a user chooses to uninstall the app, it's removed completely with no trace left behind. This reduces time authoring setup experiences and keeping users up-to-date.
✔️ Automatic updates and licensing. Your app can participate in the Microsoft Store's built-in licensing and automatic update facilities. Automatic update is a highly reliable and efficient mechanism, because only the changed parts of files are downloaded.
✔️ Increased reach and simplified monetization. Choosing to distribute through the Microsoft Store expands your reach to millions of Windows 10 users, who can acquire apps, games and in-app purchases with local payment options.
✔️ Add UWP features. At your own pace, you can add UWP features to your app's package, like a XAML user-interface, live tile updates, UWP background tasks, app services, and many more.
✔️ Broadened use-cases across device. Using the bridge, you can gradually migrate your code to the Universal Windows Platform to reach every Windows 10 device, including phones, Xbox One and HoloLens.
You can refer to MSDN if you are interested to learn more.
Prerequisite before starting the process:
  • Windows 10 OS
  • Visual Studio 2017
  • Windows 10 SDK
Let's start converting WPF App to UWP App:

1. Below is my simple WPF project structure which just print Hello World.



2. Now right click on the solution and select Add -> New Project, A project template dialog will appear select 'Windows Appliaction Packaging Project'  under 'Windows Universal'.


3. After selecting the project another modal dialog will appear which will ask you to select SDK minimum and maximum target version. For more info please refer this MSDN link.I am selecting Anniversary Update(Build 14393) as Minimum version and Fall Creators Update(Build 16299) as target version.



4. New project is now added in your solution.



5. Right click on the UWP project -> Add Reference -> Give reference to your WPF App.



6. Now build your solution and you are ready to go. You can create a store package and can directly publish your app in the store.Now you might be wondering how to add UWP functionalities to your App. Keep following the article, Below I will show on how you can consume UWP API's in your WPF Application.


7. Right click on References of your WPF Applicatoin. Select “Add Reference…” from the context menu. On the left of the Reference Manager, choose Browse and find the following file: C:\Program Files (x86)\Windows Kits\10\UnionMetadata\winmd. Add it to your project as a reference. Note: You will need to change the filter to “All Files”.



8. Right click on References of your WPF Application. Select “Add Reference…” from the context menu. On the left of the Reference Manager, go to Browse and find the directory “C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5”. Add System.Runtime.WindowsRuntime.dll to your project.



9. Now you can access UWP API's from your WPF project, in below example I am accessing ApplicationData API to create File.


var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists);

10. To test your application you need to set UWP App as a Startup project. To do that right click on UWP App and select 'Set as Startup Project'. 


I hope you get to know on how you can convert your WPF Application to UWP App and take advantage of latest and greatest API and reaching more user base by submitting your App to Microsoft Store.
Feel free to comment below if you have any questions or suggestions.
Happy Coding! :)



Saturday, 10 June 2017

Privacy Policy


We do not collect any personally identifiable data and keep all e-mails etc from our users confidential. Your personal information will NOT be sold/given to any third party firms.
We restrict access to personal information to our employees and third party individuals.
Please note that this Privacy Policy may change from time to time. We will not reduce your rights under this Policy without your explicit consent, and we expect most such changes will be minor. Regardless, we will post any Policy changes on this page and , if the changes are significant, we will provide a more prominent notice (including, for certain services, email notification of Policy changes).

Monday, 26 January 2015

How to use SQLite with Entity Framework 6 in WPF Application

When you are looking for a free and light weight tool to store your data locally SQLite is the best alternative to go for, SQLite is a small, fast and reliable database which can be used by end user without any installation requirement.

Getting Started:


Here are the steps you need to follow:

1> Create File->New Project -> WPF Application.

2> Right click on your project -> Properties -> Build Tab -> Change Platform Target to x86 and build solution.

3> Now  you first need to install Entity Framework via nuget.

Go to Tools -> Library Package Manager - > Package Manager Console, 

Your Package Mangager Console will open, paste the below line and hit enter.

Install-Package EntityFramework

It will install latest version of Entity Framework from nuget in your application.

Also you need to have SQLite to be install in your application, so paste the below line in your PMC and hit enter.

Install-Package System.Data.SQLite

4> Now as SQLite and EF is reference in our application, we need to create SQLite  database to use with our application.

There is a good Firefox plugin "SQLite Manger" for creating and managing SQLite database.

I have created a database name "MyDB", with one table "UserInfo", below is the structure of this table:


CREATE TABLE "UserInfo" ("ID" INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , "FirstName" TEXT, "LastName" TEXT)

Also add few records in the table, so that you can view that in your application.


5> Once your database is created, In your application add new folder give it any name say "DataFile" and put your SQLite database here in this folder.

Now right click your database file -> Properties -> Select "Copy if newer" in Copy to Output Directory field and build project.


6> Open MainWindow.xaml and insert below code in your grid.


<DataGrid x:Name="dataGrid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />


7> Go to MainWindow.cs file and add following code(This is code base configuration instead of configuring in app.config):


        public class SQLiteConnectionFactory : IDbConnectionFactory
        {
            public DbConnection CreateConnection(string nameOrConnectionString)
            {
                return new SQLiteConnection(nameOrConnectionString);
            }
        }

        public class SQLiteConfiguration : DbConfiguration
        {
            public SQLiteConfiguration()
            {
                SetDefaultConnectionFactory(new SQLiteConnectionFactory());
                SetProviderFactory(&quot;System.Data.SQLite&quot;, SQLiteFactory.Instance);
                SetProviderFactory(&quot;System.Data.SQLite.EF6&quot;, SQLiteProviderFactory.Instance);
                Type t = Type.GetType(&quot;System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6&quot;);
                FieldInfo fi = t.GetField(&quot;Instance&quot;, BindingFlags.NonPublic | BindingFlags.Static);
                SetProviderServices(&quot;System.Data.SQLite&quot;, (DbProviderServices)fi.GetValue(null));
            }
        }


8> We need to create a class that represent our UserInfo table, so here is the class for that:

        [Table("UserInfo")]
        public class User
        {
            public int ID { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
        }


9> No we need to create DataContext of use Entity Framework with Sqlite:

         public class MyDBContext : DbContext
        {
            public MyDBContext()
                : base(new SQLiteConnection()
                {
                    ConnectionString =
                        new SQLiteConnectionStringBuilder() { DataSource = "I:\\WPF\\MyDB.sqlite"}
                        .ConnectionString
                }, true)
            {

            }
            public DbSet<User> Users { get; set; }
        }


You can see that I have give static path of my sqlite database in DataSource field, but you can give relative path using Assembly.GetExecutingAssembly() method.


10> As our DataContext is created, we need to fetch record from the database to display in our datagrid, so write this below code in your MainWindow_Loaded:

                MyDBContext context = new MyDBContext();
                context.Users.OrderBy(c => c.FirstName).Load();
                this.dataGrid.ItemsSource = context.Users.Local;


11> Now run the application and you will see that all your records in database file will be display in datagrid.


Happy Coding :)






Monday, 12 January 2015

How to integrate Font Awesome in WPF Application.

Font Awesome is really a good choice for those who are looking for icons fonts instead of heavy images that needs to embed in to the application, that makes application heavy with lots of images.

Font Awesome has many advantages, like you don't have to care of colors, if you need to change it any time, you have to just change simple property value of  XAML controls. Also you don't have to care of the size and pixel rendering, Font Awesome is really good in that as it uses vector graphics, so simply settings FontSize or relative properties on control will do your job.

Lets have a look on how to integrate Font Awesome in your WPF application:

1> First you have to download Font Awesome which you can from below link :

    http://fortawesome.github.io/Font-Awesome/

2>Now create an File->New Project -> WPF Application.

3>Add new folder "Fonts" in your application.

4>Extract Font Awesome downloaded zip, inside fonts you will find "fontawesome-webfont.ttf" file, add this file to your "Fonts" folder in your application. Below is the screenshot of how its in VS 2012





5>Add below snippet to App.xaml file inside <Application.Resources> tag
  
<FontFamily x:Key="FontAwesome">/Fonts/fontawesome-webfont.ttf#FontAwesome</FontFamily>

6> Open MainWindow.xaml and replace the grid with below snippet:

<Grid VerticalAlignment="Center" HorizontalAlignment="Center">

<StackPanel Orientation="Horizontal" >

<TextBlock Text="I" FontSize="32" Margin="10" VerticalAlignment="Center"></TextBlock>

           
<TextBlock x:Name="tbFontAwesome" Text="&#xf004;" FontFamily="{StaticResource FontAwesome}" Foreground="Red" FontSize="32" Margin="10" VerticalAlignment="Center"></TextBlock>
<TextBlock Text="Font Awesome" FontSize="32" Margin="10" VerticalAlignment="Center"></TextBlock>

</StackPanel>

</Grid>


7> Notice "Text" property of  "tbFontAwesome" textblock, its the Unicode for Heart.You can find more others from this below link:

http://fortawesome.github.io/Font-Awesome/cheatsheet/


8>Now run the application and you will see the output as below.So now you can set size or color of your choice.