uwp标题栏中的后退按钮不可见

时间:2017-03-01 10:09:54

标签: c# xaml uwp back-button

我正在尝试使用我的uwp应用程序的后退按钮。我使用下面的代码。但它不可见。请帮帮我。

  public class TitleBarBehavior : DependencyObject, IBehavior
{
    public DependencyObject AssociatedObject { get; private set; }

    public void Attach(DependencyObject associatedObject)
    {
        var newTitleBar = associatedObject as UIElement;
        if (newTitleBar == null)
            throw new ArgumentException(
                "TitleBarBehavior can be attached only to UIElement");

        Window.Current.SetTitleBar(newTitleBar);
    }

    public void Detach() { }

    public bool IsChromeless
    {
        get { return (bool)GetValue(IsChromelessProperty); }
        set { SetValue(IsChromelessProperty, value); }
    }

    public static readonly DependencyProperty IsChromelessProperty =
        DependencyProperty.Register("IsChromeless",
        typeof(bool),
        typeof(TitleBarBehavior),
        new PropertyMetadata(false, OnIsChromelessChanged));

    private static void OnIsChromelessChanged(DependencyObject d,
        DependencyPropertyChangedEventArgs e)
    {
        CoreApplication.GetCurrentView().TitleBar
            .ExtendViewIntoTitleBar = (bool)e.NewValue;
    }
}

和app.xaml.cs中的代码

SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;

            //SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
            SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = rootFrame.CanGoBack ?
                      AppViewBackButtonVisibility.Visible :
                      AppViewBackButtonVisibility.Collapsed;


 private void OnBackRequested(object sender, BackRequestedEventArgs e)
        {
            Frame FromrootFrame = Window.Current.Content as Frame;
            strpage = FromrootFrame.Content.ToString();
            //if (rootFrame != null)
            //{
            //    Type whatpageisit = rootFrame.SourcePageType;
            //    // handle this page type
            //}
            if (FromrootFrame.CanGoBack)
            {
                e.Handled = true;
                FromrootFrame.GoBack();
            }
        }

但我得到的错误是 "类型' IBehavior'存在于Microsoft.Xaml.Interactivity,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = null'和' Microsoft.Xaml.Interactivity,Version = 12.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e3'"

任何人都可以告诉我如何解决它。

1 个答案:

答案 0 :(得分:1)

当您导航到新页面或返回上一页时,根框架将引发事件OnNavigated,您需要更新此事件中后退按钮的可见性,因此请尝试使用以下代码:

protected override void OnLaunched(LaunchActivatedEventArgs e)
{
    ……
    frame.OnNavigated += Frame_Navigated;
    ……
}

private void Frame_Navigated(object sender, NavigationEventArgs e)
{
    var frame = Window.Current.Content as Frame;
    SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = frame.CanGoBack ?
                  AppViewBackButtonVisibility.Visible :
                  AppViewBackButtonVisibility.Collapsed;
}

关于您的引用错误,我建议您删除所有已安装的XAML行为引用,然后安装此nuget包:

Install-Package Microsoft.Xaml.Behaviors.Uwp.Managed