2017-03-01 70 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