Windows 8 UserControl Frame Object Navigation

谁都会走 提交于 2020-01-14 03:33:09

问题


Within a XAML user control, the Frame object is null:

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

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.

The search button below must navigate the user to another XAML page.


回答1:


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);
    }

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));

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.

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
}

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>();



回答2:


I've successfully used the code from app.xaml.cs

Frame frame = Window.Current.Content as Frame;

and then used the standard Navigate code.




回答3:


simple code ( may not be 100% efficient) is :

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



来源:https://stackoverflow.com/questions/12860919/windows-8-usercontrol-frame-object-navigation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!