c# - NavigationService 提示从未附加到任何 View 的类执行 "FileNotFound"

标签 c# silverlight windows-phone-7 navigateuri

我试图从处理服务器 API 的某个类导航到另一个页面(使用 UriMapping),但它不起作用。这是我的代码:

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 一直说,那个文件不存在。但是,如果我将从某个类调用 Navigate(),附加到 View (fe,MainPage.xaml.cs),则导航成功。 谁能帮忙?

UPD. 我有多个 *.xaml 页面,但此类未直接连接到另一个页面。现在,这个类名为“JSON-RPC”。我尝试了不同的方法来解决我的问题(甚至将他命名为“公共(public)部分 JSON-RPC:PhoneApplicationPage”),但是......问题是:如果我从某个类调用 Navigate(),附加到 *.xaml 页面(在解决方案资源管理器它像树一样显示 - *.xaml 页面 -> pagename.xaml.cs),然后它就可以工作了;如果我从我的类 JSON-RPC 调用 Navigate(),那么它总是说“找不到文件”。

UPD2。解决方案: 有时我觉得自己是个傻瓜。 答案是:

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

我们需要调用 Dispatcher 来解决这个问题。

最佳答案

确保:

  • 您在 uri 末尾包含“.xaml”扩展名。
  • 您完全指定文件的路径(对于/listItems 确保 listItems.xaml 位于应用程序的根目录中)。
  • 构建 Action xaml 文件的设置为“页面”。

此外:您可以使用 RootFrame 而不是 RootVisual。

这是一个对我有用的例子:

App.Current.RootFrame.Navigate(new Uri("/Pages/Error.xaml?Message=Application error", UriKind.Relative));

关于c# - NavigationService 提示从未附加到任何 View 的类执行 "FileNotFound",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6972549/

相关文章:

c# - 无法更新 Entity Framework 中的多对多关系

c# - 无法在 WPF MVVM 中将 View 绑定(bind)到 ViewModel

c# - 将文本框值复制到另一个 - ASP.NET MVC Razor

c# - silverlight、保存负载、IsolatedStorageFile 和 IsolatedStorageFileStream。异常(exception)情况

c# - 将 ListView 绑定(bind)到另一个类中的属性

silverlight - Silverlight 应用程序中的文化

Silverlight 应用程序在 IE10 中的页面刷新时消失

iphone - 如何同时本地化 iPhone(monotouch) 和 Windows Phone 7 应用程序?

youtube - Windows Phone 7 是否有类似于 iPhone 的 YouTube 播放器?

windows-phone-7 - HtmlAgilityPack XPath 无效 token