Category Archives: Silverlight


How to terminate your Windows Phone application programmatically?

21/08/2012 Update: 

There is another way to close the Windows Phone application without throwing exception see more details in my next post.

We want full power!

Its known that in Windows Phone API there is no easy way to terminate the current application from your code.

I guess makers of the platform did not wanted us developers to mingle with the application lifetime too much, phone should decide when an app is not needed and terminate it.

But i just simply cannot help myself, i always like to have full control, for example in my Offline Browser application i want to end the app if user presses the hardware Back Key multiple times in short time intervals.

Because there is this annoying situation when user goes navigating between the pages in your app (main screen, then settings, then about screen,

then again main screen etc) and then when he wants to terminate it he need to click the hardware back button N number of times in order to close it.

And even in some other situations its needed to force close the app, for example if some change in application settings requires app restart etc.

Show Us The Code!

So i decided to change this and wrote this simple AppLifetimeHelper that allows you to terminate your current app without any delay.

How it works is that it uses Application.Current.RootVisual and casts it to PhoneApplicationFrame that has RemoveBackEntry method that basically simulates

the users hardware Back Key press and then in endless loop it calls this method until the navigation back stack of the app is empty.

Then all we need to do is to call the GoBack method of the PhoneApplicationFrame and it will terminate the app since there are no more entries in the app Back Stack.

Simple or what?   8-)

So here is the code if you AppLifetimeHelper that you can freely use in your apps if you need this functionality:

using System.Linq;
using System.Windows;
using Microsoft.Phone.Controls;

namespace Roboblob
    public class AppLifetimeHelper
        public void CloseApplication()

        private PhoneApplicationFrame _root;
        private PhoneApplicationFrame Root
                if (_root == null)
                    _root = Application.Current.RootVisual as PhoneApplicationFrame;

                return _root;

        private void ClearApplicationNavigationBackStack()
            if (Root == null)

                while (Root.BackStack.Any())
            { }

And if you are really lazy today…

Here is the download link to the Visual Studio solution with Windows Phone application that uses this helper to terminate it self when a button is clicked…


Throttling Immediate TextBox Binding Behavior for Windows Phone

The learning curve of a Windows Phone developer

While developing Offline Web Browser for Windows Phone i had to build a small MVVM framework in order to keep the logic out of the views.

One of the first thing i found missing was a way to force immediate propagation of text entered in TextBox control to the databound property of my ViewModel.

By default, TextBox binding is triggered only when control loses focus, and this is kind of lame.

Code reuse is not a myth!

This is really old problem. It existed in desktop Silverlight from the beginning. And it made its way to the Windows Phone platform.

Fortunately I remembered that back in the days while experimenting with Silverlight MVVM framework i already solved this problem by creating a custom silverlight TextBox control so i decided to just reuse that code and create a Behavior that will force binding update to fire on each keystroke in TextBox.

That’s the beauty of developing a Windows Phone app – you can reuse most of the Silverlight code you built over the years :)

But then i noticed another problem. Because now each keystroke was propagating changes to my ViewModel, if user would type fast and i do for example some web service call on each property change – then i can have too many requests to the web service (one for each keystroke) but in fact i want to do a call only when user stopped typing for a while.

Fortunately, solution is very simple – we will use Reactive Extensions (Rx) since its perfect fit and its already built-in into Windows Phone.

We just need to reference Microsoft.Phone.Reactive.dll from GAC (no additional download needed) and we are half-way there.

OK, but show us some code!

Yes lets see how the code for the ThrottledImmediateBindingBehavior looks like:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Interactivity;
using Microsoft.Phone.Reactive;

namespace Roboblob.Mvvm.Behaviors
    public class ThrottledImmediateBindingBehavior : Behavior
        private BindingExpression _expression;

        public bool Throttle { get; set; }

        private double _throttleDelayInSeconds = 0.5;
        private IDisposable _currentObservable;

        public double ThrottleDelayInSeconds
            get { return _throttleDelayInSeconds; }
            set { _throttleDelayInSeconds = value; }

        protected override void OnAttached()

            if (Throttle)
                this._expression = this.AssociatedObject.GetBindingExpression(TextBox.TextProperty);
                var keys = Observable.FromEvent(AssociatedObject, "TextChanged").Throttle(TimeSpan.FromSeconds(ThrottleDelayInSeconds));
                _currentObservable = keys.ObserveOn(Deployment.Current.Dispatcher).Subscribe(evt => OnTextChanged(evt.Sender, evt.EventArgs));
                this._expression = this.AssociatedObject.GetBindingExpression(TextBox.TextProperty);
                this.AssociatedObject.TextChanged += this.OnTextChanged;

        protected override void OnDetaching()

            if (Throttle)
                if (_currentObservable != null)
                    this._expression = null;
                this.AssociatedObject.TextChanged -= this.OnTextChanged;
                this._expression = null;

        private void OnTextChanged(object sender, EventArgs args)
            if (_expression != null)

Simple Is Beautiful

Our Binding class has bool property Throttle where we can enable/disable throttling and also a ThrottleDelayInSeconds where we specify after how many seconds later after user stops typing our property is updated.

In the OnAttached – we just hook to the TextChanged and each time user types something we update the binding source.

But if Throttle is set to true, we hook to the TextChanged event over Reactive Extensions and we setup throttling so that binding do not trigger if changes are too fast.

In this only after changes stop firing for the time defined in ThrottleDelayInSeconds, only then binding update is triggered.

Off course we do a little cleanup in the OnDetaching as all good coders :)

What about the Xaml???

Yes in Xaml we can use it like this:

            <TextBox Text="{Binding SearchCriteriaText, Mode=TwoWay}">
                    <RoboblobBehaviors:ThrottledImmediateBindingBehavior Throttle="True" ThrottleDelayInSeconds="1" />

For those who are lazy to type I’m attaching a zipped source code of simple Windows Phone Mango project that is demonstrating the usage of the behavior.

Btw does somebody even remember how it was to type source code from printed computer magazines int your 8bit computer?

Probably not :D

Well at least I hope that someone will find this useful.

Until next time, happy coding!

Using Offline Browser for Windows Phone as Instapaper client



Lets set some expectations here. We wont be adding new items to reading list or removing items or anything similar.

We can use our desktop PC for that.

Here we will just explain how to use Offline Browser for Windows Phone to get all those links from reading list via RSS feed and download them to the phone so we can read them whenever we want – even when we are offline.

So first we need to add the Instapaper unread items Rss feed into Offline Browser as a Links Feed.

Here is how you can do that:

1. Open the Offline Browser for Windows Phone and on the Browser page go to and login to your account.

Once you are logged in, you will be redirected to this url: which is your unread items list.

2. Now we should find the RSS feed of your unread Instapaper items and use it as links source (Link Feed) of the Offline Browser app.

The RSS feed icon and link are at the bottom right corner of the web page, take a look at the screenshot below:

3. Now long tap that link ‘This folder’s RSS’ and context menu of Offline Browser will appear:

4. Click on the “Add Link as Links Feed” item and RSS feed of your unread instapaper items will be added as Links Feed for the Offline Browser.

5. Now lets enable Links Feed synchronization in settings:

There we see the RSS feed from instapaper added to the app as Link Feed and we see that background downloading of the URLs from the feeds is enabled and done every 2 hours.

So even if our application is not running, a background task will retrieve the RSS feed and if there are new links they will all be added to the download queue.

When this happens you will get a toast notification and live tile of the Offline Browser will show number of new links retrieved from all feeds.

Here is how this looks on the Windows Phone start screen:

6. When we click on the Offline Browser icon and there are new items to download we are immediately taken to the download queue screen.

There you can simply click on the Sync button and all new links from download queue will be downloaded.

(app will also check if there are some newer items in the feed and retrieve them together with the items that are already in the download queue).

Once pages are downloaded we can go to the saved pages screen and open them in the browser screen:

Its important to say that once you go through this process and your instapaper RSS feed is added to the app, it becomes automatic and phone will download any links you add to your instapaper reading list.

So now that you know how you can use Offline Browser as your Instapaper client – go ahead, download it from marketplace and give it a try!


Offline Browser for Windows Phone is released


Here is some good news:

finally my Windows Phone application Offline Browser is available for download in the Windows Phone Marketplace!

Offline Web Browser? But who needs that?

Well actually i do. To be honest app was created (as many other apps) in order ‘to scratch an itch‘.

I’m an old information junkie and while traveling to work i like reading books and surfing the web, but often i don’t have internet connection or its very slow (for example when I’m in the subway).

Although there are many excellent E-Book readers for Windows Phone (one of the best is for sure Freda) i could not find a single offline internet web browser that would allow me to save full content of web pages to my phone and read them later when I’m offline.

This is how idea for Offline Browser for Windows Phone was born.

Here are some of Offline Browser current features:

  • save to your phone full content of web pages (HTML content with all images, CSS Style Sheets, JScript etc)
  • later view all saved pages even when you are offline
  • all links between saved pages work even when you are offline
  • save all the links that are on current page with one click (useful for online news/magazines that have many links to related content)
  • long tap gesture on link/image shows context menu with additional options for that link/image
  • automatic retrieval of links for download queue by adding RSS or Atom feeds as source (add your Instapaper or Delicious or any other feed)
  • download all queued links by Sync option
  • change Browser User Agent (mobile or desktop Internet Explorer, IPhone or any other custom string)
  • save pages via Mobilizer service (Instapaper, ReadItLater, Google Mobilizer etc)
  • page scroll position is preserved so when you later open saved page its scrolled down exactly where you were last time
  • its simple to use and can completely replace built in Internet Explorer browser since its uses same page rendering engine but adds more features

So now that we have an overview of what the app does, lets see how it looks and works:

The main browser page

First and main application screen is the Browser page and this is where most of the magic happens:

As you can see UI is minimal.

I wanted to be able to focus on the page I’m currently viewing and not on the UI elements of the app.

Its very similar to the stock phone browser and icons do not cover too much of the screen space.

The main feature of the app is the Save button at the bottom of the page (annotated in the screenshot with the red arrow).

By pressing this button the whole page is downloaded to the phone with all the content including images etc.






Sliding Menu on the bottom of Browser page:

When we click on the three dots in the lower right corner the sliding menu appears at the bottom with links to the other pages in the app and additional options available for this web page.








Browser Context Menu

When we do a long tap on any link or image on the web page a context menu appears with additional options available for link/image.

We can save all the links pointing to the same domain and share the link URL over phone email or social networks accounts.

Images can be saved to the phone Media Library.

Also you can add link as your link feed URL. So you can just long tap on some RSS/Atom feed URL and add it as a source for new links for your download queue. More on this feature later in this post.

Similar context menu appears when you long tap (hold) somewhere on a page – but with slightly different options that are available for current page.

Same menu can be invoked also from the bottom sliding menu when clicked on three dots icon.





Saved Web Pages screen:

All saved pages are displayed and managed on this screen. You can see a list of your downloaded web pages sorted by date in descending order.

Filter box on top can be used to narrow down a list to some specific pages if you have a lot of them.

On this screen you can long tap on a page in the list to get context menu with options for that specific page like Delete, Delete All and View.

By just clicking on a page you are taken to the browser screen and full content of this page is loaded from the phone storage without the need of internet connection.

Neat right?







So how do we automate this thing?

Yes i know, I’m also lazy. I don’t like having to go into the mobile app, browsing websites on the phone and saving pages for later viewing or typing long URLs.

So i came up with this solution:

When I’m at my desktop computer and i see a web page I want to read later on my phone i add it to my reading list or i bookmark it under special tag in my account.

Then i copy URL of these RSS feeds from Instapaper account or to Offline Browser application settings page as Link Feeds and these feeds will be used as source for new links that i will later download to my phone.

Here is how it looks in the application settings page for the Link Feeds:

 You can switch on/off background retrieval of the links from the feeds and you can set how often synchronization is performed.

 Below you can manually add more Link Feeds by typing their URL or pasting it into the text box.

 Its important to note here that if you switch on this option then feeds are synced in the background tasks even when your app is not running.

 Due to limitations in Windows Phone Background Tasks application does not sync full content of the pages from those URLs but only fetches their URLs and adds them to the download queue.

You can then see those links in the Download Queue page and download them all just by clicking on the Sync button.





 Application tile counter

When you are using background syncing of Link Feeds, application fetches new URLs in the background and notifies you via Toast notification and via Application Tile counter like this:

Windows Phone Offline Browser Live Tile counter on Home Screen

Here application has downloaded new links from all the Link Feeds and its showing us that there are 5 new web pages to be downloaded.








Download Queue

When we have some pages in the download queue then on application start we are immediately taken to the Download Queue page:

Windows Phone Offline Browser Download Queue page showing urls to download

This can be configured in the settings so that you are not redirected here when there are links in the queue so you can choose whats more logical for you.

Once we click the sync button on the top of the page, application starts downloading all the web pages from the download queue and saves them to phone.

Once they are downloaded you can access them from the Saved Pages screen.







Browser User Agent settings

Another feature of the app is that you can choose in Settings the Browser User Agent String to be used when browsing and downloading web pages:

Windows Phone Offline Browser Browser User Agent String Settings

You can pick one of the existing browser User Agent Strings like Mobile or Desktop Internet Explorer or IPhone or simply choose Custom and enter or copy/paste your own User Agent of choice.








Before going to sleep

So this was just a short introduction on what this handy application is and and what it can do.

If you want to test it go to the marketplace and download a trial version of Offline Browser (its fully functional trial with ads) and give it a try.

If you really find it useful well you might as well buy it, right?  ;)

In my upcoming blog posts i will explain some of the less visible features like how saving of page scroll position works etc.

Also expect some Windows Phone programming posts since i have a lot of tips and tricks to share so stay tuned and happy offline browsing!