Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass non-string parameters between pages in windows phone 8?

I'm working on converting a windows store app to windows phone 8. For WinRT, you could pass any object as a parameter when calling frame.navigate. (frame.navigate(type sourcePageType, object parameter))

The navigation seems to work differently for windows phone, you navigate by calling into a uri, like: frame.navigate(new uri("mypage.xaml", UriKind.Relative))

The windows documentation notes that you can pass a string as a parameter by adding it to the uri.

Is there an accepted way of passing complex objects between pages that I just haven't found?

like image 777
Mr. Spoof Avatar asked Nov 30 '12 22:11

Mr. Spoof


3 Answers

I ended up extending the NavigationService class, like so:

public static class NavigationExtensions
{
    private static object _navigationData = null;

    public static void Navigate(this NavigationService service, string page, object data)
    {
        _navigationData = data;
        service.Navigate(new Uri(page, UriKind.Relative));
    }

    public static object GetLastNavigationData(this NavigationService service)
    {
        object data = _navigationData;
        _navigationData = null;
        return data;
    }
}

Then you'd call NavigationService.Navigate("mypage.xaml", myParameter); on the source page, and in the OnNavigatedTo method of the target page call var myParameter = NavigationService.GetLastNavigationData(); to get the parameter data.

like image 157
Zik Avatar answered Nov 10 '22 03:11

Zik


I want to just add a VB.net version of the great answer provided by Zik above. Once I figured out how to translate his code to VB I immediately had navigation working similarly to the WinRT/Windows 8 way.

I created a module with the following code:

Module NavigationExtensionsModule

Sub New()
End Sub
Private _navigationData As Object = Nothing

<System.Runtime.CompilerServices.Extension> _
Public Sub Navigate(service As NavigationService, page As String, data As Object)
    _navigationData = data
    service.Navigate(New Uri(page, UriKind.Relative))
End Sub

<System.Runtime.CompilerServices.Extension> _
Public Function GetLastNavigationData(service As NavigationService) As Object
    Dim data As Object = _navigationData
    _navigationData = Nothing
    Return data
End Function
End Module

And then navigate to another page like this:

 NavigationService.Navigate("pagename.xaml", ObjectToPassToThePage)

And lastly, to get that object in my other page, in the OnNavigatedTo sub:

ThisPageData = NavigationService.GetLastNavigationData()

Me.DataContext = ThisPageData

Credit to Zik for the actual answer.

like image 29
pumpkinszwan Avatar answered Nov 10 '22 04:11

pumpkinszwan


If you are using MVVM architecture,then you can pass string or any value after registering using Messenger. Create a model class (say PageMessage) with a string(say message) variable. You want to pass string from homepage.xaml to newpage.xaml,then in homepage.xaml just send the message like this

Messenger.Default.Send(new PageMessage{message="Hello World"});

In the newpage.xaml,you should register the messenger like this,

Messenger.Default.Register<PageMessage>(this, (action) => ReceiveMessage(action));

 private object ReceiveMessage(PageMessage action)
 {
    string receivedMessage=action.message;
    return null;
 }
like image 2
Ramesh Avatar answered Nov 10 '22 04:11

Ramesh