IPropertySet 接口

定义

表示键值对的集合,并关联其他几个集合接口。

public interface class IPropertySet : IIterable<IKeyValuePair<Platform::String ^, Platform::Object ^> ^>, IMap<Platform::String ^, Platform::Object ^>, IObservableMap<Platform::String ^, Platform::Object ^>
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.FoundationContract, 65536)]
/// [Windows.Foundation.Metadata.Guid(2319707551, 62694, 17441, 172, 249, 29, 171, 41, 134, 130, 12)]
/// [Windows.Foundation.Metadata.HasVariant]
struct IPropertySet : IIterable<IKeyValuePair<winrt::hstring, IInspectable const&>>, IMap<winrt::hstring, IInspectable const&>, IObservableMap<winrt::hstring, IInspectable const&>
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.FoundationContract), 65536)]
[Windows.Foundation.Metadata.Guid(2319707551, 62694, 17441, 172, 249, 29, 171, 41, 134, 130, 12)]
[Windows.Foundation.Metadata.HasVariant]
public interface IPropertySet : IDictionary<string,object>, IEnumerable<KeyValuePair<string,object>>, IObservableMap<string,object>
Public Interface IPropertySet
Implements IDictionary(Of String, Object), IEnumerable(Of KeyValuePair(Of String, Object)), IObservableMap(Of String, Object)
派生
属性
实现
IMap<K,V> IDictionary<K,V> IDictionary<String,Object> IMap<Platform::String,Platform::Object> IMap<winrt::hstring,IInspectable> IIterable<IKeyValuePair<K,V>> IEnumerable<KeyValuePair<K,V>> IEnumerable<KeyValuePair<String,Object>> IIterable<IKeyValuePair<Platform::String,Platform::Object>> IIterable<IKeyValuePair<winrt::hstring,IInspectable>> IObservableMap<String,Object> IObservableMap<Platform::String,Platform::Object> IObservableMap<winrt::hstring,IInspectable>

Windows 要求

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

示例

此示例演示如何为 Windows 运行时 属性返回的 IPropertySet 对象中的项检查。 具体而言,这是来自 CoreApplication.Properties 属性。 此代码片段来自 ControlChannelTrigger HTTP 客户端示例。 请注意 C# 版本如何强制转换为 IDictionary<string,object> ,以便它可以使用索引器,而 Visual C++ 组件扩展 (C++/CX) 版本使用 HasKeyLookup。 通常,只需使用从返回类型的各种Windows 运行时属性获取的 IPropertySet 对象即可:在属性集中查找特定键,然后将某些应用属性设置为相应的值(如果存在)。

// In this example, the channel name has been hardcoded to lookup the property bag 
// for any previous contexts. The channel name may be used in more sophisticated ways 
// in case an app has multiple ControlChannelTrigger objects. 
string channelId = "channelOne";
if (((IDictionary<string, object>)CoreApplication.Properties).ContainsKey(channelId))
{
    try
    {
        AppContext appContext = null;
        lock(CoreApplication.Properties)
        {
            appContext = ((IDictionary<string, object>)CoreApplication.Properties)[channelId] as AppContext;
        }
        if (appContext != null && appContext.CommunicationInstance != null)
        {
            CommunicationModule communicationInstance = appContext.CommunicationInstance;

            // Clear any existing channels, sockets etc. 
            communicationInstance.Reset();

            // Create CCT enabled transport. 
            communicationInstance.SetUpTransport(communicationInstance.serverUri, GetType().Name);
        }
    }
    catch (Exception ex)
    {
        Diag.DebugPrint("Registering with CCT failed with: " + ex.ToString());
    }
}
else
{
    Diag.DebugPrint("Cannot find AppContext key channelOne");
}
// In this example, the channel name has been hardcoded to look up the property bag
// for any previous contexts. The channel name may be used in more sophisticated ways
// in case an app has multiple ControlChannelTrigger objects.
std::wstring channelId{ L"channelOne" };
if (CoreApplication::Properties().HasKey(channelId))
{
    try
    {
        auto appContext{ CoreApplication::Properties().Lookup(channelId).as<AppContext>() };
        if (appContext && appContext->CommunicationInstance())
        {
            CommunicationModule communicationInstance{ appContext->CommunicationInstance() };

            // Clear any existing channels, sockets etc.
            communicationInstance.Reset();

            // Create CCT enabled transport.
            communicationInstance.SetUpTransport(L"NetworkChangeTask");
        }
    }
    catch (winrt::hresult_error const& ex)
    {
        Diag::DebugPrint(L"Registering with CCT failed with: " + ex.message());
    }
}
else
{
    Diag::DebugPrint(L"Cannot find AppContext key channelOne");
}
// In this example, the channel name has been hardcoded to look up the property bag
// for any previous contexts. The channel name may be used in more sophisticated ways
// in case an app has multiple ControlChannelTrigger objects.
String^ channelId = "channelOne";
if (CoreApplication::Properties->HasKey(channelId))
{
    try
    {
        auto appContext = dynamic_cast<AppContext^>(CoreApplication::Properties->Lookup(channelId));
        if (appContext != nullptr && appContext->CommunicationInstance != nullptr)
        {
            CommunicationModule^ communicationInstance = appContext->CommunicationInstance;

            // Clear any existing channels, sockets etc. 
            communicationInstance->Reset();

            // Create CCT enabled transport 
            communicationInstance->SetUpTransport("NetworkChangeTask");
        }
    }
    catch (Exception^ ex)
    {
        Diag::DebugPrint("Registering with CCT failed with: " + ex->Message);
    }
}
else
{
    Diag::DebugPrint("Cannot find AppContext key channelOne");
}

注解

此接口不同寻常,因为它不定义任何新成员。 相反,它会关联其他三个集合接口,以便它们共享相同的类型参数约束:

如果将对象强制转换为 IPropertySet (这不是泛型) 则可以使用这三个接口的组合方法,具体取决于使用 字符串 作为键, 将 Object 用于值。 对于实际实现,Windows 运行时使用 PropertySet 类。 请参阅 PropertySet 各个成员的文档,了解如何在强制转换为 IPropertySet 后使用这些方法。

在许多情况下,Windows 运行时 API 使用 PropertySet 作为值,它实际上在签名中显示为 IPropertySet。 可以将 PropertySet 视为可供应用代码使用的 IPropertySet 的实际实现。 JavaScript 代码可以将任何 IPropertySet 值视为实现 PropertySet 原型。 按类型使用是 IPropertySet 的main方案;实际上,在应用代码中实现 接口并不常见。

IPropertySet 也由 ValueSet 类实现。 ValueSet 是多个 ContinuationData 属性的值类型,当应用在调用可能停用应用的 AndContinue 方法后恢复时,这些属性可启用还原状态。 ValueSet 和所有 ContinuationData 属性和 AndContinue 方法仅Windows Phone API,因为它只有具有此行为的Windows Phone。 有关详细信息,请参阅调用 AndContinue 方法后如何继续Windows Phone应用商店应用ValueSetPropertySet 的区别在于,Insert 等方法的 ValueSet 实现强制其属性值为值类型。

事件

MapChanged

在映射更改时发生。

(继承自 IObservableMap<K,V>)

适用于

另请参阅