4

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?

Community
  • 1
  • 1
Rico-E
  • 1,806
  • 2
  • 28
  • 47
  • Are both projects windows phone app projects? – Alaa Masoud Jul 04 '13 at 13:28
  • At this Point: no. Project A is a Windows phone app, and the other one (`ProjectB`) is a class library. But I can change this, if it would be better. – Rico-E Jul 04 '13 at 13:32
  • Hmm @Rico-E you need to use Navigate, as Alaa's answer specified. `.Show()` won't work. – Paul Annetts Jul 04 '13 at 14:07
  • 1
    possible duplicate of [How do I pass non-string parameters between pages in windows phone 8?](http://stackoverflow.com/questions/13654379/how-do-i-pass-non-string-parameters-between-pages-in-windows-phone-8) – Paul Annetts Jul 04 '13 at 14:12
  • @PaulAnnetts I know that `.Show()` won't work, as this method does not exist, it should only clarify what I want to do. The problem with `Navigate` is, that I can not give an object as Parameter, can I? I am wondering if I can get the xaml behind the `bPage`-object, and show that. Is it possible? – Rico-E Jul 04 '13 at 14:16
  • @PaulAnnetts Thank you for pointing me to the other thread. It helped. – Rico-E Jul 04 '13 at 14:26

1 Answers1

3

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.

Alaa Masoud
  • 7,085
  • 3
  • 39
  • 57
  • 1
    Sorry, but this is actually NO answer. I want to pass an object between both pages. Navigation itself is no problem. – Rico-E Jul 04 '13 at 13:59
  • 1
    Thank you very much for your time! This seems to me as if this is a very good solution. In an abstract way it is very similar to that way in http://stackoverflow.com/questions/13654379/how-do-i-pass-non-string-parameters-between-pages-in-windows-phone-8 . I will give it a try. Thank you very much! – Rico-E Jul 05 '13 at 07:34