2015-06-02 10 views
20

मैं विंडोज मोबाइल 10 के लिए बैक बटन और विंडोज 10 टैबलेट मोड के लिए बैक बटन कैसे संभालें? मैं हर जगह देख रहा हूं लेकिन इसके लिए कोई उदाहरण नहीं मिल रहा है।विंडोज 10 यूएपी बैक बटन

उत्तर

28

यह विषय Guide to Universal Windows Platform apps में उपयोग किए गए उदाहरणों में से एक है। मैं दृढ़ता से सुझाव देता हूं कि यूनिवर्सल ऐप्स के साथ शुरुआत करते समय।

पृष्ठ शीर्षलेख के बटन के लिए Windows.UI.Core.SystemNavigationManager का उपयोग करें और बटन को दिखाने या छिपाने के लिए AppViewBackButtonVisibility प्रॉपर्टी सेट करें और नेविगेशन करने के लिए BackRequested ईवेंट को संभालें।

Windows.UI.Core.SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible; 
Windows.UI.Core.SystemNavigationManager.GetForCurrentView().BackRequested += (s,a) => 
{ 
    Debug.WriteLine("BackRequested"); 
    if (Frame.CanGoBack) 
    { 
     Frame.GoBack(); 
     a.Handled = true; 
    } 
} 

आप हार्डवेयर वापस बटन एक ही रूप में आप विंडोज फोन 8.1 में क्या अप तार, लेकिन आप PhoneContract लिए जाँच करनी चाहिए (या व्यक्तिगत वर्ग और विधि) यह नहीं है सुनिश्चित करने के लिए:

if (ApiInformation.IsApiContractPresent ("Windows.Phone.PhoneContract", 1, 0)) { 
    Windows.Phone.UI.Input.HardwareButtons.BackPressed += (s, a) => 
    { 
     Debug.WriteLine("BackPressed"); 
     if (Frame.CanGoBack) 
     { 
      Frame.GoBack(); 
      a.Handled = true; 
     } 
    }; 
} 
+0

AppViewBackButton कहां रखा जाना चाहिए? मेनपेज contstructor मेरे लिए कुछ भी नहीं करता है और न ही App.xaml.cs पर ऑनलांच किया गया – robertk

+0

यह शीर्षक बार पर स्वचालित रूप से चला जाता है :) – shady

+1

क्या बैक बटन के पृष्ठभूमि रंग को अनुकूलित करने का कोई तरीका है? –

6

अपने App.xaml.cs लिए निम्न कोड जोड़ें और यह डेस्कटॉप, टेबलेट और मोबाइल पर नेविगेशन संभाल लेंगे (मैं मोबाइल एमुलेटर पर यह परीक्षण किया) बेहतर करने के लिए प्रकाश डाला मतभेद और (JEFF PROSISE द्वारा Handling The Back Button In Windows 10 UWP Apps) स्पष्टीकरण

sealed partial class App : Application 
{ 

    public App() 
    { 
     this.InitializeComponent(); 
     this.Suspending += OnSuspending; 
    } 

    protected override void OnLaunched(LaunchActivatedEventArgs e) 
    { 
     Frame rootFrame = Window.Current.Content as Frame; 

     // Do not repeat app initialization when the Window already has content, 
     // just ensure that the window is active 
     if (rootFrame == null) 
     { 
      // Create a Frame to act as the navigation context and navigate to the first page 
      rootFrame = new Frame(); 

      rootFrame.NavigationFailed += OnNavigationFailed; 
      rootFrame.Navigated += OnNavigated; 

      if (e.PreviousExecutionState == ApplicationExecutionState.Terminated) 
      { 
       // TODO: Load state from previously suspended application 
      } 

      // Place the frame in the current Window 
      Window.Current.Content = rootFrame; 

      // Register a handler for BackRequested events and set the 
      // visibility of the Back button 
      SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested; 

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

     if (rootFrame.Content == null) 
     { 
      // When the navigation stack isn't restored navigate to the first page, 
      // configuring the new page by passing required information as a navigation 
      // parameter 
      rootFrame.Navigate(typeof(MainPage), e.Arguments); 
     } 

     // Ensure the current window is active 
     Window.Current.Activate(); 
    } 

    void OnNavigationFailed(object sender, NavigationFailedEventArgs e) 
    { 
     throw new Exception("Failed to load Page " + e.SourcePageType.FullName); 
    } 

    private void OnNavigated(object sender, NavigationEventArgs e) 
    { 
     // Each time a navigation event occurs, update the Back button's visibility 
     SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = 
      ((Frame)sender).CanGoBack ? 
      AppViewBackButtonVisibility.Visible : 
      AppViewBackButtonVisibility.Collapsed; 
    } 

    private void OnSuspending(object sender, SuspendingEventArgs e) 
    { 
     var deferral = e.SuspendingOperation.GetDeferral(); 
     // TODO: Save application state and stop any background activity 
     deferral.Complete(); 
    } 

    private void OnBackRequested(object sender, BackRequestedEventArgs e) 
    { 
     Frame rootFrame = Window.Current.Content as Frame; 

     if (rootFrame.CanGoBack) 
     { 
      e.Handled = true; 
      rootFrame.GoBack(); 
     } 
    } 
} 
+0

मैं बटन, पीछे, घर और खोज के साथ बटन छिपाने के लिए अपने लुमिया 950 पर यह कोशिश कर रहा हूं। यह कुछ भी नहीं करता है और वे अभी भी वहां हैं, कोई विचार क्यों? – Nick

+0

उत्तर बाईं ओर दिखाई देने वाले विशेष बैक बटन के संबंध में है और हार्डवेयर बटन को संभालने वाला है जिसमें ऐप से बाहर निकलने का डिफ़ॉल्ट व्यवहार है। घर और खोज बटन के साथ इसका कोई संबंध नहीं है। –

+0

आह ठीक है धन्यवाद, मेरा बुरा। मुझे वह जानकारी मिली जो मैं अंत में चाहता था। – Nick