Search code examples
c#silverlightwindows-phone-7navigateuri

NavigationService complaining about "FileNotFound" executing from class not attached to any view


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.


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.