Windows 8 UserControl框架对象导航 [英] Windows 8 UserControl Frame Object Navigation
问题描述
在XAML用户控件中,Frame对象为null:
Within a XAML user control, the Frame object is null:
this.Frame.Navigate(typeof(FaxPropertiesPage));
this.Frame.Navigate(typeof(FaxPropertiesPage));
如何使用Windows 8 XAML用户控件在页面之间导航?我已将控件放置在XAML页面上的Callisto弹出窗口中.
How do I navigate between pages with a Windows 8 XAML User Control? I have placed the control within a Callisto Flyout on a XAML page.
下面的搜索按钮必须将用户导航到另一个XAML页面.
The search button below must navigate the user to another XAML page.
推荐答案
有一种不错的方法,但又不是那么好的方法:
There's the nice way and the not-so-nice way:
两者都以导航服务开头:
Both of them start with a navigation service:
public interface INavigationService
{
bool CanGoBack { get; }
void GoBack();
void GoForward();
bool Navigate<T>(object parameter = null);
bool Navigate(Type source, object parameter = null);
void ClearHistory();
event EventHandler<NavigatingCancelEventArgs> Navigating;
}
public class NavigationService : INavigationService
{
private readonly Frame _frame;
public NavigationService(Frame frame)
{
_frame = frame;
frame.Navigating += FrameNavigating;
}
#region INavigationService Members
public void GoBack()
{
_frame.GoBack();
}
public void GoForward()
{
_frame.GoForward();
}
public bool Navigate<T>(object parameter = null)
{
Type type = typeof (T);
return Navigate(type, parameter);
}
那么,我在哪里可以得到相框?在App.xaml.cs
So, where do I get the Frame? In App.xaml.cs
protected async override void OnLaunched(LaunchActivatedEventArgs args)
{
// Do not repeat app initialization when already running, just ensure that
// the window is active
if (args.PreviousExecutionState == ApplicationExecutionState.Running)
{
Window.Current.Activate();
return;
}
// Create a Frame to act as the navigation context and navigate to the first page
var rootFrame = new Frame();
if (DesignMode.DesignModeEnabled)
SimpleIoc.Default.Register<INavigationService, DesignTimeNavigationService>();
else
SimpleIoc.Default.Register<INavigationService>(() => new NavigationService(rootFrame));
我在这里使用MVVM Light.这使生活变得轻松,因为我所有的视图模型都是使用依赖项注入创建的,并将其服务注入了其中.
I'm using MVVM Light here. This makes life easy because all my viewmodels get created using dependency injection and have their services injected into them.
如果您不使用MVVM Light之类的东西,而是依靠后台代码,那么您仍然可以使这项工作有效:只需将导航服务设为静态
If you're not using something like MVVM Light and rely on code-behind then you can still make this work: Just make the navigation service static
public class NavigationService : INavigationService
{
public static INavigationService Current{
get;set;}
blah blah blah
}
并将App.xaml.cs更改为:
And change App.xaml.cs to:
protected async override void OnLaunched(LaunchActivatedEventArgs args)
{
// Do not repeat app initialization when already running, just ensure that
// the window is active
if (args.PreviousExecutionState == ApplicationExecutionState.Running)
{
Window.Current.Activate();
return;
}
// Create a Frame to act as the navigation context and navigate to the first page
var rootFrame = new Frame();
NavigationService.Current= new NavigationService(rootFrame));
}
然后您可以在应用中的任何位置通过说出以下内容来访问主框架:
And you can then access your main Frame anywhere in the app by saying:
NavigationService.Current.Navigate<MyView>();
这篇关于Windows 8 UserControl框架对象导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!