2

I'm trying to navigate to another page (using UriMapping) from some class, that handles Server API, but it is not works. Here my code:

public void processResponce(item Response)
{
    try
    {
        var token = Response.result.token;
        this.setToken("&token=" + token);
        Debug.WriteLine(this.apiUrl);

        (Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/listItems", UriKind.Relative));
    }
    catch
    {
        Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                var messageFromServer = Response.error.message;
                MessageBox.Show(messageFromServer);

                Debug.WriteLine(messageFromServer);
            });
    }
}

SDK keeps saying, that file does not exists. But if i will call Navigate() from some class, attached to view (fe, MainPage.xaml.cs), then navigation succeed. Can anyone help?

UPD. I have multiple *.xaml pages, but this class not connected directly to another page. Right now, this class named "JSON-RPC". I tryed different ways to resolve my problem (even named him "public partial JSON-RPC : PhoneApplicationPage"), but... Problem is: if i'll call Navigate() from some class, attached to *.xaml page (in solution explorer it displays like a tree - *.xaml page -> pagename.xaml.cs), then it works; if i'll call Navigate() from my class JSON-RPC, then it always says "File not found".

UPD2. Solution: Sometimes i feel myself as a fool. Answer is:

  Deployment.Current.Dispatcher.BeginInvoke(() =>
             {
                  (Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/listItems", UriKind.Relative));
             });

We need to invoke Dispatcher to solve this issue.

Fr3nzy
  • 21
  • 3
  • 1
    I'm not quit sure I understood you, are u not missing a ".xaml" in the end of name? – Mo Valipour Aug 07 '11 at 12:31
  • No. I have multiple *.xaml pages, but this class not connected directly to another page. Right now, this class named "JSON-RPC". I tryed different ways to resolve my problem (even named him "public partial JSON-RPC : PhoneApplicationPage"), but... Problem is: if i'll call Navigate() from some class, attached to *.xaml page (in solution explorer it displays like a tree - *.xaml page -> pagename.xaml.cs), then it works; if i'll call Navigate() from my class JSON-RPC, then it always says "File not found". – Fr3nzy Aug 07 '11 at 14:28

2 Answers2

0

Make sure that:

  • You are including the ".xaml" extension at the end of the uri.
  • You fully specify the route to the file (for /listItems make sure that listItems.xaml is at the root directory of the app).
  • The build action of the xaml file is set to "Page".

Also: you can use RootFrame instead of RootVisual.

Here's an example that works for me:

App.Current.RootFrame.Navigate(new Uri("/Pages/Error.xaml?Message=Application error", UriKind.Relative));
alf
  • 18,372
  • 10
  • 61
  • 92
  • 1. I'm using UriMapping, there i've declared UriMaps in App.xaml:
    `
    `
    3. Build action? Not quite sure i understend that.
    Plus,RootFrame does not exists in System.Windows.Application :(
    – Fr3nzy Aug 07 '11 at 16:54
  • Also, if i'll use full Uri to a page ("/Views/ListItemsAndSomethingElse.xaml") + UriKind.Relative / Absolute... it's doesn't work eigther. – Fr3nzy Aug 07 '11 at 17:02
  • Check the properties of the xaml file. You should see a property called Build action that tells VS how to build the file. Also, I would try to comment out the UriMapper just to see if it's causing the problem. Other than those, I have no further ideas :) – alf Aug 07 '11 at 17:30
  • If you about opening tag, then " – Fr3nzy Aug 07 '11 at 18:09
  • Select the xaml file in solution explorer and then hit F4, and you should see the properties for that file. Also, is your class in the same assembly than the page your trying to navigate to? – alf Aug 07 '11 at 18:30
0

Sometimes i feel myself as a fool. Answer is:

  Deployment.Current.Dispatcher.BeginInvoke(() =>
             {
                  (Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/listItems", UriKind.Relative));
             });

We need to invoke Dispatcher to solve this issue.

Fr3nzy
  • 21
  • 3