2017-03-01 69 views
2

我想爲我的uwp應用程序使用後退按鈕。我爲此使用了下面的代碼。但它不可見。請幫幫我。在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存在,版本= 2.0 .0.0,Culture = neutral,PublicKeyToken = null'和'Microsoft.Xaml.Interactivity,Version = 12.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e3''

任何人都可以請告訴我如何解決它。

+0

我已經加入同一裝配Microsoft.Xaml.Interactivity 2個的NuGet包,現在我有刪除一個然後也後面的按鈕不visi BLE。但沒有錯誤。 – Archana

回答

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