如何使用 C++CX 中的 Calendarview::setdensitycolors 方法

How to use the Calendarview::setdensitycolors method from C++CX

这是我要调用的方法:

https://msdn.microsoft.com/en-us/library/windows/apps/dn890067.aspx

所以我创建了一个 Platform::Collections::Vector 并填充它,很简单吧?

Platform::Collections::Vector<Windows::UI::Color>^ dayColors = ref new Platform::Collections::Vector<Windows::UI::Color>();
dayColors->Append(Windows::UI::Colors::Green);
myCalendarView->SetDensityColors(dayColors);

但是,我遇到了这个我一辈子都无法解决的编译错误:

error C2678: 二进制“==”: 未找到采用 'const Windows::UI::Color' 类型左手操作数的运算符(或没有可接受的转换)

我该如何解决这个问题?

error C2678: binary '==': no operator found which takes a left hand operand of type 'const Windows::UI::Color' (or there is no acceptable convertion)

这个错误实际上是由代码行Platform::Collections::Vector<Windows::UI::Color>^ dayColors抛出的。根据 Collections (C++/CX) 文档中 Vector 的值类型:

For non-scalar value types such as Windows::Foundation::DateTime, or for custom comparisons—for example, objA->UniqueID == objB->UniqueID—you must provide a custom function object.

Windows::UI::Color 是结构类型,可能包含自定义比较,因此需要自定义函数对象。

按如下方式添加自定义结构将解决您的问题:

struct MyEqual : public std::binary_function<const Windows::UI::Color, const Windows::UI::Color, bool>
{
    bool operator()(const Windows::UI::Color& _Left, const Windows::UI::Color& _Right) const
    {
        return _Left.A == _Right.A;
    };
};

void CCalendarView2::MainPage::CalendarView_CalendarViewDayItemChanging(Windows::UI::Xaml::Controls::CalendarView^ sender, Windows::UI::Xaml::Controls::CalendarViewDayItemChangingEventArgs^ args)
{
    Platform::Collections::Vector<Windows::UI::Color, MyEqual>^ dayColors = ref new Platform::Collections::Vector<Windows::UI::Color,MyEqual>();
    dayColors->Append(Windows::UI::Colors::Green); 
    args->Item->SetDensityColors(dayColors);
}