A base class from which you can derive to implement your own custom observable associative collection. For more info, and code examples, see Collections with C++/WinRT.
Syntaxtemplate <typename D, typename K, typename V>
struct observable_map_base : map_base<D, K, V>
Template parameters
typename D
Your derived type name.
typename K
The type of the keys in the collection.
typename V
The type of the values in the collection.
Minimum supported SDK: Windows SDK version 10.0.17763.0 (Windows 10, version 1809)
Namespace: winrt
Header: %WindowsSdkDir%Include<WindowsTargetPlatformVersion>\cppwinrt\winrt\base.h (included by default)
Member functions IteratorsA observable_map_base is a range, and that range is defined by internal free functions (each of which retrieves an iterator) that are compatible with standard language features. Because of this, you can enumerate the elements in a observable_map_base object with a range-based for
statement.
You can also retrieve an IIterator from the observable_map_base::First function, and use that to iterate through the elements in a observable_map_base object.
...
#include <iostream>
using namespace winrt;
using namespace Windows::Foundation::Collections;
...
struct MyObservableMap :
implements<MyObservableMap, IObservableMap<winrt::hstring, int>, IMap<winrt::hstring, int>, IMapView<winrt::hstring, int>, IIterable<IKeyValuePair<winrt::hstring, int>>>,
winrt::observable_map_base<MyObservableMap, winrt::hstring, int>
{
auto& get_container() const noexcept
{
return m_values;
}
auto& get_container() noexcept
{
return m_values;
}
private:
std::map<winrt::hstring, int> m_values{
{ L"AliceBlue", 0xfff0f8ff }, { L"AntiqueWhite", 0xfffaebd7 }
};
};
...
IObservableMap<winrt::hstring, int> map{ winrt::make<MyObservableMap>() };
for (auto const& el : map)
{
std::wcout << el.Key().c_str() << L", " << std::hex << el.Value() << std::endl;
}
IIterator<IKeyValuePair<winrt::hstring, int>> it{ map.First() };
while (it.HasCurrent())
{
std::wcout << it.Current().Key().c_str() << L", " << std::hex << it.Current().Value() << std::endl;
it.MoveNext();
}
observable_map_base::Clear function
Removes all elements from the observable_map_base object.
Syntaxvoid Clear() noexcept;
observable_map_base::First function
Retrieves an IIterator representing the first element in the observable_map_base object.
Syntaxauto First();
Return value
An IIterator representing the first element in the observable_map_base object.
observable_map_base::GetView functionRetrieves an immutable view of the observable_map_base object.
Syntaxwinrt::Windows::Foundation::Collections::IMapView<K, V> GetView() const;
Return value
An IMapView containing an immutable view of the observable_map_base.
observable_map_base::HasKey functionDetermines whether the specified key belongs to an element in the observable_map_base object.
Syntaxbool HasKey(K const& key) const noexcept;
Parameters
key
The key to look for.
true
if an element containing the key is found, otherwise false
.
Inserts or updates an element in the observable_map_base object.
Syntaxbool Insert(K const& key, V const& value);
Parameters
key
The key associated with the element to insert or update.
value
The value to insert or replace.
true
if an element with the specified key was found and updated; otherwise false
.
Looks up the element identified by the specified key, and retrieves the corresponding value.
SyntaxV Lookup(K const& key) const;
Parameters
key
The key to look up.
The value corresponding to the key being looked up if found, otherwise a winrt::hresult_out_of_bounds exception is thrown.
observable_vector_base::MapChanged functionRegisters and/or revokes a delegate that handles the map-changed event of the observable_map_base object.
Syntax// Register
winrt::event_token MapChanged(winrt::Windows::Foundation::Collections::MapChangedEventHandler<K, V> const& handler);
// Revoke with event_token
void MapChanged(winrt::event_token const cookie);
// Revoke with event_revoker
MapChanged_revoker MapChanged(winrt::auto_revoke_t, winrt::Windows::Foundation::Collections::MapChangedEventHandler<K, V> const& handler) const
Return value
Either void
, a winrt::event_token with which you can revoke a registered delegate, or a MapChanged_revoker (a type alias for a winrt::event_revoker<IObservableMap<K, V>>) with which you can revoke a registered delegate.
winrt::event_revoker<IObservableMap<winrt::hstring, int>> m_event_revoker;
...
m_event_revoker = map.MapChanged(winrt::auto_revoke, [this](IObservableMap<winrt::hstring, int> const&, IMapChangedEventArgs<winrt::hstring> const&)
{
...
});
observable_map_base::Remove function
Removes an element from the observable_map_base object.
Syntaxvoid Remove(K const& key);
Parameters
key
The key associated with the element to remove.
Retrieves the number of elements in the observable_map_base object.
Syntaxuint32_t Size() const noexcept;
Return value
A value representing the number of elements in the observable_map_base object.
See alsoRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4