Windows 8 UserControl框架对象导航

时间:2012-10-12 14:11:40

标签: microsoft-metro

在XAML用户控件中,Frame对象为null:

this.Frame.Navigate(typeof运算(FaxPropertiesPage));

如何在使用Windows 8 XAML用户控件的页面之间导航?我已将控件放在XAML页面上的Callisto Flyout中。

下面的搜索按钮必须将用户导航到另一个XAML页面。

enter image description here

3 个答案:

答案 0 :(得分:5)

我已成功使用app.xaml.cs中的代码

Frame frame = Window.Current.Content as Frame;

然后使用标准的导航代码。

答案 1 :(得分:1)

有很好的方式和不那么好的方式:

他们都以导航服务开始:

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中

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。这样可以简化生活,因为我的所有视图模型都是使用依赖注入创建的,并将服务注入其中。

如果您没有使用像MVVM Light这样的东西并依赖代码隐藏,那么您仍然可以完成这项工作:只需使导航服务静态

  public class NavigationService : INavigationService
    {
        public static INavigationService Current{
get;set;}

blah blah blah
}

将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();
        NavigationService.Current= new NavigationService(rootFrame));
}

然后,您可以通过说:

访问应用中任意位置的主框架
NavigationService.Current.Navigate<MyView>();

答案 2 :(得分:1)

简单代码(可能不是100%有效)是:

Frame frame = new Frame(); frame.Navigate(typeof运算(ExerciseAddPage)