Archive

Archive for the ‘Windows’ Category

Windows 8 – Downloading File

Here is a snippet for downloading file using C#

    async Task<bool> DownloadFile(Uri source, StorageFile destination)
    {
        try
        {
            BackgroundDownloader downloader = new BackgroundDownloader();
            DownloadOperation download 
             = downloader.CreateDownload(source, destination);
            await download.StartAsync();

            return true;
        }
        catch
        {
            return false;
        }
    }

If you want to show progress indicator, then add a ProgressBar object into the UI. And use StartAsync().AsTask(Progress)

Windows 8 – Validate URL

Sample code for validating URL’s.

Invoke below method and pass the URL.

private async Task<bool> UriExists(string url)
{
    try
    {
        //Validate whether url has a valid uri pattern
        if (!Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
            return false;

        //Creating the HttpWebRequest
        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
        //Setting the Request method HEAD, you can also use GET too.
        request.Method = "HEAD";
        //Getting the Web Response.
        HttpWebResponse response = 
            await request.GetResponseAsync() as HttpWebResponse;

        if (response.StatusCode == HttpStatusCode.OK || 
            response.StatusCode == HttpStatusCode.Forbidden)
            return true;
    }
    catch(WebException ex)
    {
        HttpWebResponse r = ex.Response as HttpWebResponse;
        if (r.StatusCode == HttpStatusCode.Forbidden)
            return true;
    }
    return false;
}

Windows 8 – Display ToolTip

Tool tips are important for a user friendly application. In this post I am going to explain how we can set tool tip for Windows Store Applications

Option #1 – Using Properties Window

In design mode, open the control properties. And set the “ToolTipService.ToolTip” property

T1

Option #2 – By Editing XAML

In XAML mode, set control property as shown below

T2

Option #3 – Using Code Behind

        ToolTip textUriTip = new ToolTip();
        textUriTip.Content = "New ToolTip";
        ToolTipService.SetToolTip(usr, textUriTip);

T3

Windows 8 – How to save & retreive application settings

February 26, 2013 Leave a comment

Windows 8 store applications don’t have application configuration files. Hence storing application level settings is done in a different way.

Each application has an ApplicationDataContainer. Its mutable and specific for each application. System internally manages the data by isolating it from other applications settings. Hence its more secure & reliable.

Current architecture offers 3 types of storage

  • Local – Here data is stored locally on the device
  • Roaming – Here data exists on all devices
  • Temporary – Temporary data

Here are two methods which you can use for saving and retrieving application data.


using Windows.Storage;

bool SaveSettings(string key, string value)
{
    try
    {
        var applicationData = Windows.Storage.ApplicationData.Current;
        applicationData.LocalSettings.Values[key] = value;
        return true;
    }
    catch
    {
        return false;
    }
}
string RetrieveSettings(string key)
{
    try
    {
        var applicationData = Windows.Storage.ApplicationData.Current;
        if (applicationData.LocalSettings.Values[key] != null)
            return applicationData.LocalSettings.Values[key].ToString();
        return null;
    }
    catch
    {
        return null;
    }
}

Windows 8 Store App Development – Pre Requisites

January 20, 2013 Leave a comment

App Development for Windows 8 is all together a new story for developers. So I thought to outline the steps here so it will benefit the community.

  1. Windows 8 Operating System
  2. Visual Studio 2012
  3. Developer License

On my laptop I am using Windows 7, since its configured with all software I don’t want to do a fresh install or upgrade to Windows 8. So my options left are either use a VPC or dual boot windows 8. Dual booting needs a new dedicated drive. In order to create a new drive, I shrink one of my drive using Windows disk management tool. The new drive should have

  • Enough storage space to accommodate Windows 8 installation
  • Either Primary or Logical drive (Dynamic drives won’t work)

This setup helps me to keep my current environment intact and it give me the flexibility to play with Windows 8.

Installation Process was straight forward. And it completed pretty quickly. The installer was Windows Enterprise edition, hence I was unable to enter the product key. After searching on this topic, I found a command Slui.exe 0x3 which enables this. Read this KB article.

Next, installed Visual Studio 2012.

From Visual Studio, New Project

SelectProject

Once Visual Studio finishes creating the Project, it will try to get the developer license. First a popup window is displayed, on this we should read and accept the terms and condition. Next another popup asks for Microsoft logon credentials. Finally a success message showing the license information such as validity etc.

Normally the license is valid for 30 days, after that we can renew it for next 30 days and so on. On the other hand if you have a Store account, then the developer license validity is 90 days.

I started developing my first app on Windows 8. I will be explain my experience on next post.

Categories: C#, Windows, Windows 8