ApplicationDataContainer 类

定义

表示应用设置的容器。 此类的方法和属性支持创建、删除、枚举和遍历容器层次结构。

public ref class ApplicationDataContainer sealed
public ref class ApplicationDataContainer sealed : IClosable
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
class ApplicationDataContainer final
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
class ApplicationDataContainer final : IClosable
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
public sealed class ApplicationDataContainer
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
public sealed class ApplicationDataContainer : System.IDisposable
Public NotInheritable Class ApplicationDataContainer
Public NotInheritable Class ApplicationDataContainer
Implements IDisposable
继承
Object Platform::Object IInspectable ApplicationDataContainer
属性
实现

Windows 要求

设备系列
Windows 10 (在 10.0.10240.0 中引入)
API contract
Windows.Foundation.UniversalApiContract (在 v1.0 中引入)

示例

调用 ApplicationDataContainer.CreateContainer 方法以创建设置容器,或返回现有容器。 此示例创建名为 exampleContainer 的设置容器,并添加名为 的设置 exampleSettingApplicationDataCreateDisposition 枚举中的 Always 值指示应创建容器(如果尚不存在)。

使用 ApplicationDataContainer.Values 属性访问 exampleSetting 容器中的 exampleContainer 设置。

使用完设置容器后,调用 ApplicationDataContainer.DeleteContainer 方法来删除 exampleContainer 设置容器。

Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;

// Create a setting in a container

Windows.Storage.ApplicationDataContainer container = 
   localSettings.CreateContainer("exampleContainer", Windows.Storage.ApplicationDataCreateDisposition.Always);

if (localSettings.Containers.ContainsKey("exampleContainer"))
{
   localSettings.Containers["exampleContainer"].Values["exampleSetting"] = "Hello, Windows!";
}

// Read data from a setting in a container

bool hasContainer = localSettings.Containers.ContainsKey("exampleContainer");
bool hasSetting = false;

if (hasContainer)
{
   hasSetting = localSettings.Containers["exampleContainer"].Values.ContainsKey("exampleSetting");
}

// Delete a container

localSettings.DeleteContainer("exampleContainer");
Windows::Storage::ApplicationDataContainer localSettings{
    Windows::Storage::ApplicationData::Current().LocalSettings() };

// Create a setting in a container.
Windows::Storage::ApplicationDataContainer container{
    localSettings.CreateContainer(L"exampleContainer", Windows::Storage::ApplicationDataCreateDisposition::Always) };

if (localSettings.Containers().HasKey(L"exampleContainer"))
{
    auto values{ localSettings.Containers().Lookup(L"exampleContainer").Values() };
    values.Insert(L"exampleSetting", winrt::box_value(L"Hello, Windows!"));
}

// Read data from a setting in a container.
bool hasContainer{ localSettings.Containers().HasKey(L"exampleContainer") };
bool hasSetting{ false };

if (hasContainer)
{
    auto values{ localSettings.Containers().Lookup(L"exampleContainer").Values() };
    hasSetting = values.HasKey(L"exampleSetting");
}

// Delete a container.
localSettings.DeleteContainer(L"exampleContainer");
ApplicationDataContainer^ localSettings = ApplicationData::Current->LocalSettings;

// Create a setting in a container

ApplicationDataContainer^ container = 
   localSettings->CreateContainer("exampleContainer", ApplicationDataCreateDisposition::Always);

if (localSettings->Containers->HasKey("exampleContainer"))
{
   auto values = localSettings->Containers->Lookup("exampleContainer")->Values;
   values->Insert("exampleSetting", "Hello, Windows!");
}

// Read data from a setting in a container

bool hasContainer = localSettings->Containers->HasKey("exampleContainer");
bool hasSetting = false;

if (hasContainer)
{
   auto values = localSettings->Containers->Lookup("exampleContainer")->Values;
   hasSetting = values->HasKey("exampleSetting");
}

// Delete a container

localSettings->DeleteContainer("exampleContainer");
Dim localSettings As Windows.Storage.ApplicationDataContainer = Windows.Storage.ApplicationData.Current.LocalSettings

' Create a setting in a container

Dim container As Windows.Storage.ApplicationDataContainer = 
   localSettings.CreateContainer("exampleContainer", Windows.Storage.ApplicationDataCreateDisposition.Always)

If localSettings.Containers.ContainsKey("exampleContainer") Then
    localSettings.Containers("exampleContainer").Values("exampleSetting") = "Hello, Windows!"
End If

' Read data from a setting in a container

Dim hasContainer As Boolean = localSettings.Containers.ContainsKey("exampleContainer")
Dim hasSetting As Boolean = False

If hasContainer Then
    hasSetting = localSettings.Containers("exampleContainer").Values.ContainsKey("exampleSetting")
End If

' Delete a container

localSettings.DeleteContainer("exampleContainer")

注解

注意

临时应用数据存储没有设置容器。

Values 属性获取 ApplicationDataContainerSettings 对象,该对象提供对存储在容器中的设置的访问权限。

属性

Containers

获取此应用程序设置容器的子应用程序设置容器。

Locality

获取与当前设置容器关联的应用数据存储 (本地或漫游) 的类型。

Name

获取当前设置容器的名称。

Values

获取一个 对象,该对象表示此设置容器中的设置。

方法

Close()

注意

此成员未在 C# 中实现。

CreateContainer(String, ApplicationDataCreateDisposition)

在当前设置容器中创建或打开指定的设置容器。

DeleteContainer(String)

删除指定的设置容器、其子容器以及层次结构中的所有应用程序设置。

Dispose()

执行与释放或重置非托管资源关联的应用程序定义的任务。

适用于

另请参阅