Application.Properties 属性

定义

获取应用程序范围属性的集合。

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

属性值

一个包含应用程序范围的属性的 IDictionary

示例

以下示例演示如何使用 Properties创建和使用应用程序范围属性。

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"])
            {
                this.Title += " [SafeMode]";
            }
        }
    }
}

注解

Application 公开可用于存储应用程序范围属性的字典 Properties 。 这使你能够以线程安全的方式在所有代码 AppDomain 之间共享状态,而无需编写自己的状态代码。

存储在 中的 Properties 属性必须转换为返回的相应类型。

属性 Properties 是线程安全的,可从任何线程获取。

适用于

产品 版本
.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, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅