How to format texts of TextBlock using xaml in Windows Phone

Sometime you may want to write formatted text in your app to give more pleasant for eye of users or to get more attention from user, sometime you may also want to display text where first few part should be bold, colorful with big font size and rest part is normal.

As for example :

Hello World ! I am learning how to format texts 

Here, we can see a sentence is mixed with different format of texts. Some part is in Bold and Colorful, some part is normal and some part is bold with underline as shown in below pic.


from above figure, we can see that  “Hello World” is in Red Color, “I am learning” is in italic font and “How to format text” is in Bold with underline text style.

Code for format TextBlock

To show formatted text i.e. text having multiple styles or colors in a single line in your windows phone apps then we can easily do so with a TextBlock

    <TextBlock HorizontalAlignment="Left"  TextWrapping="Wrap" VerticalAlignment="Top" Width="480">
                    <Run Text="Hello World! " FontSize="18" Foreground="Red" FontWeight="Bold" ></Run>
                    <Run Text="I am learning " FontStyle="Italic" ></Run>
                    <Run Text="How to format text." FontWeight="Bold" TextDecorations="Underline"></Run>

We can also change the font style of selected part of a line by using FontFamily property of TextBlock.

    <TextBlock HorizontalAlignment="Left"  TextWrapping="Wrap" VerticalAlignment="Top" Width="480">
                    <Run Text="Hello World! " FontFamily="Verdana"  FontSize="18" Foreground="Red" FontWeight="Bold" ></Run>
                    <Run Text="I am learning " FontFamily="Times New Roman" FontStyle="Italic" ></Run>
                    <Run Text="How to format text." FontFamily="Algerian" FontWeight="Bold" TextDecorations="Underline"></Run>

Here is the output of above code.


Posted in C#, CodeProject, Windows Phone, Windows Phone App | Tagged , , , , | Leave a comment

How to pass values between pages in Windows Phone 8 using PhoneApplicationService

It is obvious that if your application redirect(navigate) from one page to another page, you will frequently want to pass values between these pages. Lets consider a example where a user need to pass value of textbox from one page to another page, like when a user enter something in textbox of page2 and would like to see same entered(from page2) value in textbox of page1. We can pass information between pages in various ways but here in this article we are going to use PhoneApplicationService to pass values between pages in Windows Phone 8.

Using PhoneApplicationService to accomplish task of passing values between pages is not a cumbersome task. We should know what is PhoneApplicationService before learning what is the actual use of PhoneApplicationService to pass values between pages.

PhoneApplicationService –

The PhoneApplicationService is a class of Microsoft.Phone.Shell Namespace. The PhoneApplicationService class provides access to various conditions of the application’s lifetime. This includes management of the application’s idle behavior and management of the application’s state when it becomes active or inactive.
Read More : MSDN-PhoneApplicationService Class

Using PhoneApplicationSerivce States –

PhoneApplicationService.State Property state that it is used to Gets the dictionary used for passing an application’s state between invocations.

Each and every Windows Phone application containing PhoneApplicationService which defined in Microsoft.Phone.Shell. Every application has its one PhoneApplicationService defined in its App.xaml file of a Windows Phone project. We can use this service’s property called “State”. These “States” are all transient data which means they are available only for one instant of your application which means once you restart your application these states will be lost. Also we should not try to create a new PhoneApplicationService instead we must try to use the service from the current instant. Which can be retrieved using this code:


Note – Windows Phone applications are deactivated when the user navigates to another application. When the user returns to the application, by using the Back button or by completing a Launcher or Chooser task, the application is reactivated. An application can store transient application state in the State dictionary in the handler for the Deactivated event. In the Activated event handler, an application can use the values stored in the State dictionary to transient application state. (Reference MSDN)

To Use PhoneApplicationSerivce States to pass information –

 protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
            // Called when a page becomes the active page in a frame
            // Text is param, you can define anything instead of Text but remember you need to further use same param.
            PhoneApplicationService.Current.State["Text"] = txtboxvalue.Text;

to go back to first page


Note – Page.OnNavigatedFrom method Invoked immediately after the Page is unloaded and is no longer the current source of a parent Frame.

To Use PhoneApplicationSerivce States to retrieve information –

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
            if (PhoneApplicationService.Current.State.ContainsKey("Text"))
                txtvalue.Text = (string)PhoneApplicationService.Current.State["Text"];


to navigate next page

  NavigationService.Navigate(new Uri("/Page2.xaml", UriKind.Relative));

Note – Page.OnNavigatedTo method Invoked when the Page is loaded and becomes the current source of a parent Frame.

How is PhoneApplicationService.Current.State remembered
The PhoneApplicationService class plays a major role in the tombstoning game, since it exposes the Launching, Deactivate, Activated, and Closing events that have the corresponding methods (which you already saw) in the App.xaml.cs file. The objects are serialized (using DataContractSerializer) and then kept in memory. This is different (and faster) compared to IsolatedStorage which saves the data to disk.

When you use give code and debug the application, the emulator windows will launch up and run application as shown in below pics –


Demo of passing value between pages

Download Source Code
Try source code to Learn more. Click Me!

Posted in C#, CodeProject, Windows Phone | Tagged , , , , | 2 Comments

What to Choose between XAML and JavaScript for Windows Store apps development.

If you are novice in windows store app development then it is obvious to get stuck on the question which platform is best for metro apps development or which one is suitable for you to code and build metro apps ? As developers new to Windows 8, we’re having a hard time choosing between C#/XAML and JS/HTML for our upcoming app development.
I still remember the day when I started to learn coding in .Net framework 2.0 and an overarching question was echoes about new application development C# or VB.NET ? Now from .NET Framework 4.5, developers have a new rivalry to choose: XAML/C# or HTML5/JavaScript ? and its quite fact that Making this new choice has farther reaching implications than the prior C# or VB.NET decision.

The Landscape of Modern App Languages
You can use many languages to create Windows 8 apps and to interact with WinRT. Here are the available language sets for Windows Store app development:

Language Familiarity
To develop Windows Store apps, language will probably be the biggest factor in decision. If you have no experience with web development then C#/XAML will be easier for you. XAML developers will see a lot of familiarity to Silverlight or WPF in Windows Store development but there are plenty of difference as well.
In same way if you are good on web development, love to code using JavaScript, and are just more comfortable on the client-side of things then HTML5/JavaScript is for you. A HTML developers will be familiar with how the markup looks but will need to learn the WinJS specific classes and data attributes.

Concept of HTML and JavaScript in Windows Store apps.
HTML5 and JavaScript are open web standards and that’s why they work in nearly every browser in the same way. That is the reason why you can develop HTML5 applications for Mac OS and port it without writing numerous lines of coding to Windows. There are a lot of JavaScript functions, that are only supported by IE10(Internet Explorer 10) (in fact, nearly every function you can find in the WinJS-File).
If you want to port Win 8 Application to any other Operation System, you have to review your code and need to write extra codes in order to target the desired platform.

Concept of XAML and C# in Windows Store apps.
XAML( Extensible Application Markup Language) is an XML-based markup language developed by Microsoft. XAML is the language behind the visual presentation of an application that you develop in Microsoft Expression Blend, just as HTML is the language behind the visual presentation of a Web page. Creating an application in Expression Blend means writing XAML code, either by hand or visually by working in the Design view of Expression Blend. XAML is .NET and so it is fixed to this Framework. You cannot easily port a XAML Application to a other platform. Therefore, XAML and C# is very good implemented into Visual Studio (Intellisense,Blend and Design-Views) and has some nice features like LINQ(Language-Integrated Query), DataBinding, Resolution Independence, Debugging. whereas C# is nothing more than a language which is used to write code behind file to manipulate and make UI(User Interface) in actions.

When and Why I should choose HTML/JavaScript or XAML/C# for Windows Store apps ?

Personal Experience and your Interest
I am repeating again, If you have no experience with web development but you know how to code using C# then C#/XAML will be easier for you. XAML developers from both the Windows Presentation Foundation (WPF) and Silverlight camps continue to write the same declarative XAML for the UI layout alongside C#, Vb.Net. In sameway if you have Interest in web development, love to code using JavaScript, and are just more comfortable on the client-side of things then HTML5/JavaScript is for you. A HTML developers will be familiar with how the markup looks but will need to learn the WinJS specific classes and data attributes.

Tooling Support
Blend for XAML offers a lot of the standard XAML editing features XAML developers/designers have come to expect but doesn’t offer much beyond that. Since WPF, designers and developers have enjoyed Expression Blend. Expression Blend provides a visual and designer interface to accomplish complex actions and interactions in XAML UIs.

Note : Blend for Visual Studio 2012/2013 provides visual authoring for Windows Store apps, with support for both XAML and HTML.

The XAML functionality is similar to previous versions of Blend, so if you’ve used previous versions you’ll feel right at home. In addition to rich support for Windows Store app development, Blend for Visual Studio 2012/2013 also supports enhanced compatibility with the XAML Designer in Blend.
whereas, Blend support for HTML represents a new and innovative kind of authoring environment for HTML, So If you are new to blend then it would be tedious task to work.

For more details and differences : Design Windows Store apps using Blend for Microsoft Visual Studio 2013

Third party library support
You can find lots of libraries that is written for C# and Javascript. Many of these libraries will work just fine with no change necessary. so if your concern is for third party library support then you don’t need to worry.
Read more : Code libraries, toolkits and other sources of help

If your app target OOP(Object Oriented Programming)
The key part of OOP Inheritance, Polymorphism, Abstraction and Encapsulation, unit testing architectural reuse, and pervasive design patters are completely supported by OOPL like C++, C#, and XAML/C# is best option to make apps that targets OOP in comparison to HTML/JavaScript. C#/Vb.Net has everything and full set of features including symbolic refactoring, complex metrics and superior code analysis.

Resolution Independence
Your app can have resolution Independence either you choose to write code in C#/XAML or HTML/JavaScript, but the prime factor is that HTML does not get to resolution independence as quickly as XAML (or as transparently) does but you can certainly accomplish it in HTML.
XAML is cool because XAML’s resolution independency, it looks completely the same – just clearer, sharper. Size values in XAML are not pixels, they are device-independent units.

JavaScript lover always claim that JavaScript is better in terms of Interoperability because it can cross communicating with other applications. Just because of Dynamic features, JSON and being light in nature we shouldn’t say HTML/JavaScript is better than XAML/C#.
C# can speak in same way! In other words C# can do all of that too. 🙂 We shouldn’t forget C# has DLR(dynamic language runtime). Any of the C# objects are one small serialization step away from becoming the same JSON object that a JavaScript app uses and likewise in the other direction. Just because C# application is fat and little bit complex with business logic, it does not mean it can’t communicate lightly with the outside world.

Platform Target
If you are making apps that need to be targeted only for Windows 8 then C#/XAML is best option but if you are looking to make an app that is targeted to run on more than one platform then HTML5/JavaScript option would be worth.
Read for more details : Building Cross-Platform Apps with HTML5

Functionality of Bind to anything
Data Binding is one of the greatest and most powerful features of XAML (WPF, Silverlight, Windows Phone or Windows 8) compared to other traditional web and Windows app technology in .Net. The power of XAML really starts at its native ability to data bind. Nothing data binds like XAML – one way, two way, one time, and to almost any property. Not only is it built-in, not only is it powerful, not only is it fast, but it’s simple.
For JavaScript lover – They Can use WinJS Framework and KnockoutJS both support ObservableLists that allow you to do the same but the performance is not the best.
Read more : 2 Great JavaScript Data-Binding Libraries [ For HTML/JavaScript ]

Raw Graphics
If raw graphics performance is a concern for your app, then going C++/DirectX ends up being your only real choice.
still have doubt ? If yes read below the feedback from Labs
We’ve been developing highly interactive graphical applications using markup languages like XAML and HTML for some time. But when there’s a demand for high performance, we turn to DirectX. And to test the limits, we developed a proof of concept shown in this video under Windows 8 as a DirectX app.
Read more-Gain maximum graphics performance with DirectX.

Which is good between C#/XAMl and C++/XAML ?
First let be clear that the XAML rendering engine is pure C++ / COM whether you’re using C++ or C#. Since .NET and C++ use the same WinRT framework, there probably won’t be too much difference in interacting with the XAML/WinRT platform.
From a XAML perspective it becomes a language choice. The XAML UI stack is the same regardless of which code language you choose here. Depending on your goal of the app it might make more sense to use C++ or C#.
The great advantage of using C++/XAML is if speed is important to your project. The advantage of C#/XAML is that is much more easier to code.

What Should I do ?

I should first ask: “What are my needs?”

Target – Multi-Platform
Choice – HTML5/JS

if your needs are to target the mobile world, and to get your mobile application to as many people as possible, I’d suggest going HTML/JavaScript instead of XAML/C#/VB.Net. More of your core logic will translate across the platforms when you start looking at Android or WebOS or Blackberry development using tools like phoneGap, etc.

Target – Windows Ecosystem
Choice – XAML/.Net

if your app is only targeting windows platform. I would suggest XAML. XAML gives you full access to what Microsoft has to offer. HTML5 has some limitations that XAML does not have. XAML does have a learning curve, but its worth it, if your target is windows.

Tatget – High Performance
Choice – Native (XAML/WinRT)

if your app target 2d,3d graphics and need great performance. I would suggest You should go native. Which in this case is XAML + C++/Cx (though you may be able to get away with mixture .Net managed code depending on need). Even if you were to look at porting your app to other devices in the future, you’d probably go native on each of those platforms, for performance.

Some Feedback to give more ideas to make better decision

Windows 8: Top 10 Reasons Why I Choose HTML5 Over XAML
Windows 8: 15 More Reasons why I choose XAML over HTML5
Why I Prefer to Build My Windows Store Apps in HTML
Windows 8: Top 10 Reasons why I choose XAML over HTML5

For me, I love both language(C#/XAML and HTML/JavaScript) equally and here I am not underestimating the power of JavaScript and HTML5 but when matter comes to choose a platform to make a Windows Store app, personally I would definitely choose C#/XAML because of my skills is more familiar than JavaScript. 🙂 If you have same question for language opting then don’t worry. Many have same query. You are in an interesting situation, where your mind is on blank slate. Your choice may be different. If it is, make sure that you have valid reasons for your choice. Not all choices are equal. You can choose platform as per your skills familiarity and based on app requirement.

Posted in .Net, C#, CodeProject, Windows 8 | Tagged , , , | 3 Comments

How to solve issues while installing the NuGet AdDuplex pack into the windows phone 8 project.

Today I came through a weird problem while installing NuGet AdDuplex control to my windows phone 8 project. I tried almost everything but couldn’t figure out the actual reason to not getting installed AdDuplex pakage in my project.

Error –
This is what error I was getting while adding NuGet AdDuplex pack


even I tried to install AdDuplex package from package manager console( VS 2012 Tools – library package manager -Click package manager console ) by using below command but got same error as mentioned earlier.

PM>Install-Package AdDuplexWindows

Solution – We can solve this weird problem just by Updating NuGet in Visual Studio. Sometime updating NuGet can solve the compatibility issues.

You can update NuGet using the Visual Studio Extension Manager. Navigate to the Extension Manager and click on the Updates tab to check for updates.

If there is a new version of NuGet you will see it in the list of available updates.


Click on Install button.
Select NuGet in the list and click Update. When the update is complete, close and re-open all open instances of Visual Studio.

Now again come to  Manage NuGet package manager and In the Online portion search for “AdDuplexWP8” and click install


Now we can see that AdDuplex package is successfully installed and existing in reference of Windows Phone 8 project.



Keep learning and keep sharing :)

Posted in CodeProject, Tips&Tricks | Tagged , , | Leave a comment

Windows 8 Metro Style App Development Useful links for Beginner

Metro apps are touch-screen-friendly apps written especially for Microsoft’s WinRT programming interfaces.
ARM-based Windows RT devices, such as the Surface tablet, will require Metro apps. Devices running Windows 8 are expected to be able to run both Metro apps and Win32 apps. According to Microsoft developer Jensen Harris, these apps are to be optimized for touchscreen environments and will be more specialized than current desktop applications. Apps can run either in a full-screen mode, or be snapped to the side of a screen. Apps can provide toast notifications on screen or animate their tiles on the Start screen with dynamic content. Apps can use “contracts”; a collection of hooks to provide common functionality that can integrate with other apps, including search and sharing.
Windows Store apps run within a new set of APIs known as Windows Runtime, which supports programming languages such as C, C++, Visual Basic .NET, C#, along with HTML5 and JavaScript.

The following links are useful to learn the Windows 8 Metro apps :

MSDN Tutorials

Windows 8 Metro Style App Development Official Website

MSDN always would be a great place to start.

Getting started developing apps for Windows 8

Windows Metro App Development Phase

Language Based Tutorials
For C#/VB and XAML –
For HTML/JavaScript –
For C++/XAML –
For C++ and DirectX –

Videos Tutorials

Channel 9 –
CodeProject TV –

Microsoft Virtual Academy –
Developing Windows Store Apps with HTML5 Jump Start
Planning and Preparing for Windows 8
Windows 8 UX Design Jump Start

Sample Codes

This is not a book but set of codes that may help you understand the basics of Windows 8 Application Development. These codes also contain Samples for Javascript and C++.

Windows 8 Metro Apps Templates

Free ebook: Programming Windows 8 Apps with HTML, CSS, and JavaScript
The PDF version of the Apress book Metro Revealed: Building Windows 8 Apps with HTML5 and JavaScript by Adam Freeman is available as a free download from Microsoft


Posted in .Net, C#, Windows 8 | Tagged , | Leave a comment