如何在 C++ UWP 项目中使用 TimeSpan-Elements 初始化和填充 IVector?

How to initialize and fill an IVector with TimeSpan-Elements in a C++ UWP project?

我正在尝试在 C++/CX UWP 项目中以定义的时间间隔从现有视频中截取屏幕截图。我的想法是使用 MediaComposition Library:

中的函数 "GetThumbnailsAsync"
    create_task(MediaClip::CreateFromFileAsync(this->last_video)).then([this](MediaClip^ clip )
{
    // Create a MediaComposition containing the clip and set it on the MediaElement.
    MediaComposition^ composition = ref new MediaComposition();
    composition->Clips->Append(clip);

    TimeSpan ts;
    ts.Duration = 1000;

    IVector<TimeSpan>^ i_ts_vector;
    //TODO

    create_task(composition->GetThumbnailsAsync(is_ts_vector, 0, 0, VideoFramePrecision::NearestFrame)).then([this, clip, composition](IVectorView<ImageStream^>^ imageStream)
    {
        //TODO
    });
});

last_video 是带有视频路径的 StorageFile。

这不起作用,因为 i_ts_vector 未初始化且仍为 NULL。我已经尝试过类似的东西:

IVector<TimeSpan>^ i_ts_vector = ref new Vector<TimeSpan>();

这适用于 int 向量,但不适用于 TimeSpan 向量。它给出了一个编译器错误:

Error C2678 binary '==': no operator found which takes a left-hand operand of type 'const Windows::Foundation::TimeSpan' (or there is no acceptable conversion)

如何使用 TimeSpan-Elements 初始化和填充 IVector?或者有更好的截图方式吗?

科罗诺

这里的问题是(参考 Value types in Vector

Any element to be stored in a Platform::Collections::Vector must support equality comparison, either implicitly or by using a custom std::equal_to comparator that you provide. All reference types and all scalar types implicitly support equality comparisons. 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.

Time​Span Struct也是一种没有相等运算的非标量值类型(operator==)。所以你得到了Error C2678。要解决此问题,您可以提供如下自定义仿函数:

struct MyEqual : public std::binary_function<const TimeSpan, const TimeSpan, bool>
{
    bool operator()(const TimeSpan& _Left, const TimeSpan& _Right) const
    {
        return _Left.Duration == _Right.Duration;
    }
};

然后在 Platform::Collections::Vector 中使用它,例如:

IVector<TimeSpan>^ i_ts_vector = ref new Platform::Collections::Vector<TimeSpan, MyEqual>();