2#include <winrt/Windows.Foundation.h>
16 cppxaml::xaml::FrameworkElement FindChildWithVSG(cppxaml::xaml::FrameworkElement fe) {
17 cppxaml::xaml::FrameworkElement root{
nullptr };
18 for (
auto i = 0; i < cppxaml::xaml::Media::VisualTreeHelper::GetChildrenCount(fe); i++) {
19 auto child = cppxaml::xaml::Media::VisualTreeHelper::GetChild(fe, i);
20 if (
auto childFE = child.try_as<cppxaml::xaml::FrameworkElement>()) {
21 winrt::Windows::Foundation::Collections::IVector<cppxaml::xaml::VisualStateGroup> vsgs = cppxaml::xaml::VisualStateManager::GetVisualStateGroups(childFE);
22 if (vsgs.Size() != 0) {
32 struct VSMListener : winrt::implements<VSMListener, winrt::Windows::Foundation::IInspectable> {
34 VSMListener(cppxaml::xaml::FrameworkElement fe,
const std::unordered_map<std::wstring, cppxaml::xaml::VisualStateChangedEventHandler>& map) : m_map(map) {
35 if (
auto root = FindChildWithVSG(fe)) {
36 for (
const cppxaml::xaml::VisualStateGroup& vsg : cppxaml::xaml::VisualStateManager::GetVisualStateGroups(root)) {
37 vsg.CurrentStateChanged([_this = this->get_strong(), fe](winrt::Windows::Foundation::IInspectable sender, cppxaml::xaml::VisualStateChangedEventArgs args) {
39 auto newState = args.NewState();
40 auto newName = newState.Name();
41 const auto newNameStr = newName.c_str();
42 if (_this->m_map.find(newNameStr) != _this->m_map.end()) {
43 _this->m_map[newNameStr](fe, args);
52 std::unordered_map<std::wstring, cppxaml::xaml::VisualStateChangedEventHandler> m_map{};
56 template<
typename TFrameworkElement>
57 auto VSMListener(TFrameworkElement obj,
const std::unordered_map<std::wstring, cppxaml::xaml::VisualStateChangedEventHandler>& map) {
58 cppxaml::xaml::FrameworkElement fe(obj);
60 fe.Loaded([map](winrt::Windows::Foundation::IInspectable sender,
auto&) {
61 auto listener = winrt::make_self<cppxaml::details::VSMListener>(sender.as<cppxaml::xaml::FrameworkElement>(), map);
65 auto listener = winrt::make_self<cppxaml::details::VSMListener>(fe, map);
The main CppXAML namespace.