Visually Located


Avoid InvalidOperationException when navigating in your Windows Phone apps

I have no idea how I get this error, but in every app I’ve published I get error reports in which navigation fails and throws an InvalidOperationException. The entire stack trace is similar to System.InvalidOperationException Navigation is not allowed when the task is not in the foreground.    at System.Windows.Navigation.NavigationService.Navigate(Uri source)    at MyWPApp.MainPage.OnListBoxItemSelected(Object sender, SelectionChangedEventArgs e)    at System.Windows.Controls.Primitives.Selector.OnSelectionChanged(SelectionChangedEventArgs e)    at System.Windows.Controls.Primitives.Selector.InvokeSelectionChanged(List`1 unselectedItems, List`1 selectedItems)    at System.Windows.Controls.Primitives.Selector.SelectionChanger.End()    at System.Windows.Controls.Primitives.Selector.SelectionChanger.SelectJustThisItem(Int32 oldIndex, Int32 newIndex)    at System.Windows.Contro... [More]

Performing an async operation in the OnBackKeyPress method and still exit your Windows Phone apps

The OnBackKeyPress method allows you to perform special logic when the user presses the phone back button. You usually override this method when you do not want the user to actually go back a page. An example of this is if you show a popup, when the user presses the back button you should close the popup. protected override void OnBackKeyPress(CancelEventArgs e) { base.OnBackKeyPress(e);   if (PopupIsOpen()) { e.Cancel = true; ClosePopup(); } } This workflow works great because everything is synchronous. You may have seen some apps that prompt you with a MessageBox asking if you want to exit. If you say yes the app exits and if you say no, you do not exit the app. protected override void OnBackKeyPress(CancelEventArgs e) { base.OnBackKeyPress(e); var result = MessageBox.Show( "Are... [More]

Making the Windows Phone Toolkit CustomMessageBox async

The Windows Phone Toolkit has a nice CustomMessageBox control that allows you to customize the button text of a MessageBox. This message box is nice but you must subscribe to an event for when it is closed. Microsoft.Phone.Controls.CustomMessageBox msgBox = new CustomMessageBox(); msgBox.RightButtonContent = "cancel"; msgBox.LeftButtonContent = "delete"; msgBox.Caption = "Are you sure you want to delete?"; msgBox.Title = "Delete message?"; msgBox.Dismissed += (o, eventArgs) => { // Do some logic in here. }; msgBox.Show(); This requires that your logic for user input to be either in a separate method or above the Show method like in my example. If the CustomMessageBox had a way to show it asynchronously, you could have your logic in one place. Luckily this is very easy with an extension method. public static class Toolk... [More]

Making the Windows Phone Toolkit ListPicker ItemsPanel display a WrapPanel

There are many things that should be part of the Windows Phone SDK and the ListPicker is one of them. There are so many aspects of the phone that use a control that the ListPicker was based on. One of these is the accent color picker. In Phone 7 the phone would display a vertical list of colors with the name of the colors next to it. In Phone 8 they changed this experience and went with a WrapPanel. If you want to to get this same experience, you’ll want to use the ListPicker from the Windows Phone Toolkit. The ListPicker will first display what appears to be a ComboBox, yet when you tap this ‘ComboBox’ it opens up and takes over the entire phone. This is all pretty standard and the ListPicker covers this perfectly. Where it fails us is when we want to change how the ListPicker stacks it’s items. This is generally done by changing the ItemsPanel of an ItemsControl. Unfortunately the ListPicker does not allow you to change the ItemsPanel. If you download or br... [More]

Using a custom UriMapper to navigate to a login screen

When writing a Windows Phone app you might need to prompt the user with login information. Maybe you want to know who in the family is using the app, or the most likely case is that you are trying to make an app that uses web services. When you want to give the user a login screen, you want this to be the first page that they see the first time they open the app. When they have already entered login information you don’t want to show the login screen and instead take them to the main page. There are a few ways to do this. One way that is very common is to place logic into the MainPage.xaml.cs that checks if a login is needed and if so, navigate to the login screen. When doing this you need to take care to remove any backstack entries from the NavigationService when you get to the login page. You do this because if the user hits the back button, you want them to exit the app, not go to your main page. There are two logical places to put the code for this. One in the OnNavigatedTo overr... [More]

Changing the background color of your pivot headers

Let’s say you’re trying to put some style into your app and you really want a nice background color to all of your PivotItem titles. How would you go about doing this? You’re probably thinking “Shawn, that’s easy, just modify the HeaderTemplate on the Pivot.” And I would probably agree with you. Let’s try that out. <controls:Pivot Title="MY APPLICATION"> <controls:Pivot.HeaderTemplate> <DataTemplate> <Grid Background="Red"> <TextBlock Text="{Binding}"/> </Grid> </DataTemplate> </controls:Pivot.HeaderTemplate> <!--Pivot item one--> <controls:PivotItem Header="item1"/>   <!--Pivot item two--> <controls:PivotItem Header="item2"/> </controls:Pivot> Looking at the design view of this gives We ... [More]

Enabling tilt on “non-selectable” items

While working an a recent app I wanted to enable tilt functionality onto a Grid that contained a few items. Unfortunetly I was unable to get the control to tilt. I tried setting all sorts of properties, but nothing worked. I downloaded the source and took a look at it, and the same time I asked the twiiter universe whether non selectable items could be “tiltable” Morten Neilson replied with exactly what I was seeing.  His suggestion was to change the source which is easy enough to do. Just add FrameworkElement to the list. static TiltEffect() { // The tiltable items list. TiltableItems = new List<Type>() { typeof(ButtonBase), typeof(ListBoxItem), typeof(MenuItem), }; } But I wanted to find a way to make this work without having to change the source for the WP7 toolkit. I love open source projects, I have two myself. But it’s a ... [More]

Creating a Custom MessageBox for Windows Phone Applications

UPDATE: See a new post to get the latest code for the custom MessageBox. UPDATE: After posting this blog I found out about the message box within the XNA framework. This does allow for custom button text which is what I was trying to accomplish. However, the user experience is different than what you get from the message boxes within the native phone applications (eg: deleting a text). With the native message boxes, the application bar disappears, but with the XNA message box, it gets greyed out. It’s the little things that matter. Also within the XNA framework you cannot add additional components to the message box. For example, you might want to add a “Do not show me this again” option within the message box. While using a Windows Phone you get prompted every once in awhile by a message box. Custom Applications have them, even apps native to the phone has them. When deleting a text message or a contact you get a nice prompt asking you if you want to delete ... [More]