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:)

  • himanshu rathore

    sir i wanted to reverse a string in windows phone7, how can i do this, ie..
    ..
    input box – “you are intelligent” (user type this in the input box)
    ..
    press enter. (user presses enter)
    ..
    ouput box display – “tnegilletni era uoy” (will be displayed to user)


    sir please help..

  • nick.harris

    Hi Himanshu,

    Assuming that you are not using bindings to a view model and you have a named textbox say txtInputString then in the click handler for your outputbox you can use linq which will do the work for you.

    add a using System.Linq; to the top of your *.cs

    within the click handler do a

    txtInputString.Text = new string(txtInputString.Text.Reverse().ToArray());

    if your after better performance you probably want to traverse the array and create your new string manually the way we used to do it in the good ole days ;)

    Kind Regards,
    Nick

  • himanshu rathore

    sir, i am very very thankful to.you, a hearty thankful.
    ..
    your suggestin did worked sir, and i completd my application..
    i am a new msp at microsoft, india. and i had to submit my applctn over there. thanks to yu for helping when i was under probelm during building a applictn. i did now complted my appl and submited too.
    .
    ..
    personally i would say one.
    people starting name nick are always lucky for me,
    infact to whom i love her name is also Nicky.. is alwayz lucky for me.
    .
    this tym its yu.same strtin name wid nick..lucky for me ..
    since today, was the last day for submission of applictn.

    ..
    facebook profile
    http://www.facebook.com/Himansh.Pal
    ..
    thankyi thankyu thankyu….!!!!!!!

    Regards
    Himanshu Rathore
    Msp – India

  • nick.harris

    Hi Himanshu,

    no problems thats what my blog is all about :)
    Post a deep link to your app as a reply here once its available on Marketplace.

    Kind regards,
    Nick Harris

  • Pranesh Hanumanth

    Hi nick

    Consider this scenario, I am calling the below lines of code on click of a button in the app which i am creating.

    WebBrowserTask webBrowserTask = new WebBrowserTask();

    webBrowserTask.Uri = new Uri(“http://www.google.com”);

    webBrowserTask.Show();

    Now what happens is like on click of button a browser opens and the app goes to the deactivated state. Now if i click back, it comes to the same page from which i went to the browser, but where as i want to go to some other page. If i try to navigate to some other page, it give me an exception “Navigation in not allowed when the task is not in the foreground”. Is there any other way to navigate to some other page on close of the browser.

    Thanks and regards
    Pranesh