A base class from which you can derive to implement your own custom observable vector. For more info, and code examples, see Collections with C++/WinRT.
Syntaxtemplate <typename D, typename T>
struct observable_vector_base : vector_base<D, T>
Template parameters
typename D
Your derived type name.
typename T
The type of the elements in the observable_vector_base.
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_vector_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_vector_base object with a range-based for
statement.
You can also retrieve an IIterator from the observable_vector_base::First function, and use that to iterate through the elements in a observable_vector_base object.
...
#include <iostream>
using namespace winrt;
using namespace Windows::Foundation::Collections;
...
struct MyObservableVector :
implements<MyObservableVector, IObservableVector<float>, IVector<float>, IVectorView<float>, IIterable<float>>,
winrt::observable_vector_base<MyObservableVector, float>
{
auto& get_container() const noexcept
{
return m_values;
}
auto& get_container() noexcept
{
return m_values;
}
private:
std::vector<float> m_values{ 0.1f, 0.2f, 0.3f };
};
...
IObservableVector<float> coll{ winrt::make<MyObservableVector>() };
for (auto const& el : coll)
{
std::wcout << el << std::endl;
}
IIterator<float> it{ coll.First() };
while (it.HasCurrent())
{
std::wcout << it.Current() << std::endl;
it.MoveNext();
}
observable_vector_base::Append function
Appends an element to the end of the observable_vector_base object.
Syntaxvoid Append(T const& value);
Parameters
value
The element to append.
Removes all elements from the observable_vector_base object.
Syntaxvoid Clear() noexcept;
observable_vector_base::First function
Retrieves an IIterator representing the first element in the observable_vector_base object.
Syntaxauto First();
Return value
An IIterator representing the first element in the observable_vector_base object.
observable_vector_base::GetAt functionRetrieves the element at the specified index in the observable_vector_base object.
SyntaxT GetAt(uint32_t const index) const;
Parameters
index
A zero-based element index.
The element at the specified index in the observable_vector_base object.
observable_vector_base::GetMany functionRetrieves a collection of elements in the observable_vector_base object beginning at the given index.
Syntaxuint32_t GetMany(uint32_t const startIndex, array_view<T> values) const;
Parameters
startIndex
A zero-based element index to start at.
values
An array_view to copy the items into.
A value representing the number of elements retrieved.
observable_vector_base::GetView functionRetrieves an immutable view of the observable_vector_base object.
Syntaxwinrt::Windows::Foundation::Collections::IVectorView<T> GetView() const noexcept;
Return value
An IVectorView containing an immutable view of the observable_vector_base.
observable_vector_base::IndexOf functionRetrieves the index of a specified element in the observable_vector_base object.
Syntaxbool IndexOf(T const& value, uint32_t& index) const noexcept;
Parameters
value
The element, in the observable_vector_base object, to look for.
index
The zero-based index of the element if the element is found, otherwise the number of elements in the observable_vector_base object.
true
if the element is found, otherwise false
.
Inserts an element at the specified index in the observable_vector_base object.
Syntaxvoid InsertAt(uint32_t const index, T const& value);
Parameters
index
The zero-based index at which to insert the element.
value
The element to insert.
Removes the element at the specified index in the observable_vector_base object.
Syntaxvoid RemoveAt(uint32_t const index);
Parameters
index
The zero-based index of the element to remove.
Removes the last element from the observable_vector_base object.
Syntaxvoid RemoveAtEnd();
observable_vector_base::ReplaceAll function
Replaces all the elements in the observable_vector_base object with the specified elements.
Syntaxvoid ReplaceAll(array_view<T const> value);
Parameters
value
An array_view containing the new elements.
Sets the value of the element at the specified index in the observable_vector_base object.
Syntaxvoid SetAt(uint32_t const index, T const& value);
Parameters
index
The zero-based index of the element whose value to set.
value
The element value to set.
Retrieves the number of elements in the observable_vector_base object.
Syntaxuint32_t Size() const noexcept;
Return value
A value representing the number of elements in the observable_vector_base object.
observable_vector_base::VectorChanged functionRegisters and/or revokes a delegate that handles the vector-changed event of the observable_vector_base object.
Syntax// Register
winrt::event_token VectorChanged(winrt::Windows::Foundation::Collections::VectorChangedEventHandler<T> const& handler);
// Revoke with event_token
void VectorChanged(winrt::event_token const cookie);
// Revoke with event_revoker
VectorChanged_revoker VectorChanged(winrt::auto_revoke_t, winrt::Windows::Foundation::Collections::VectorChangedEventHandler<T> const& handler) const;
Return value
Either void
, a winrt::event_token with which you can revoke a registered delegate, or a VectorChanged_revoker (a type alias for a winrt::event_revoker<IObservableVector<T>>) with which you can revoke a registered delegate.
winrt::event_revoker<IObservableVector<float>> m_event_revoker;
...
m_event_revoker = coll.VectorChanged(winrt::auto_revoke, [this](IObservableVector<float> const&, IVectorChangedEventArgs const&)
{
...
});
See also
RetroSearch 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