正确实现listView的项目库

Properly implement ItemSource for ListView

本文关键字:项目库 listView 实现      更新时间:2023-10-16

我尝试实现一个可以将其作为ItemSource作为ListView的类实现,此处是Microsoft文档,即实现IObservableVector<T>,但与往常一样,文档永远无法使用,并且没有C 示例代码。我的主页的XAML内容只是

<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();

我可以遵循样本实现吗?

与文档相反,您不得实现 IObservableVector。您必须实现的是Windows::UI::Xaml::Interop::IBindableObservableVector。我在Windows 8.1 XAML样本中发现了这一点,这是文章中的。(文档是指神遗弃的Windows 8.1!)不幸的是,我将失去所有通用代码。无论如何,不应该从通用开始。

Microsoft的文档像往常一样,总是缺乏必要的清晰度。它永远不会说出可以将哪种类型设置为ItemsSource。通过实验,看来它必须是Windows::UI::Xaml::Interop::IBindable*伞下的东西。