Rico-E
Rico-E

Reputation: 1845

windows phone, instantiate a page and show it

Is it possible to instantiate a PhoneApplicationPage from another project (in the same solution, but this shouldn't be important, should it?) and show it?

Well, I know that it is possible to instantiate another page with:

MyPageInProjectB bPage= new MyPageInProjectB ();

but how do I show it?

I want to do the following: I have two projects, and I want to show a page of one project within the other project and passing data between them. So I thought this would be perfect:

public partial class MyPageInProjectA : PhoneApplicationPage
{
    private MyPageInProjectB bPage;

    public MyPageInProjectA()
    {
        InitializeComponent();

        MyComplexObject objectIWantToPassToOtherPage = new MyComplexObject ();

        bPage= new MyPageInProjectB (objectIWantToPassToOtherPage);
    }

    private void ShowBPageButton_Click(object sender, EventArgs e)
    {
        // this is now what I want to do, but what doesn't work
        bPage.Show();
    }
}

Or is there another way to pass complex data between those pages?

I don't want to use query strings, because it happens sometimes, that the (De-)Serializer has problems with MyComplexObject.

I read how to navigate between both pages in this thread: How to redirect to a page which exists in specif project to another page in other project in the same solution? but I want to pass a complex object from one page to another. How can I do that?

Upvotes: 4

Views: 1032

Answers (1)

Alaa Masoud
Alaa Masoud

Reputation: 7135

Edit: Ok, now after reading your question carefully I can give you a better answer.

First, you cannot pass data to a page constructor since the runtime itself handles instance creation when navigating and you can only navigate using NavigationService. However, you have other options.

One of them is using query string but if you don't want to use that you could use PhoneApplicationService to store your complex object and have the other page read from it.

// In source page
MyComplexObject objectIWantToPassToOtherPage = new MyComplexObject ();
PhoneApplicationService.Current.State["MyComplexObject"] = objectIWantToPassToOtherPage;
NavigationService.Navigate(new Uri("/ProjectB;component/MyPageInProjectB.xaml", UriKind.Relative));

// In destination page's constructor
public MyPageInProjectB() {
  var myComplexObject = (MyComplexObject) PhoneApplicationService.Current.State["MyComplexObject"];
  // ...
}

Other than that is using global variables. These are the only options I can think of.


To navigate to the page in your class library ProjectB you need to use a uri similar to the following:

/{assemblyName};component/{path}

In your case, you can do the following:

NavigationService.Navigate(new Uri("/ProjectB;component/MyPageInProjectB.xaml", UriKind.Relative));

Note that I am specifying relative path here so MyPageInProjectB.xaml needs to be in the root folder inside ProjectB for the above example to work.

Upvotes: 3

Related Questions