Windows Phone 7 Navigation is not allowed when the task is not in the foreground

I hit the following issue bouncing around an app that utilised the SmsComposeTask today.  I have simplified the code to the following for demonstration purposes:

private void OnAdClick(object sender, MouseButtonEventArgs e)
{
   SmsComposeTask sms = new SmsComposeTask();
   sms.To = "111";
   sms.Body = "blah";  �
   sms.Show();
}

Issue:

When clicking the button that creates the SMS compose task in quick succession the following occurs.

InvalidOperationException

Navigation is not allowed when the task is not in the foreground. Error: -2147220989

at Microsoft.Phone.Shell.Interop.ShellPageManagerNativeMethods.CheckHResult(Int32 hr)
at Microsoft.Phone.Shell.Interop.ShellPageManager.NavigateToExternalPage(String pageUri, Byte[] args)
at Microsoft.Phone.Tasks.ChooserHelper.Navigate(Uri appUri, ParameterPropertyBag ppb)
at Microsoft.Phone.Tasks.SmsComposeTask.Show()
at Demo.OnAdClick(Object sender, MouseButtonEventArgs e)
at System.Windows.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)
at Microsoft.Xna.Framework.Input.UnsafeNativeMethods.CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam)
at Microsoft.Xna.Framework.Input.SafeNativeMethods.CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam)
at Microsoft.Xna.Framework.Input.WindowMessageHooker.Hook.WndProc(IntPtr msgWnd, UInt32 msg, IntPtr wParam, IntPtr lParam)

Cause:
The cause is that the first click will launch the SmsComposeTask causing the application to be deactivated i.e sent to the background, with the second click being handled after the deactivated event has occured so its just a matter of cleaning up your click handler.

Kind Regards,
Nick Harris

35ºC, 95ºF bedroom is like a sauna today, difficult to concerntrate, bring on autumn:)

Free Event The SAUG OO Basics and SOLID Design principles

Grace Hotel , Kiralee or Pinaroo Function Room77 York stSydney,NSW. 2000
Omar has been a coder since he can remember! BASIC, PASCAL, COBOL, JAVA, .NET and alien languages unknown to human kind. From being a Computer Science student to Microsoft’s .NET ambassador to .NET consultant and then Enterprise Architect at Datacom, Omar has enjoyed every step of the journey one line of code at a time. Omar has worked through ASP.NET, Windows Forms, WPF, SharePoint, BizTalk, TFS and CRM projects and considers himself a Microsoft technologies veteran. He was also a speaker at TechEd events and various other MS events and user groups in Sydney, Adelaide, Brisbane, Cairo and Dubai. Omar enjoys working across multiple projects at once and his motto is “Power to the Developers”. He spends his day throwing more architectural tasks on fellow architect Paul Glavich instead of doing it himself. If you want to annoy him just say web applications are better than rich client ones.
- Thanks
Note: If anyone wants to have a play with a Windows Phone 7 device i will have mine there, please feel free to grab it and have a play – Nick

WP7 Nights at the Round Table – Jan 25th

Hi there Windows Phone 7 Developers in Sydney Australia,

Following the success of our xmas Drinks we are going to make this a monthly event :) .  Time to get out from infront of the computer and into the bar to talk about windows phone 7 dev :)

Event:
WP7 Nights at the Round Table – Jan 25th Sydney

Purpose:
Let’s get together for some drinks to trade WP7 Dev stories, demos or seek free advice from other devs to help get your app off the ground and into Marketplace. This event will be informal, around bar tables, so bring along your device or laptop if you wish to show people what you have been up to.

Let us know your coming:

If your on LinkedIn please indicate your attendance here – http://events.linkedin.com/Windows-Phone-7-Dev-Drinks-Jan-25th/pub/536053 or in the comments section below.

Date, Time, Location:

6-8pm
Tues 14th Dec 2010
City Hotel,
Corner of King and Kent St, Sydney CBD.

Hope to see you there,

Nick Harris :)

Check if a Capability is enabled in WMAppManifest on Windows Phone 7

While Microsoft provides the handy Capability Detection Tool to determine what Capabilities are required for your application there are scenarios, one being that you are a provider windows phone 7 library, where you may want to check at runtime what capabilities are present in the WMAppManifest.xml. This post will provide a helper class to check each if each capability is present.

Note: This post leverages the great work done by Nick Randolph to gain access to the WMAppManifest.xml for access to the Application Product ID in his blogpost here

The helper class is as follows:

using Microsoft.Xna.Framework;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;

namespace www.NickHarris.NET
{
    public static class CapabilityHelper
    {
        private const string WMAppManifest = "WMAppManifest.xml";
        private const string ID_CAP_NETWORKING = "ID_CAP_NETWORKING";
        private const string ID_CAP_IDENTITY_DEVICE = "ID_CAP_IDENTITY_DEVICE";
        private const string ID_CAP_IDENTITY_USER = "ID_CAP_IDENTITY_USER";
        private const string ID_CAP_LOCATION = "ID_CAP_LOCATION";
        private const string ID_CAP_SENSORS = "ID_CAP_SENSORS";
        private const string ID_CAP_MICROPHONE = "ID_CAP_MICROPHONE";
        private const string ID_CAP_MEDIALIB = "ID_CAP_MEDIALIB";
        private const string ID_CAP_GAMERSERVICES = "ID_CAP_GAMERSERVICES";
        private const string ID_CAP_PHONEDIALER = "ID_CAP_PHONEDIALER";
        private const string ID_CAP_PUSH_NOTIFICATION = "ID_CAP_PUSH_NOTIFICATION";
        private const string ID_CAP_WEBBROWSERCOMPONENT = "ID_CAP_WEBBROWSERCOMPONENT";
        private const string CAPABILITIES = "Capabilities";
        private const string NAME = "Name";

        static CapabilityHelper()
        {
            using (var strm = TitleContainer.OpenStream(WMAppManifest))
            {
                var xml = XElement.Load(strm);
                var capabilities = xml.Descendants(CAPABILITIES).Elements();

                IsNetworkingCapability = CheckCapability(capabilities, ID_CAP_NETWORKING);
                IsDeviceIdentityCapability = CheckCapability(capabilities, ID_CAP_IDENTITY_DEVICE);
                IsUserIdentityCapability = CheckCapability(capabilities, ID_CAP_IDENTITY_USER);
                IsLocationCapability = CheckCapability(capabilities, ID_CAP_LOCATION);
                IsSensorsCapability = CheckCapability(capabilities, ID_CAP_SENSORS);
                IsMicrophoneCapability = CheckCapability(capabilities, ID_CAP_MICROPHONE);
                IsMediaLibCapability = CheckCapability(capabilities, ID_CAP_MEDIALIB);
                IsGamerServicesCapability = CheckCapability(capabilities, ID_CAP_GAMERSERVICES);
                IsPhoneDialerCapability = CheckCapability(capabilities, ID_CAP_PHONEDIALER);
                IsPushNotificationCapability = CheckCapability(capabilities, ID_CAP_PUSH_NOTIFICATION);
                IsWebBrowserComponentCapability = CheckCapability(capabilities, ID_CAP_WEBBROWSERCOMPONENT);
            }
        }

        public static bool IsNetworkingCapability { get; set; }
        public static bool IsDeviceIdentityCapability { get; set; }
        public static bool IsUserIdentityCapability { get; set; }
        public static bool IsLocationCapability { get; set; }
        public static bool IsSensorsCapability { get; set; }
        public static bool IsMicrophoneCapability { get; set; }
        public static bool IsMediaLibCapability { get; set; }
        public static bool IsGamerServicesCapability { get; set; }
        public static bool IsPhoneDialerCapability { get; set; }
        public static bool IsPushNotificationCapability { get; set; }
        public static bool IsWebBrowserComponentCapability { get; set; }

        private static bool CheckCapability(IEnumerable<XElement> capabilities, string capabilityName)
        {
            var capability = capabilities.FirstOrDefault(n => n.Attribute(NAME).Value.Equals(capabilityName));
            return capability != null;
        }      
    }
}

Note: You will need to add a reference to the Microsoft.XNA.Framework.dll for the helper to build. If you are concerned about the warning that shows up when adding a reference to this assembly from within a Silverlight application, rest assured, that it is possible to utilize most, but not all, XNA assemblies in a WP7 Silverlight Application as documented here on msdn. Scroll down to the section Using Classes Across Frameworks to see exclusions.

Usage is then as simple as

   if (!CapabilityHelper.IsUserIdentityCapability)
     //capability not present, do something

Enjoy,
Nick

Finding the Device Model on Windows Phone 7

Previously i detailed how to retrieve the Device Manufacturer and users anonymous id from the device here.  This post details how to capturing the Device Model on Windows Phone 7.

Step 1: Add the ID_CAP_IDENTITY_DEVICE capability to your WMAppManifest.xml

<Capabilities>
   <Capability Name="ID_CAP_IDENTITY_DEVICE"/>
   ...
</Capabilities>

Step 2: Use the DeviceName with DeviceExtendedProperties TryGetValue method as follows:

public static string GetDeviceModel()
{
   string model = null;
   object theModel = null;

   if (Microsoft.Phone.Info.DeviceExtendedProperties.TryGetValue("DeviceName", out theModel))
      model = theModel as string;      

   return model
}

The resulting Model of my device is T8697.

If you are interested in how to access your Applications ProductID, yep the one that can change during marketplace certification please see Nick Randolphs Blog here.

Enjoy,
Nick

Free Event Windows Phone 7 Application Acceleration

Hi All,

Microsoft have invited Nick Randolph our local MVP to run three free Windows Phone 7 application acceleration events.   These are running towards the end of Jan in Sydney, Melbourne and Perth.  I would recommend that you jump over to Nicks Blog to see the agenda and follow through with the registration link as places will be limited and they will be snapped up fast.  Windows Phone 7 Application Acceleration

Enjoy,

Nick Harris