首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Windows 8 UserControl框架对象导航

Windows 8 UserControl框架对象导航
EN

Stack Overflow用户
提问于 2012-10-12 22:11:40
回答 3查看 3.5K关注 0票数 1

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

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

如何使用Windows 8 XAML用户控件在页面之间导航?我将该控件放在XAML页面上的Callisto弹出按钮中。

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-10-14 22:01:19

有好的方法和不好的方法:

代码语言:javascript
复制
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中

代码语言:javascript
复制
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这样的东西,而是依赖于代码隐藏,那么您仍然可以这样做:只需将导航服务设为静态

代码语言:javascript
复制
  public class NavigationService : INavigationService
    {
        public static INavigationService Current{
get;set;}

blah blah blah
}

并将App.xaml.cs更改为:

代码语言:javascript
复制
    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));
}

然后你可以在应用程序中的任何地方访问你的主框架,只需说:

代码语言:javascript
复制
NavigationService.Current.Navigate<MyView>();
票数 1
EN

Stack Overflow用户

发布于 2012-12-31 18:15:23

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

代码语言:javascript
复制
Frame frame = Window.Current.Content as Frame;

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

票数 5
EN

Stack Overflow用户

发布于 2012-12-27 10:23:19

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

Frame frame =新的Frame();frame.Navigate(frame.Navigate(ExerciseAddPage)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12860919

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档