using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
namespace WindowsPhoneApplication7
{
public partial class Listbox : UserControl
{
public Listbox()
{
InitializeComponent();
}
private void listbox(object sender, MouseEventArgs e)
{
this.NavigationService.Navigate(new Uri("/home.xaml", UriKind.Relative));
}
}
}
发生错误....... 不包含'NavigationService'的定义,并且没有扩展方法'NavigationService'接受类型'的第一个参数可以找到(你是否缺少using指令或汇编引用?)
答案 0 :(得分:1)
NavigationService
是PhoneApplicationPage
类的属性。您并非来自该课程,而是来自UserControl
。
您需要获取用户控件所在的父电话页面并从那里获取NavigationService参考。
您的编译器错误是因为它无法在您所做的NavigationService
课程中找到Listbox
的定义。
答案 1 :(得分:0)
亚当说的是正确的。但一个简单的解决方案是在App.xaml.cs中定义以下静态实用程序方法
public static PhoneApplicationFrame CurrentRootVisual
{
get
{
return (App.Current.RootVisual as PhoneApplicationFrame);
}
}
public static bool Navigate(Uri source)
{
if (CurrentRootVisual != null)
return CurrentRootVisual.Navigate(source);
return false;
}
public static void GoBack()
{
if (CurrentRootVisual != null)
CurrentRootVisual.GoBack();
}
然后你可以这样做:
App.Navigate(yourNavigateUri)
或 App.GoBack()
从你喜欢的任何地方!
答案 2 :(得分:0)
Dispatcher.BeginInvoke(() =>
NavigationService.Navigate(new Uri("/home.xaml", UriKind.Relative)));