How to Convert Null value To Visibility in C++/WinRT ?
NullToVisibility.csをC++/WinRTに変換する方法は?
こんにちは、C#からC++/WinRTに"NullToVisibilityConverter.cs"を移植する最善の方法を知りたいと思います。
解決方法を教えて下さい。
該当するソースコード
NullToVisibilityConverter.cs
using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;
namespace AppUIBasics.Common
{
public class NullToVisibilityConverter : IValueConverter
{
public Visibility NullValue { get; set; } = Visibility.Collapsed;
public Visibility NonNullValue { get; set; } = Visibility.Visible;
public object Convert(object value, Type targetType, object parameter, string language)
{
return (value == null) ? NullValue : NonNullValue;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
}
自分で試したこと
- file: NullToVisibilityConverter.idl
NullToVisibilityConverter.idl
namespace TD2_WinUI3
{
[default_interface]
runtimeclass NullToVisibilityConverter : [default] Windows.UI.Xaml.Data.IValueConverter
{
NullToVisibilityConverter();
static Windows.UI.Xaml.DependencyProperty NullValueProperty{ get; };
static Windows.UI.Xaml.DependencyProperty NonNullValueProperty{ get; };
IInspectable NullValue;
IInspectable NonNullValue;
}
}
- file: NullToVisibilityConverter.h
NullToVisibilityConverter.h
#pragma once
#include "winrt/Microsoft.UI.Xaml.h"
#include "winrt/Microsoft.UI.Xaml.Markup.h"
#include "winrt/Microsoft.UI.Xaml.Controls.Primitives.h"
#include "NullToVisibilityConverter.g.h"
namespace winrt::TD2_WinUI3::implementation
{
struct NullToVisibilityConverter : NullToVisibilityConverterT<NullToVisibilityConverter>
{
NullToVisibilityConverter()
{
NullValue(winrt::box_value(Microsoft::UI::Xaml::Visibility::Collapsed));
NonNullValue(winrt::box_value(Microsoft::UI::Xaml::Visibility::Visible));
};
IInspectable NullValue() { return GetValue(m_nullValueProperty); };
void NullValue(IInspectable const& value) { SetValue(m_nullValueProperty, value); };
static DependencyProperty NullValueProperty() { return m_nullValueProperty; };
IInspectable NonNullValue() { return GetValue(m_nonNullValueProperty); };
void NonNullValue(IInspectable const& value) { SetValue(m_nonNullValueProperty, value); };
static DependencyProperty NonNullValueProperty() { return m_nonNullValueProperty; };
Windows::Foundation::IInspectable Convert(Windows::Foundation::IInspectable const& value, Windows::UI::Xaml::Interop::TypeName const& targetType, Windows::Foundation::IInspectable const& parameter, hstring const& language);
Windows::Foundation::IInspectable ConvertBack(Windows::Foundation::IInspectable const& value, Windows::UI::Xaml::Interop::TypeName const& targetType, Windows::Foundation::IInspectable const& parameter, hstring const& language);
private:
static DependencyProperty m_nullValueProperty;
static DependencyProperty m_nonNullValueProperty;
};
}
namespace winrt::TD2_WinUI3::factory_implementation
{
struct NullToVisibilityConverter : NullToVisibilityConverterT<NullToVisibilityConverter, implementation::NullToVisibilityConverter>
{
};
}
- file: NullToVisibilityConverter.cpp
NullToVisibilityConverter.cpp
#include "pch.h"
#include "NullToVisibilityConverter.h"
#include "NullToVisibilityConverter.g.cpp"
using namespace winrt::Windows;
using namespace winrt::Windows::UI::Xaml;
namespace winrt::TD2_WinUI3::implementation
{
DependencyProperty NullToVisibilityConverter::m_nullValueProperty = DependencyProperty::Register(L"NullValue", winrt::xaml_typename<IInspectable>(), winrt::xaml_typename <TD2_WinUI3::NullToVisibilityConverter>(), PropertyMetadata(nullptr));
DependencyProperty NullToVisibilityConverter::m_nonNullValueProperty = DependencyProperty::Register(L"NonNullValue", winrt::xaml_typename<IInspectable>(), winrt::xaml_typename <TD2_WinUI3::NullToVisibilityConverter>(), PropertyMetadata(nullptr));
Windows::Foundation::IInspectable NullToVisibilityConverter::Convert(Windows::Foundation::IInspectable const& value, Windows::UI::Xaml::Interop::TypeName const& /* targetType */, Windows::Foundation::IInspectable const& parameter , hstring const& /* language */)
{
// Retrieve the value as String.
return (value == nullptr) ? NullValue() : NonNullValue();
}
Windows::Foundation::IInspectable NullToVisibilityConverter::ConvertBack(Windows::Foundation::IInspectable const& /* value */, Windows::UI::Xaml::Interop::TypeName const& /* targetType */, Windows::Foundation::IInspectable const& /* parameter */, hstring const& /* language */)
{
throw hresult_not_implemented();
}
}
今回作成したものも github においておきます。
0