正确实现ListView的ItemSource

时间:2016-12-15 21:07:52

标签: c++ xaml listview uwp c++-cx

我尝试在Microsoft文档here之后实现一个可以ItemSource作为ListView的类,即实现IObservableVector<T>但是一如既往,文档永远不会有效,没有C ++示例代码开头。我的XAML for MainPage的内容只是

<ListView x:Name="TestListView"/>

和源代码

public ref class MyStringSrc sealed : Windows::Foundation::Collections::IObservableVector<Platform::String^>
{
public:
    // Inherited via IVector
    virtual Windows::Foundation::Collections::IIterator<Platform::String ^> ^ First();

    // Inherited via IObservableVector
    virtual property unsigned int Size
    {
        unsigned int get()
        {
            return _size;
        }
    }
    virtual Platform::String ^ GetAt(unsigned int index);
    virtual Windows::Foundation::Collections::IVectorView<Platform::String ^> ^ GetView();
    virtual bool IndexOf(Platform::String ^value, unsigned int *index);
    virtual void SetAt(unsigned int index, Platform::String ^value);
    virtual void InsertAt(unsigned int index, Platform::String ^value);
    virtual void RemoveAt(unsigned int index);
    virtual void Append(Platform::String ^value);
    virtual void RemoveAtEnd();
    virtual void Clear();
    virtual unsigned int GetMany(unsigned int startIndex, Platform::WriteOnlyArray<Platform::String ^, 1U> ^items);
    virtual void ReplaceAll(const Platform::Array<Platform::String ^, 1U> ^items);
    virtual event Windows::Foundation::Collections::VectorChangedEventHandler<Platform::String ^> ^ VectorChanged;
private:
    unsigned int _size = 1000;
};

public ref class MainPage sealed
{
public:
    MainPage();
};
MainPage::MainPage()
{
    InitializeComponent();
    TestListView->ItemsSource = ref new MyStringSrc();
}

Windows::Foundation::Collections::IIterator<Platform::String ^> ^ MyStringSrc::First()
{
    throw ref new Platform::NotImplementedException();
    // TODO: insert return statement here
}

Platform::String ^ MyStringSrc::GetAt(unsigned int index)
{
    throw ref new Platform::NotImplementedException();
    // TODO: insert return statement here
}

Windows::Foundation::Collections::IVectorView<Platform::String ^> ^ MyStringSrc::GetView()
{
    throw ref new Platform::NotImplementedException();
    // TODO: insert return statement here
}

bool MyStringSrc::IndexOf(Platform::String ^value, unsigned int *index)
{
    return false;
}

void MyStringSrc::SetAt(unsigned int index, Platform::String ^value)
{
    throw ref new Platform::NotImplementedException();
}

void MyStringSrc::InsertAt(unsigned int index, Platform::String ^value)
{
    throw ref new Platform::NotImplementedException();
}

void MyStringSrc::RemoveAt(unsigned int index)
{
    throw ref new Platform::NotImplementedException();
}

void MyStringSrc::Append(Platform::String ^value)
{
    throw ref new Platform::NotImplementedException();
}

void MyStringSrc::RemoveAtEnd()
{
    throw ref new Platform::NotImplementedException();
}

void MyStringSrc::Clear()
{
    throw ref new Platform::NotImplementedException();
}

unsigned int MyStringSrc::GetMany(unsigned int startIndex, Platform::WriteOnlyArray<Platform::String ^, 1U> ^items)
{
    return 0;
}

void MyStringSrc::ReplaceAll(const Platform::Array<Platform::String ^, 1U> ^items)
{
    throw ref new Platform::NotImplementedException();
}

运行应用程序时,我总是得到

Microsoft C ++异常:Platform :: InvalidArgumentException ^在内存位置0x02E2DA80。 HRESULT:0x80070057参数不正确。 WinRT信息:参数不正确。

就在我设置的行

TestListView->ItemsSource = ref new MyStringSrc();

我可以遵循一个示例实现吗?

1 个答案:

答案 0 :(得分:0)

与文档相反,您绝不能实施IObservableVector。您必须实施的是Windows::UI::Xaml::Interop::IBindableObservableVector。我发现这在Windows 8.1 XAML示例中引用了文章。 (文档指的是上帝抛弃的Windows 8.1!)不幸的是,我将丢失所有的通用代码。无论如何,不​​应该以通用的方式开始。

像往常一样,微软的文档总是缺乏必要的清晰度。它永远不会告诉我们可以将哪种事物设置为ItemsSource。通过实验,似乎它必须是Windows::UI::Xaml::Interop::IBindable*伞下的东西。