c++-winrt

Not a member of 'winrt::MyApp::implementation'


The problem is that only .xaml.g.hpp/.xaml.h files are included in XamlTypeInfo.g.cpp. But I created a "Custom Control file" which just has a .idl .h and .cpp files. Can I fix this?


I tried following https://learn.microsoft.com/en-us/windows/uwp/data-binding/data-binding-quickstart#formatting-or-converting-data-values-for-display


// .idl
namespace MyApp
{
    runtimeclass BooleanToVisibilityConverter : [default] Windows.UI.Xaml.Data.IValueConverter
    {
        BooleanToVisibilityConverter();
    }
}

// .h
#pragma once
#include "BooleanToVisibilityConverter.g.h"

namespace winrt::MyApp::implementation
{
    struct BooleanToVisibilityConverter : BooleanToVisibilityConverterT<BooleanToVisibilityConverter>
    {
        BooleanToVisibilityConverter() = default;

        winrt::Windows::Foundation::IInspectable Convert(winrt::Windows::Foundation::IInspectable const& value, winrt::Windows::UI::Xaml::Interop::TypeName const& targetType, winrt::Windows::Foundation::IInspectable const& parameter, hstring const& language);
        winrt::Windows::Foundation::IInspectable ConvertBack(winrt::Windows::Foundation::IInspectable const& value, winrt::Windows::UI::Xaml::Interop::TypeName const& targetType, winrt::Windows::Foundation::IInspectable const& parameter, hstring const& language);
    };
}
namespace winrt::MyApp::factory_implementation
{
    struct BooleanToVisibilityConverter : BooleanToVisibilityConverterT<BooleanToVisibilityConverter, implementation::BooleanToVisibilityConverter>
    {
    };
}

// .cpp
#include "pch.h"
#include "BooleanToVisibilityConverter.h"
#include "BooleanToVisibilityConverter.g.cpp"

namespace winrt::MyApp::implementation
{
    winrt::Windows::Foundation::IInspectable BooleanToVisibilityConverter::Convert(winrt::Windows::Foundation::IInspectable const& value, winrt::Windows::UI::Xaml::Interop::TypeName const& targetType, winrt::Windows::Foundation::IInspectable const& parameter, hstring const& language)
    {
        if (auto boolValue = winrt::unbox_value_or<bool>(value, false))
        {
            return winrt::box_value(boolValue ? winrt::Microsoft::UI::Xaml::Visibility::Visible : winrt::Microsoft::UI::Xaml::Visibility::Collapsed);
        }
        return winrt::box_value(winrt::Microsoft::UI::Xaml::Visibility::Collapsed);
    }
    winrt::Windows::Foundation::IInspectable BooleanToVisibilityConverter::ConvertBack(winrt::Windows::Foundation::IInspectable const& value, winrt::Windows::UI::Xaml::Interop::TypeName const& targetType, winrt::Windows::Foundation::IInspectable const& parameter, hstring const& language)
    {
        throw hresult_not_implemented();
    }
}

But I am getting an error when I do

<Page.Resources>
    <local:BooleanToVisibilityConverter  x:Key="BoolToVisConverter"/>
</Page.Resources>

error C2039: 'BooleanToVisibilityConverter': is not a member of 'winrt::MyApp::implementation'


Solution

  • The tutorial said to

    Also add #include "StringFormatter.h" to MainPage.xaml.h.

    MainPage.xaml.h is then included in XamlTypeInfo.g.cpp