How to Detect if Battery Saver is Currently Enabled on Windows Phone 8

To detect when Battery Saver is enabled you must check the PowerManager.PowerSavingModeEnabled property. Because this property is only available for devices running Windows Phone 8 Update 3, you must access this property via reflection after checking the device OS version. The following example shows you how to detect if Battery Saver is currently enabled.

Limit Input to Two Decimal Places in C#

Sometimes you need to restrict the input to two decimal places in C#. The following function returns true if the input is valid, otherwise it returns false.

You will need to listen to a Key_Down event handler for your TextBox and put the following code in it. Setting e.handled to true prevents the current character to be input in the TextBox.

How to make TextBox lose its focus in Windows Phone 8.1?

This method is ugly but it works. The key part is IsTabStop property. If I don’t touch it – the keyboard disappers for a fraction of a second and reapears again.

Makes virtual keyboard disappear when user taps enter key.

You just need to call the LostFocus method and pass the name of the TextBox control as a parameter to make it lose focus.

Get a verified Dream Spark account.

Students can get verified and download free softwares through their dreamspark accounts. Here’s how you can get yourself verified.
Visit: https://www.msacademicverify.com/AVE/Verify/NewDefault.aspx?lc=1033&wa=wsignin1.0

There are various ways to get verified.

  • Using a college/school email id: A email id issued by your school/college with .edu domain
  • School account
  • Using a International Student Identity card
  • Using a verification code: Ask your college’s MSPs to provide you a verification code

If you don’t have any of these then supply a college document. Acceptable documentation is dated student ID, current progress report, current dated class schedule, or acceptance letter to the school of higher education.

Here’s how you could fill it up.

college-id

Dictionary in C#

Dictionary<TKey, TValue> provide fast lookups, based on keys, to get values. With them, we use keys and values of any type, including int and string. Dictionary is used with different elements. We specify its key type and its value type (string, int).

Define a Dictionary

We specify its key type and its value type (string, int).

Add elements to a Dictionary

Here’s how to add elements to a Dictionary.

Read and update existing values

To read a value use the following statement.

To update an existing value simply use the following.

Check if a key exists in a Dictionary

This sees if a given string is present in a Dictionary.

Iterate through a Dictionary

Here we loop over KeyValuePairs in a Dictionary. With collections like Dictionary, we must always know the value types. With each KeyValuePair, there is a Key member and Value member.

Remove elements from a Dictionary

Here’s how you can remove elements from a Dictionary.

Count elements in a Dictionary

This computes the total number of keys in a Dictionary. This is simpler than accessing the Keys property, or looping over the Dictionary to count it.

Clear all elements from a Dictionary

We can erase all pairs with the Clear method.

Check if a value exists in a Dictionary

This method lacks the constant-time look up speed of ContainsKey. It instead searches the entire collection.

Get all keys of a Dictionary

Here we use the Keys property. We then look through each key and look up the values.

Get Root Domain and Favicon from URL in C#

In this quick tip I will show you a simple way to retrieve Root Domain and favicon icon from URL using C#. Its quite useful if you are building some kind of a newsreader app and wish to display the source of a news article in a proper way. Use the displayRootDomain method that takes url as input and gets the root domain and favicon.

This works for most URLs but I can’t guarantee if it will work for all links.

Binding JSON data to a LongListSelector

Pre requisite (Part 1) of this article was published here: Deserialize Json data using Newtonsoft Json.NET library

If you have successfully parsed your JSON data from a local/hosted file or read data from a server, now you need to show it to the user. To do use we use the concept of DataBinding.

What is DataBinding?

According to MSDN, Data binding is the process that establishes a connection between the application UI and business logic. If the binding has the correct settings and the data provides the proper notifications, then, when the data changes its value, the elements that are bound to the data reflect changes automatically. Data binding can also mean that if an outer representation of the data in an element changes, then the underlying data can be automatically updated to reflect the change. For example, if the user edits the value in a TextBox element, the underlying data value is automatically updated to reflect that change.

Now let’s see a practical example to show this feature. We will continue with the app from previous post – Deserialize Json data using Newtonsoft Json.NET library


 

1. a. Creating a template

The first step is to create a general template to show the data. Let’s take a look at our sample JSON dat
a used in the app. Now for a simple template, let’s assume we need to show all 5 types of information form the Json data. Let’s create a StackPanel for each information tag like this.
This results in the following format:SP1

1. b. Creating a Template

Let’s use the same style for all the tags and create a unified StackPanel containing 5 such smaller StackPanel.This is what our template looks like now:

SP2

2. Creating a LongListSelector with a DataTemplate

Now we need to show multiple objects from the JSON, we will create a LongListSelector and provide it with a DataTemplate (which the above StackPanel we created!) to show our JSON. The following code does the magic:
The result looks like a empty StackPanel but don’t worry everything is still there and it’s fine.

3. DataBinding

This the important step. We need to careful to bind correct elements to correct UI elements.  We need to bind the LongListSelector with the name of object in RootObject class. Our classes are these:
Thus we need to bind, LongListSelector with mydata from line 12. Please be careful as to make sure to bind the RootObject with the LongListSelector. And then we bind the various tags like id, name, location etc to respective variable of class mydataYou can see the binding syntax from the code snippet below:
Please take a moment to observe various Data Bindings carefully before proceeding.

4. Set the DataContext of LongListerSelector to parsed JSON

This is the final step. After parsing the JSON data, set the DataContext of LongListSelector to your parsed JSON data like:

Feel free to be creative with the DataTemplate you create. You can now use this knowledge to download Json files from servers to create a dynamic app, use some web APIs to exchange Json data and make an awesome app.

And you’re done! This is what final result looks like:

wp_ss_20150304_0004

 

 

 

 

 

 

 

 

 

 

 

Download source code from Github

 

Visit my personal website chauhanvaibhav.net

Handling the Back Button in a Windows Phone 8.1 app

If you are developing an app using the Universal app template then you may have noticed that the app terminates on pressing the hardware back button.
Lets navigate from Page1.xaml to Page2.xaml by using a button with the click event code:

this.Frame.Navigate(typeof(Page2));

When we are on Page2, and the hardware back button is used, the app closes without an exception or anything. It just returns to the start screen. This is new to Windows Phone 8.1. If you create a new Hub Universal App using a VS2013 template, you’ll notice a class in Common folder called a NavigationHelper.
This NavigationHelper gives you a hint how to properly react to back button press. So, if you don’t want to use the NavigationHelper, here’s how to get the old behavior back:

You can also do it on app level, to avoid having to do it on every page:

 
if you wish to disable the back button in your application you can view the following post
 
How to disable Hardware Back button in Windows Phone App