Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.
Create simple overlay with UserControl in Windows Phone
This article explains how to create a simple overlay using UserControl in Windows Phone.
Overlays can have a wide range of applications, be it a simple prompt or displaying a progress indicator. Using UserControl for implementing it gives us feasibility to easily construct any type of overlay. This article demonstrates how to implement a sample progress overlay.
The following steps describe how to implement it:
- Create new Windows Phone project from Visual Studio.
- Add new UserControl named OverLay.xaml in current project.
Copy and paste following code in your OverLay.xaml file.
<Grid x:Name="LayoutRoot" Background="Transparent">
<ProgressBar IsIndeterminate="True" Foreground="Orange" Height="50" Width="480" VerticalAlignment="Center"/>
<TextBlock Text="Wait" Foreground="Orange" HorizontalAlignment="Center"/>
LayoutRoot is populated with ProgressBar and TextBlock but you can put any UI controls here that you want to show on the overlay. For example, if you want to show a simple About prompt then you may put a TextBlock and an Image with custom size.
In code behind file of Overlay.xaml initialize the size of LayoutRoot to the value you want. The following code shows how the height and width of LayoutRoot are set to the height and width of the screen.
this.LayoutRoot.Height = Application.Current.Host.Content.ActualHeight;
this.LayoutRoot.Width = Application.Current.Host.Content.ActualWidth;
SystemTray.IsVisible = false; //to hide system tray
Now open MainPage.xaml.cs file and copy and paste the following code in it:
public partial class MainPage : PhoneApplicationPage
private Popup popup;
this.popup = new Popup();
private void BtnStart_Click(object sender, RoutedEventArgs e)
this.LayoutRoot.Opacity = 0.2;
OverLay ovr = new OverLay();
this.popup.Child = ovr;
this.popup.IsOpen = true;
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (s, a) =>
worker.RunWorkerCompleted += (s, a) =>
popup.IsOpen = false;
this.LayoutRoot.Opacity = 1.0;
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
this.popup.IsOpen = false;
The BtnStart_Click event contains the logic of showing the progress overlay; when activating and deactivating the overlay you need to set the opacity of underlying page. In the DoWork method of BackgroundWorker, you can write the actual logic. In the current implementation, it just blocks the UI thread for some time.
The following screen shot shows the above implemententation of the progress overlay:
Finally don't forget to set IsOpen property of popup to false on OnBackKeyPress event, otherwise the overlay will continue to display even if you navigate to another XAML page.
You can find the complete source code below.