खोज…


परिचय

यूनिवर्सल विंडोज 10 ऐप जीवनचक्र में तीन अलग-अलग अवस्थाएँ होती हैं: 1) रनिंग - एप्लिकेशन उपयोग में मौजूदा 2 है) नहीं चल रहा है - एप्लिकेशन बंद है और मेमोरी 3 से हटा दिया गया है) निलंबित - एप्लिकेशन स्थिति जमी हुई है लेकिन यह अभी भी मेमोरी में है [!] यहाँ चित्र विवरण दर्ज करें] [1]] [१] [१]: https://i.stack.imgur.com/x7MCl.png जैसा कि आप ऊपर चित्र में देख सकते हैं कि अलग-अलग घटनाएँ एक राज्य से दूसरे में जाने से जुड़ी हैं एक और। उदाहरण अनुभाग में मैं दिखाता हूं कि उन्हें कैसे संभालना है।

टिप्पणियों

MSDN ब्लॉग पर दो अच्छे लेखों को संदर्भित करना अच्छा है:

  1. https://msdn.microsoft.com/en-us/windows/uwp/launch-resume/app-lifecycle
  2. https://blogs.windows.com/buildingapps/2016/04/28/the-lifecycle-of-a-uwp-app/#RqKAKkevsAPIvBUT.97

"चल रहा है" राज्य संभाल रहा है

"रनिंग" स्थिति में जाने पर इस ईवेंट के साथ विशेष हैंडलर जुड़ा होता है: "App.xaml.cx" क्लास खोलें और "OnLaunched" विधि देखें - यह तब सक्रिय होता है जब ऐप्लिकॉन उपयोगकर्ता द्वारा "टर्मिनेडेड" स्टेट से खोला जाता है:

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;

           //You can get information about previous state of the app:

            if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
            {
                //The app was previously suspended but was then shutdown 
                //at some point because the system needed to reclaim memory.
            }
            if (e.PreviousExecutionState == ApplicationExecutionState.ClosedByUser)
            {
                //The user closed the app with the close gesture in tablet mode,
                //or with Alt+F4.When the user closes the app, it is first suspended
                //and then terminated.
            }
            if (e.PreviousExecutionState == ApplicationExecutionState.NotRunning)
            {
                //An app could be in this state because it hasn't been launched since the last time
                //the user rebooted or logged in. It can also be in this state if it was running 
                //but then crashed, or because the user closed it earlier.
            }
            if (e.PreviousExecutionState == ApplicationExecutionState.Running)
            {
                //The app was already open when the user tried to launch it again
            }
            if (e.PreviousExecutionState == ApplicationExecutionState.Suspended)
            {
                //The user either minimized or switched away from your app
                //and didn't return to it within a few seconds.
            }

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

        //When available system resources allow, the startup performance of Windows Store
        //apps on desktop device family devices is improved by proactively launching
        //the user’s most frequently used apps in the background. A prelaunched app 
        //is put into the suspended state shortly after it is launched.Then, when the
        //user invokes the app, the app is resumed by bringing it from the suspended
        //state to the running state--which is faster than launching the app cold.
        //The user's experience is that the app simply launched very quickly.
        if (e.PrelaunchActivated == false)
        {
            if (rootFrame.Content == null)
            {
                rootFrame.Navigate(typeof(MainPage), e.Arguments);
            }
            Window.Current.Activate();
        }
    }

"सस्पेंड" राज्य हैंडलिंग

"सस्पेंडेड" स्थिति में जाने पर इस घटना से जुड़ा विशेष हैंडलर है: "App.xaml.cx" क्लास खोलें और "ऐप" कंस्ट्रक्टर देखें - ईवेंट हैंडलर है:

public App()
    {
        this.InitializeComponent();
        //Handle suspending operation with event handler:
        this.Suspending += OnSuspending;
    }

अब आप निलंबन घटना को संभाल सकते हैं:

private Dictionary<string, object> _store = new Dictionary<string, object>();
private readonly string _saveFileName = "store.xml";
private async void OnSuspending(object sender, SuspendingEventArgs e)
    {
        var deferral = e.SuspendingOperation.GetDeferral();
        _store.Add("timestamp", DateTime.Now);
        await SaveStateAsync();
        //TODO: Save application state and stop any background activity
        //Here you can use  await SuspensionManager.SaveAsync();
        //To read more about saving state please refer to below MSDN Blog article:
        //https://blogs.windows.com/buildingapps/2016/04/28/the-lifecycle-of-a-uwp-app/#RqKAKkevsAPIvBUT.97
        deferral.Complete();
    }

    private async Task SaveStateAsync()
    {
        var ms = new MemoryStream();
        var serializer = new DataContractSerializer(typeof(Dictionary<string, object>));
        serializer.WriteObject(ms, _store);

        var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(_saveFileName, CreationCollisionOption.ReplaceExisting);

        using (var fs = await file.OpenStreamForWriteAsync())
        {
            //because we have written to the stream, set the position back to start
            ms.Seek(0, SeekOrigin.Begin);
            await ms.CopyToAsync(fs);
            await fs.FlushAsync();
        }
    }

"फिर से शुरू" राज्य हैंडलिंग

आपका आवेदन "निलंबित" राज्य से उपयोगकर्ता द्वारा खोला जा सकता है। "OnResuming" इवेंट हैंडलर करते समय इसका उपयोग किया जाता है। "App.xaml.cs" वर्ग में:

public App()
    {
        this.InitializeComponent();
        this.Suspending += OnSuspending;
        //Handle resuming operation:
        this.Resuming += App_Resuming;
    }

private void App_Resuming(object sender, object e)
    {
        //Do some operation connected with app resuming for instance refresh data
    }


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow