【问题标题】:Windows 8: Disabling Back button to a frame for a deleted itemWindows 8:禁用返回按钮到已删除项目的框架
【发布时间】:2013-07-18 16:39:01
【问题描述】:

我不确定我是在问正确的问题还是以错误的方式解决这个问题,但我正在使用 GridApp 项目模板开发一个 Windows 8 应用程序。

在 itemdetail 模板中,我可以删除您正在查看的项目。删除后,我导航回应用程序的主入口页面。

但是,后退按钮在那里,如果我单击它,它会尝试返回到已删除对象的那个框架。

如何避免这种情况?

【问题讨论】:

    标签: xaml windows-8 windows-runtime winrt-xaml


    【解决方案1】:

    在您的删除功能中,您可以调用GoBack(),以便在删除后页面自动导航到主页。

    后退按钮也应该有以下代码

    IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}"

    根据是否有要返回的页面来启用和禁用返回按钮。

    正如您所说,GoHome() 是解决此问题的最佳方法。

    Little more detail on page navigation in Windows 8/RT

    【讨论】:

    • 我确实有 IsEnabled 的代码,但它仍然显示为已启用。我想我可能做错了。我如何告诉应用程序它不能返回?我不能像你说的那样调用GoBack,因为前一帧也可能与被删除的项目绑定,所以我需要让它到根帧并停在那里。
    • 你可以随时通过Frame.Navigate(typeof (NameOfMainFrame));回到主框架。
    • 使用Frame.Navigate(typeof(NameOfMainFrame)); 只会让问题变得更糟。如果你想回到你的主屏幕,我会使用GoHome(),它实际上实现为while (this.Frame.CanGoBack) this.Frame.GoBack();。它不会清除您的导航堆栈,但会一直倒带到开始。然后应该禁用您的后退按钮,因为在后退堆栈中没有进一步的地方。
    • GoHome() 完美运行!谢谢!您可以将此添加为答案,以便我可以将其标记为这样吗?也感谢您的意见 Maynak!
    【解决方案2】:

    最简单的方法是使用与按钮相关的内置命令机制:

    Xaml:

    视图模型:

    public ViewModel()
    {
        _goBackCommand=new DelegateCommand(GoBackMethod,CanGoBackMethod);
    }
    public ICommand GoBackCommand
    {
        get{return _goBackCommand;}
    } 
    
    private void GoBackMethod()
    {
        Frame.Navigate(blah);
    }
    
    private bool CanGoBackMethod()
    {
        return _isDeleted;
    }
    
    public void Delete()
    {
        _isDeleted=false;
        //this forces the command to re-evaluate whether it can execute
        _goBackCommand.RaiseCanExecuteChanged();
    }
    

    即使您不使用 MVVM 并且仅使用代码隐藏,您仍然可以将命令绑定到您的按钮对象并对其执行完全相同的操作。如果你需要创建一个具有 RaiseCanExecuteChanged 功能的命令,那么你可以使用这个:

    public class DelegateCommand : ICommand
    {
        private readonly Predicate<object> _canExecute;
        private readonly Action<object> _execute;
    
        public event EventHandler CanExecuteChanged;
    
        public DelegateCommand(Action<object> execute) 
                       : this(execute, null)
        {
        }
    
        public DelegateCommand(Action<object> execute, 
                       Predicate<object> canExecute)
        {
            _execute = execute;
            _canExecute = canExecute;
        }
    
        public override bool CanExecute(object parameter)
        {
            if (_canExecute == null)
            {
                return true;
            }
    
            return _canExecute(parameter);
        }
    
        public override void Execute(object parameter)
        {
            _execute(parameter);
        }
    
        public void RaiseCanExecuteChanged()
        {
            if( CanExecuteChanged != null )
            {
                CanExecuteChanged(this, EventArgs.Empty);
            }
        }
    }
    

    如果您使用的是 Prism 或 MvvmLight,那么他们自己的命令可以立即实现这一点。

    【讨论】:

      【解决方案3】:

      这是删除所有返回导航。

      如果 (this.Frame != null) { while (this.Frame.CanGoBack) this.Frame.GoBack(); }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-10
        • 1970-01-01
        • 1970-01-01
        • 2016-10-31
        • 1970-01-01
        相关资源
        最近更新 更多