Coding Paradox

Blog On .Net, CSharp, Windows Phone Programming

Archive for the ‘C#’ tag

Updating App Tile using Background Agent in Windows Phone 8 App

with one comment

Background Agents and Scheduled Tasks allows your application to execute to code in the background even when the application is not running in the foreground. This is quite useful in various type of scenarios where you want to update the Live Tile frequently, show a toast notification on getting some data from web service, perform some data synchronization etc. In this tutorial we will take a look at how to use Background Agent to update the app tile in Windows Phone 8 application.

The Windows Phone API includes the ScheduledTaskAgent class which can be used to create Background agents which run on periodic schedules. Two types of tasks can be created which are

  • PeriodicTask : This is useful for scenarios which require small time to execute. This is run once every 30 minutes are useful for performing tasks like raising notifications, updating tile etc.
  • ResourceIntensiveTask : This is useful when performing operations which require a long amount of time. This tasks executes for 10 minutes however there are a lot of criteria related to processor activity which must be fulfilled before the task is executed. You can use this while performing lengthy operations like synchronizing data or uploading pictures.

A Windows Phone application can have only one background agent but it can register both PeriodicTask as well as ResourceIntensiveTask. If an application registers both the type of tasks, there are both executed at different intervals. The tasks can use a limited amount of memory which is around 11 MB at a given time. If memory usage increase, the task is immediately terminated. If the agents terminated two consecutive times due to unhandled exceptions, they are automatically removed from schedule. Read the rest of this entry »

Written by Mako

June 1st, 2014 at 3:26 pm

Using Local Database in Windows Phone Apps

without comments

In my previous posts, we looked at how to store data in Windows Phone Apps using Local Storage and Isolated Storage. In this post we will take a look at how to create a Local Database for storing data of our Windows Phone app. Database are great storage option when your app want to store some relational data. The database gets created into the local app folder and is only accessible to the same application.

We will be using the LINQ to SQL assembly and mainly the classes present in the System.Data.Linq and System.Data.Linq.Mapping namespace to create our database schema and perform CRUD operations on it.

In this demo, we will create a sample application that will store Students Information in the database. For each student, we will also store the courses that the student has taken. Lets get our hands dirty.

Open Visual Studio –> New Project –> Windows Phone Blank App and name the application “LocalDatabaseWPApp”.

Read the rest of this entry »

Written by Mako

May 12th, 2014 at 5:40 pm

Working With Local Storage in Windows Phone App

with 2 comments

In almost all the apps that are developed, some sort of data needs to be stored on the device either temporarily or permanently. There are lots of options of storing data when working on Windows Phone platform. In my earlier tutorials, we looked at how to create a simple Windows Phone app and performing page navigation. In this post, lets take a look at how to Read and Write files to the Local Folder in Windows Phone.

Open Visual Studio 2013/2012 and Create a New Windows Phone App project and name it “LocalStorageWpApp”. We will be creating a small application where we will read and write contents in a local file on Windows Phone.

Once the default solution is create, open MainPage.xaml and rename the page title to say “Local Storage”. Add the below XAML code to the ContentPanel Grid.

Read the rest of this entry »

Written by Mako

April 17th, 2014 at 6:47 pm

Performing Page Navigation in Windows Phone Apps

without comments

In the previous article, we learned the basics of creating a simple Windows Phone app. In this post, we will look at how to perform navigation between different pages in Windows Phone app. We will use the same Visual Studio solution used in the previous post and continue from there.

Open the MyFirstWPApp solution in Visual Studio. We will be adding a new page in our solution to which we can navigate to. Right Click on the project name in solution explorer –> Select Add –> New Item.

In the window that opens, select “Windows Phone Portrait Page”, name the page as “SecondPage.xaml” and click on Add.

Read the rest of this entry »

Written by Mako

April 6th, 2014 at 2:24 pm

Using ASP.Net WebAPI with Web Forms

with one comment

Asp.Net WebAPI is a framework for building RESTful HTTP services which can be used across a wide range of clients including web, phone and desktop applications. With WebAPI we can use XML and JSON to send and retrieve data from the service. The use of Json or XML makes it quite flexible to be used across a variety of devices.

Although WebAPI ships and installs with ASP.Net MVC 4, it can be used in normal Web Forms application as well.

In this example, we will use WebAPI with a traditional Web Forms application for adding and displaying a list of User Information. We will be using Visual Studio 2012 and .Net 4.5.

Creating the Project

Open Visual Studio 2012, Go To New -> Project. Select Visual C# -> Web from the left hand navigation and select project type as ASP.Net Web Forms Application. Enter the project name as WebAPIDemo

A new project will be created with a default template and our solution will show the following structure

Adding Model Class

We will then add a Model Class to our project. The Model class will represent the data in our application. We will add a model class called Users in our project

Read the rest of this entry »

Written by Mako

May 1st, 2013 at 3:33 pm

Calling ASP.Net WebAPI using HttpClient

without comments

In this post, we are going to learn how to call an ASP.Net WebAPI using HttpClient libraries. The HttpClient Library is quite useful and can be used while calling your WebAPI from Windows applications, Console Applications or even Windows 8 applications.

We will use the same WebAPI which we created in my previous post “Using ASP.Net WebAPI with WebForms” but this time we will consume it from a WPF application.

Create A WPF Application Project

Open Visual Studio 2012, Go To New -> Project. Select Visual C# -> Windows from the left hand navigation and select project type as WPF Application. Enter the project name as WebAPIClient.

Installing WebAPI Client Libraries

We will then add the HttpClient libraries in our project using the NuGet Package Manager which makes it very easy to install the libraries in our project

In the Tools menu, select Library Package Manager -> Manage NuGet Packages For Solution

In the Manage NuGet Packages dialog box which opens, select online and search for “Http Client” and select the Microsoft Http Client Libraries which appear as shown below

Select Install and it will ask you to select the project in which you want to install it. Click on Ok and close the dialog once the installation is done.

Similarly also install the package “JSON.Net” from Nuget Package Manager.

Read the rest of this entry »

Written by Mako

February 24th, 2013 at 8:12 pm

Posted in ASP.Net,C#,WebAPI

Tagged with , ,

Store Images in SQL Server

with one comment

In the following Post, we will look at how to save images in SQL server.  We will then read the image from the Sql Server and will render it on our page using a Http Handler.

First we will create a Database in Sql Server called ImageDB . Below are the table and stored procedure scripts which we are going to use in the application

The Image Table contains an ImageData column of type varbinary which will be used to store the images. Sql Server also contains an Image Data type but as per MSDN site, it is going to be removed from future versions so it is better to go with varbinary.

We will create a new ASP.Net Web Application in Visual Studio. The default.aspx page of our application will have the following markup

Type the Path of the image that you want to store in the Database in the textbox. Clicking on Save Button will save the image in the database and will retrieve the image from and display in in the page using Image Handler.

Here is the Code for the click event

In the save button click, we are using the ‘ReadAllBytes’ method of the File class to read the image file and store it as a byte array. We are then calling the Insert Image stored procedure to store the image in the Sql Server database.  We are returning the ID of the image from the stored procedure and passing it to the ‘ShowSelectedImage’  function which will display the image.

We are going to create an HttpHandler which will get the image from the Database. Right Click on the solution and select Add -> New Item -> Generic Handler and name it ‘ImageHandler.ashx’ .

We are using ImageHandler so that we can dynamically generate images instead of reading the image from the Database, saving it on a file and then displaying it.

An HttpHandler derives from the IHttpHandler interface and implements “ProcessRequest()” function and “IsReusable” property. Here is what the code for out ImageHandler.ashx will look like

The “ProcessRequest()” method get called for each request and we are passing the ID of the image to the handler as a query string. The “GetImageFromDB” will call the stored procedure and return the contents from the Database.

We will write the Byte contents of the image to the Response Stream using the Response.BinaryWrite method.

To use an HttpHandler, we will have to register it in out Web.Config file.

In the Type we are mentioning the classname and the assembly name combination.

Here is the Code for “ShowSelectedImage” which calls the HttpHandler.

We are setting the ImageURL property of the Image to point to handler and we are passing the id of the image as query string.

Any image that you upload will be stored in the SqlServer Database and will be retrieved and displayed dynamically.

For running the solution, make changes the “ConnectionString” property as per your system configuration. You can also find the Database script in the attached zip solution as ‘Script.sql’.

Download Solution

Written by Mako

February 4th, 2012 at 7:37 am

Posted in ASP.Net,C#,Sql Server

Tagged with , ,