我试图从处理服务器 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/