Megosztás a következőn keresztül:


Útmutató: Application-Scope tulajdonságainak megőrzése és visszaállítása az alkalmazás munkamenetei között

Ez a példa bemutatja, hogyan őrizheti meg az alkalmazás hatókörének tulajdonságait az alkalmazás leállásakor, és hogyan állíthatja vissza az alkalmazás hatókörének tulajdonságait az alkalmazás következő indításakor.

példa

Az alkalmazás megőrzi az alkalmazás hatókörének tulajdonságait, és visszaállítja őket az elkülönített tárolóból. Az izolált tároló egy védett tárolóterület, amelyet az alkalmazások biztonságosan használhatnak fájlhozzáférés-engedély nélkül. Az App.xaml fájl az App_Startup esemény kezelőjeként határozza meg a Application.Startup metódust, a App_Exit metódust pedig az Application.Exit esemény kezelőjeként, ahogyan az a következő XAML kiemelt soraiban látható:

Megjegyzés:

A CSharphez a következő XAML van megírva. A Visual Basic verziója kihagyja az osztálydeklarációt.

<Application x:Class="SDKSamples.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml"
             Startup="App_Startup"
             Exit="App_Exit">
</Application>

Ez a következő példa az alkalmazás mögötti kódját mutatja be, amely az XAML eseménykezelőiből áll. A App_Startup metódus visszaállítja az alkalmazás hatókörének tulajdonságait, a App_Exit metódus pedig menti az alkalmazás hatókörének tulajdonságait.

using System.IO.IsolatedStorage;
using System.IO;
using System.Windows;

namespace SDKSamples
{
    public partial class App : Application
    {
        string _filename = "App.data";

        public App()
        {
            // Initialize application-scope property
            Properties["NumberOfAppSessions"] = "0";
        }

        private void App_Startup(object sender, StartupEventArgs e)
        {
            // Restore application-scope property from isolated storage
            IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForDomain();
            try
            {
                if (storage.FileExists(_filename))
                {
                    using (IsolatedStorageFileStream stream = storage.OpenFile(_filename, FileMode.Open, FileAccess.Read))
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        // Restore each application-scope property individually
                        while (!reader.EndOfStream)
                        {
                            string[] keyValue = reader.ReadLine().Split(new char[] { ',' });
                            Properties[keyValue[0]] = keyValue[1];
                        }
                    }
                }
            }
            catch (DirectoryNotFoundException ex)
            {
                // Path the file didn't exist
            }
            catch (IsolatedStorageException ex)
            {
                // Storage was removed or doesn't exist
                // -or-
                // If using .NET 6+ the inner exception contains the real cause
            }
        }

        private void App_Exit(object sender, ExitEventArgs e)
        {
            // Increase the amount of times the app was opened
            Properties["NumberOfAppSessions"] = int.Parse((string)Properties["NumberOfAppSessions"]) + 1;

            // Persist application-scope property to isolated storage
            IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForDomain();
            using (IsolatedStorageFileStream stream = storage.OpenFile(_filename, FileMode.Create, FileAccess.Write))
            using (StreamWriter writer = new StreamWriter(stream))
            {
                // Persist each application-scope property individually
                foreach (string key in Properties.Keys)
                    writer.WriteLine("{0},{1}", key, Properties[key]);
            }
        }
    }

}
Imports System.IO
Imports System.IO.IsolatedStorage

Class Application

    Private _filename As String = "App.data"

    Public Sub New()
        ' Initialize application-scope property
        Properties("NumberOfAppSessions") = "0"
    End Sub

    Private Sub App_Startup(ByVal sender As Object, ByVal e As StartupEventArgs)
        ' Restore application-scope property from isolated storage
        Dim storage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForDomain()
        Try
            If storage.FileExists(_filename) Then

                Using stream As IsolatedStorageFileStream = storage.OpenFile(_filename, FileMode.Open, FileAccess.Read)
                    Using reader As New StreamReader(stream)

                        ' Restore each application-scope property individually
                        Do While Not reader.EndOfStream
                            Dim keyValue() As String = reader.ReadLine().Split(New Char() {","c})
                            Properties(keyValue(0)) = keyValue(1)
                        Loop

                    End Using
                End Using

            End If

        Catch ex As DirectoryNotFoundException
            ' Path the file didn't exist
        Catch ex As IsolatedStorageException
            ' Storage was removed or doesn't exist
            ' -or-
            ' If using .NET 6+ the inner exception contains the real cause
        End Try
    End Sub

    Private Sub App_Exit(ByVal sender As Object, ByVal e As ExitEventArgs)
        'Increase the amount of times the app was opened
        Properties("NumberOfAppSessions") = Integer.Parse(DirectCast(Properties("NumberOfAppSessions"), String)) + 1

        ' Persist application-scope property to isolated storage
        Dim storage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForDomain()

        Using stream As IsolatedStorageFileStream = storage.OpenFile(_filename, FileMode.Create, FileAccess.Write)
            Using writer As New StreamWriter(stream)

                ' Persist each application-scope property individually
                For Each key As String In Properties.Keys
                    writer.WriteLine("{0},{1}", key, Properties(key))
                Next key

            End Using
        End Using
    End Sub

End Class