基本上我想在我的WPF应用程序中的两个页面之间导航。
首先我试过这个。
NavigationService nav = NavigationService.GetNavigationService(this);
Page1 page = new Page2();
nav.Navigate(page);
然后这个
NavigationService.Navigate(new Page2());
请注意我的原始页面名称是sysinfo.xaml,我的源页面名称是entry.xaml.i hust put page1和page 2为了简单起见 然后参考MSDN示例
this.NavigationService.Navigate(new ContentPage());
我想在点击下一个按钮时从第1页导航到第2页 我的下一个按钮代码
<Button Content="Next" Height="30" Name="nextbutton" Width="95" Margin="0,269,408,12" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Click="nextbutton_Click" />
我的源页面c#code
void nextbutton_Click(object sender, RoutedEventArgs e)
{
NavigationService nav = NavigationService.GetNavigationService(this);
nav.Navigate(new System.Uri("Page2.xaml", UriKind.RelativeOrAbsolute));
}
我的错误是NULLREFERENCEEXCEPTION -Unhandled对象引用未设置为对象的实例。
我的完整堆栈跟踪
at Crystal_pre_Install.Entry.nextbutton_Click(Object sender, RoutedEventArgs e) in C:\Users\monika\documents\visual studio 2010\Projects\Crystal pre-Install\Crystal pre-Install\Entry.xaml.cs:line 30
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
at System.Windows.UIElement.CrackMouseButtonEventAndReRaiseEvent(DependencyObject sender, MouseButtonEventArgs e)
at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)
at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.Run()
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run(Window window)
at System.Windows.Application.Run()
at Crystal_pre_Install.App.Main() in C:\Users\monika\documents\visual studio 2010\Projects\Crystal pre-Install\Crystal pre-Install\obj\x86\Debug\App.g.cs:line 0
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
答案 0 :(得分:3)
我偶然碰到了这个帖子。 这似乎是一个着名的问题,所以这里有一个替代方案:
如果您从一个WINDOW导航到另一个WINDOW,您应该可以这样做:
Page2 page2 = new Page2();
this.Hide();
page2.showDialog();
您可以将对当前页面的引用传递给Page2的构造函数,这样您就可以像这样显示隐藏的表单:
//In MainWindow
Page2 page2 = new Page2(this);
this.Hide();
page2.showDialog();
//In Page2
private Window previousWindow;
public Page2(Window pageReference) {
InitializeComponent();
previousWindow = pageReference;
this.Closing += Page_Closing;
}
void Page_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (previousWindow != null)
previousWindow.Show();
}
如果你是&#34;导航&#34; (这不是正确的术语),你应该这样做:
this.Content = new Page2();
这个问题可能已经两年了,但我认为仍有人在寻找这个问题。 我希望这会有所帮助。