locked
Open new page on proximity event

    Question

  • private void messageReceived(Windows.Networking.Proximity.ProximityDevice device, Windows.Networking.Proximity.ProximityMessage message)
    {
                //this is where I would normally set my text block
                //to display the incoming NFC string using 'message'
                //above
    
                this.Frame.Navigate(typeof(MyNewPage), message);
                
    }

    Hi,

    I've wrote a C# Metro application that is able to receive plain text data over NFC.

    Currently I'm able to print the incoming strings to a text block located on my main page.

    What I would like to be able to do is open a new page upon receiving the data and then use the data on this new page.

    I've tried placing the code to open the new page within the messageReceived handler but it's generating the following exception:

    "An exception of type 'System.Exception' occurred in nfcInterface.exe but was not handled in user code

    Additional information: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))"

    If someone could tell me what I'm doing wrong and suggest an alternative method to display the incoming data on a page separate to my main page it would be very much appreciated.

    Thanks,

    Gary

    Thursday, August 2, 2012 10:01 AM

Answers

  • Gary,

    Here you go:

            // Display Error Screen
            async private void DisplayErrorScreen()
            {
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    // Dispaly Error Message.
                 });
            }

    Best Wishes - Eric
    Friday, August 3, 2012 12:59 PM
    Moderator

All replies

  • Gary,

    You need to use async and await:

    http://msdn.microsoft.com/en-us/library/windows/apps/hh770843.aspx

    Best Wishes - Eric

    Thursday, August 2, 2012 5:19 PM
    Moderator
  • Hi Eric,

    Thank you for your reply. I thought I might need to use async and await, at least now I know I'm on the right track.

    I'm already using async and await elsewhere in my app to read text files. I can't quiet figure out how to use them in this case. How exactly can I asynchronously navigate to a new page and what exactly do I need to "await"?

    A code example would be really helpful.

    Many thanks,

    Gary

    Friday, August 3, 2012 10:34 AM
  • Gary,

    Here you go:

            // Display Error Screen
            async private void DisplayErrorScreen()
            {
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    // Dispaly Error Message.
                 });
            }

    Best Wishes - Eric
    Friday, August 3, 2012 12:59 PM
    Moderator
  • That's perfect, thanks Eric!

    private async void messageReceived(Windows.Networking.Proximity.ProximityDevice device, Windows.Networking.Proximity.ProximityMessage message)
    {
                
    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
         this.Frame.Navigate(typeof(MyNewPage), message);
    });
    
    }

    Friday, August 3, 2012 2:34 PM
  • You are very welcome Gary!
    Friday, August 3, 2012 2:47 PM
    Moderator