使用 UWP 和 C++/WinRT 应用处理从 ListView 到 ListView 的拖放事件

handle drop event of drag and drop from ListView to ListView with UWP and C++/WinRT app

我正在开发一个简单的 UWP 应用程序,该应用程序是在 Windows 10 下用 C++/WinRT 编写的,其中包含两个 ListView 控件。此应用程序的目标是学习如何从一个 ListView 控件 select 一个项目,将其拖到另一个 ListView 控件,然后放下该项目以便从源中复制它ListView控制到目的地ListView控制。

到目前为止,我发现的所有示例都使用 C#,有一些示例使用 C++/CX 而不是 C++/WinRT 和本机 C++,但是我已经设法掌握了 [=106= 的基本机制] 从源 ListView 中获取项目就像拖放到目标 ListView 上一样。但是,当尝试从放置事件中获取信息以更新目标时 ListView 我遇到了异常。

问题:我需要做哪些改动才能使源ListView控件中的selected文本可以拖放到目标 ListView 控件和文本然后添加到目标 ListView 控件?

Visual Studio 2017 的输出 window 显示以下文本,我将其解释为地址错误异常:

Unhandled exception at 0x0259DC3C (Windows.UI.Xaml.dll) in TouchExperiment_01.exe: 0xC000027B: An application-internal exception has occurred (parameters: 0x05F5E3D8, 0x00000005).

Unhandled exception at 0x74ECE61D (combase.dll) in TouchExperiment_01.exe: 0xC0000602:  A fail fast exception occurred. Exception handlers will not be invoked and the process will be terminated immediately.

Unhandled exception at 0x74F9D7D9 (combase.dll) in TouchExperiment_01.exe: Stack cookie instrumentation code detected a stack-based buffer overrun.

Unhandled exception at 0x74F9D7D9 (combase.dll) in TouchExperiment_01.exe: Stack cookie instrumentation code detected a stack-based buffer overrun.

执行MainPage.cpp源文件中最后一个函数void MainPage::OnListViewDrop()中的以下源代码行时引发异常:

auto x = e.DataView().GetTextAsync();

附加信息 A: 使用调试器,我发现与异常关联的错误消息暗示方法 OnListViewDragItemsStarting() 提供的数据中存在错误。异常错误信息的文本为:

{m_handle={m_value=0x05550330 L"DataPackage does not contain the specified format. Verify its presence using DataPackageView.Contains or DataPackageView.AvailableFormats." } }

我还在 Visual Studio 首次抛出并捕获异常的站点上找到了在 base.h 中停止应用程序(来自 C++/WinRT 模板)的错误文本 0x8004006a : Invalid clipboard format 表示我对拖动开始创建的数据格式和拖动下降试图消耗的数据格式缺乏一致意见。

源代码概览

我修改了 MainPage.xml、MainPage.cpp、MainPage.h 和 pch.h 区域中的标准 C++/WinRT 应用模板。我还为新的 class、DataSource 添加了 class 文件,它使用 std::vector<> 来包含一些测试数据。此内存驻留数据在 App 构造函数中使用一些虚拟数据进行初始化:

App::App()
{
    InitializeComponent();
    DataSource::InitializeDataBase();
    Suspending({ this, &App::OnSuspending });
    //  … other code

首先,我必须在 pch.h 文件中添加一行以提供拖放模板:

#include "winrt/Windows.ApplicationModel.DataTransfer.h"    // ADD_TO:  need to add to allow use of drag and drop in MainPage.cpp

XAML 源文件包含两个 ListView 控件的源代码以及一个 TextBlock 控件的源代码,后者显示 selected 中的项目的完整描述来源 ListView:

 <Page
    x:Class="TouchExperiment_01.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TouchExperiment_01"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" Width="1130" Margin="0,0,0,0">
        <ListView x:Name="myList" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="300" SelectionChanged="OnSelectionChanged"
                  CanDragItems="True" DragItemsStarting="OnListViewDragItemsStarting" BorderBrush="AliceBlue" BorderThickness="3">
        </ListView>
        <TextBlock x:Name="myTextBlock" Height="200" Width="200" Text="this is temp text to replace." TextWrapping="WrapWholeWords" Margin="5"/>
        <ListView x:Name="myList2" HorizontalAlignment="Right" Height="100" VerticalAlignment="Top" Width="300" SelectionChanged="OnSelectionChanged" AllowDrop="True"
                  DragOver="OnListViewDragOver" Drop="OnListViewDrop"  BorderBrush="DarkGreen" BorderThickness="5">
        </ListView>
    </StackPanel>
</Page>

DataSource 的 class 声明很简单。 class定义如下:

#pragma once
class DataSource
{
public:
    DataSource();
    ~DataSource();

    static int InitializeDataBase();

    struct DataSourceType
    {
        std::wstring  name;
        std::wstring  description;
    };

    static std::vector<DataSourceType> myDataBase;

}

;

vector的初始化是在应用程序启动时App构造时完成的是:

int DataSource::InitializeDataBase()
{
    myDataBase.clear();

    for (int i = 0; i < 50; i++) {
        DataSourceType x;
        wchar_t  buffer[256] = { 0 };

        swprintf_s(buffer, 255, L"Name for %d Item", i);
        x.name = buffer;
        swprintf_s(buffer, 255, L"Description %d. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.", i);
        x.description = buffer;
        myDataBase.push_back(x);
    }

    return 0;
}

XAML页面后面的MainPage.cpp源代码是:

#include "pch.h"
#include "MainPage.h"
#include "DataSource.h"

using namespace winrt;
using namespace Windows::UI::Xaml;


namespace winrt::TouchExperiment_01::implementation
{
    MainPage::MainPage()
    {
        InitializeComponent();

        // load up the source ListView with the name field from out
        // in memory database.
        auto p = myList().Items();
        for (auto a : DataSource::myDataBase) {
            p.Append(box_value(a.name));
        }

        // add a single ListViewItem to the destination ListView so that we
        // know where it is.
        p = myList2().Items();
        p.Append(box_value(L"list"));
    }

    int32_t MainPage::MyProperty()
    {
        throw hresult_not_implemented();
    }

    void MainPage::MyProperty(int32_t /* value */)
    {
        throw hresult_not_implemented();
    }

    void MainPage::OnSelectionChanged(Windows::Foundation::IInspectable const & target, Windows::UI::Xaml::RoutedEventArgs const & )
    {
        // the user has selected a different item in the source ListView so we want to display
        // the associated description information for the selected ListViewItem.
        winrt::Windows::UI::Xaml::Controls::ListView p = target.try_as<winrt::Windows::UI::Xaml::Controls::ListView>();
        if (p) {
            int iIndex = p.SelectedIndex();
            myTextBlock().Text(DataSource::myDataBase[iIndex].description);
        }
    }

    void MainPage::OnListViewDragItemsStarting(Windows::Foundation::IInspectable const & target, Windows::UI::Xaml::Controls::DragItemsStartingEventArgs  const & e)
    {
        // provide the data that we have in the ListView which the user has selected
        // to drag to the other ListView. this is the data that will be copied from
        // the source ListView to the destination ListView.
        auto p = target.try_as<winrt::Windows::UI::Xaml::Controls::ListView>();
        if (p) {
            int iIndex = p.SelectedIndex();
            e.Items().SetAt(0, box_value(iIndex));
        }
    }

    void MainPage::OnListViewDragOver(Windows::Foundation::IInspectable const & target, Windows::UI::Xaml::DragEventArgs  const & e)
    {
        // indicate that we are Copy of data from one ListView to another rather than one of the other
        // operations such as Move. This provides the operation type informative user indicator when the
        // user is doing the drag operation.
        e.AcceptedOperation(Windows::ApplicationModel::DataTransfer::DataPackageOperation::Copy);
    }

    void MainPage::OnListViewDrop(Windows::Foundation::IInspectable const & target, Windows::UI::Xaml::DragEventArgs const & e)
    {
        // update the destination ListView with the data that was dragged from the
        // source ListView.
        auto p = target.try_as<winrt::Windows::UI::Xaml::Controls::ListView>();

        auto x = e.DataView().GetTextAsync();  // ** this line triggers exception on drop.
    }

}

在开始拖动之前在源 ListView 中编辑了一个项目 select 的应用程序屏幕截图如下所示。源 ListView 控件在左侧,目标 ListView 控件在右侧。

附录:参考和文档

Microsoft Docs - Windows.ApplicationModel.DataTransfer Namespace

Microsoft Docs - DragItemsStartingEventArgs Class which contains a link to this example project that looks to be using C++/CX Drag and drop sample on GitHub which contains Windows-universal-samples/Samples/XamlDragAndDrop/cpp/Scenario1_ListView.xaml.cpp 有一个有用的例子。

异常的原因是误用了 GetTextAsync() 方法,该方法是一种异步方法,需要使用线程、任务、协程或其他一些并发功能。

我找到了示例源代码Windows-universal-samples/Samples/XamlDragAndDrop/cpp/Scenario1_ListView.xaml.cpp which provided the hint as to what I was doing wrong. See also the article at https://github.com/Microsoft/cppwinrt/blob/master/Docs/Using%20Standard%20C%2B%2B%20types%20with%20C%2B%2B%20WinRT.md

    // We need to take a Deferral as we won't be able to confirm the end
    // of the operation synchronously
    auto def = e->GetDeferral();
    create_task(e->DataView->GetTextAsync()).then([def, this, e](String^ s)
    {
        // Parse the string to add items corresponding to each line
        auto wsText = s->Data();
        while (wsText) {
            auto wsNext = wcschr(wsText, L'\n');
            if (wsNext == nullptr)
            {
                // No more separator
                _selection->Append(ref new String(wsText));
                wsText = wsNext;
            }
            else
            {
                _selection->Append(ref new String(wsText, wsNext - wsText));
                wsText = wsNext + 1;
            }
        }

        e->AcceptedOperation = DataPackageOperation::Copy;
        def->Complete();
    });

为更正问题所做的更改概述

我决定将协程与 GetTextAsync() 一起使用,因为我使用的是 Visual Studio 2017 社区版的最新版本。为此,需要对方法 return 类型进行一些更改,从 void 更改为 winrt::Windows::Foundation::IAsyncAction,同时对解决方案属性进行一些更改,并添加几个包含文件以允许协程更改为编译和 运行 正确。

查看有关几种不同的并发方法的答案和注释以及 Visual Studio 2017 年解决方案属性更改以使用协程和 co_await 运算符

在 MainPage.cpp 的顶部,我添加了以下两个包含指令:

#include <experimental\resumable>
#include <pplawait.h>

我将 OnListViewDragItemsStarting() 方法修改为:

void MainPage::OnListViewDragItemsStarting(Windows::Foundation::IInspectable const & target, Windows::UI::Xaml::Controls::DragItemsStartingEventArgs  const & e)
{
    // provide the data that we have in the ListView which the user has selected
    // to drag to the other ListView. this is the data that will be copied from
    // the source ListView to the destination ListView.
    auto p = target.try_as<winrt::Windows::UI::Xaml::Controls::ListView>();
    unsigned int n = e.Items().Size();
    if (p) {
        int iIndex = p.SelectedIndex();
        e.Data().Properties().Title(hstring (L"my Title"));
        e.Data().SetText(DataSource::myDataBase[iIndex].name.c_str());
        e.Data().RequestedOperation(winrt::Windows::ApplicationModel::DataTransfer::DataPackageOperation::Copy);
    }
}

最后我重写了方法OnListViewDrop()以使用协程如下(还要求class声明中声明的return类型更改为符合新的return类型):

winrt::Windows::Foundation::IAsyncAction MainPage::OnListViewDrop(Windows::Foundation::IInspectable const & target, Windows::UI::Xaml::DragEventArgs const & e)
{
    // update the destination ListView with the data that was dragged from the
    // source ListView. the method GetTextAsync() is an asynch method so
    // we are using coroutines to get the result of the operation.

    // we need to capture the target ListView before doing the co_await
    // in a local variable so that we will know which ListView we are to update.
    auto p = target.try_as<winrt::Windows::UI::Xaml::Controls::ListView>();

    // do the GetTextAsync() and get the result by using coroutines.
    auto ss = co_await e.DataView().GetTextAsync();

    // update the ListView control that originally triggered this handler.
    p.Items().Append(box_value(ss));
}