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));
}
}
}
An error is occur ....... does not contain a definition for 'NavigationService' and no extension method 'NavigationService' accepting a first argument of type ' could be found (are you missing a using directive o开发者_高级运维r an assembly reference?)
NavigationService
is a property on the PhoneApplicationPage
class. You are not deriving from that class, you are deriving from UserControl
.
You need to get the parent phone page the user control is on and get the NavigationService reference from there.
Your compiler error is because it cannot locate a definition for NavigationService
on the Listbox
class you have made.
What Adam said is correct. But a easy solution is to define following static utility methods in 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();
}
Then you can just do:
App.Navigate(yourNavigateUri)
or App.GoBack()
From anywhere you like!
Dispatcher.BeginInvoke(() =>
NavigationService.Navigate(new Uri("/home.xaml", UriKind.Relative)));
精彩评论