Lire en anglais

Partager via


Application.Properties Propriété

Définition

Obtient une collection de propriétés de portée application.

C#
public System.Collections.IDictionary Properties { get; }

Valeur de propriété

IDictionary

IDictionary qui contient les propriétés de portée application.

Exemples

L’exemple suivant montre comment créer et utiliser une propriété d’étendue d’application à l’aide Propertiesde .

XAML
<Application x:Class="CSharp.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"
    >
</Application>
C#
using System;
using System.Windows;

namespace CSharp
{
    public partial class App : Application
    {
        void App_Startup(object sender, StartupEventArgs e)
        {
            // Parse command line arguments for "/SafeMode"
            this.Properties["SafeMode"] = false;
            for (int i = 0; i != e.Args.Length; ++i)
            {
                if (e.Args[i].ToLower() == "/safemode")
                {
                    this.Properties["SafeMode"] = true;
                    break;
                }
            }
        }
    }
}
XAML
<Window x:Class="CSharp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Loaded="MainWindow_Loaded"
    >
  <Grid>
XAML
  </Grid>
</Window>
C#
using System;
using System.Windows;
using System.Windows.Controls;

namespace CSharp
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        void MainWindow_Loaded(object sender, EventArgs e)
        {
            // Check for safe mode
            if ((bool)Application.Current.Properties["SafeMode"] == true)
            {
                this.Title += " [SafeMode]";
            }
        }
    }
}

Remarques

Application expose un dictionnaire via Properties lequel vous pouvez utiliser pour stocker les propriétés d’étendue d’application. Cela vous permet de partager l’état parmi tous les codes d’une AppDomain manière thread-safe, sans avoir à écrire votre propre code d’état.

Les propriétés stockées dans Properties doivent être converties en type approprié retourné.

La Properties propriété est thread safe et est disponible à partir de n’importe quel thread.

S’applique à

Produit Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
Windows Desktop 3.0, 3.1, 5, 6, 7

Voir aussi