【问题标题】:Asynchronous drag (and drop) from UWP app从 UWP 应用异步拖放(和拖放)
【发布时间】:2019-06-24 18:05:24
【问题描述】:

我正在尝试在我的 UWP 应用中实现拖放机制,以便可以轻松地将我的应用中的项目复制到其他应用中。

问题是,在拖动操作开始的那一刻,我并不总是拥有应该复制的数据。相反,我等待异步操作完成,然后才更新延迟的数据。

这就是我一直在使用的代码,基本上:

private void myGrid_DragStarting(UIElement sender, DragStartingEventArgs args)
{
    var deferral = args.GetDeferral();
    args.Data.RequestedOperation = DataPackageOperation.Copy;

    someAsyncFunction(async (data) => // this callback might take a few seconds to be invoked
    {
            // 
            // ... some code which also invokes another function with "await"
            //

            args.Data.SetStorageItems(new[] { data });
            deferral.Complete();
    });
}

因此,当用户开始将项目从我的应用拖到另一个应用时,它会有一个 ????在鼠标光标旁边签名。此外,比这更糟糕的是,如果用户在我获得延迟拖动的数据之前释放鼠标按钮(同时拖动它),那么什么都不会发生(好像操作静默失败)。

我已经考虑过在我自己的应用程序上向用户提供一些指示,即数据何时准备就绪,以便他们可以释放鼠标按钮。但是有没有更好的方法来防止这两个问题中的任何一个?

【问题讨论】:

    标签: c# windows asynchronous winapi uwp


    【解决方案1】:

    对于您的场景,我假设您有两个 Grid 来实现整个“拖放”操作过程。一个网格是源控制,另一个是目标。

    你说'如果用户在我获得延迟拖动的数据之前释放鼠标按钮(同时拖动它),那么什么都不会发生(就像操作静默失败一样)。'

    要处理这种情况,您可以注册 DropCompleted 事件并告诉用户“DropResult”。

    我做了一个简单的代码示例,让你了解整个过程。

    <Grid>
        <Grid CanDrag="True" Width="100" Height="100" Background="AliceBlue" DragStarting="Grid_DragStarting" DropCompleted="Grid_DropCompleted">
    
        </Grid>
        <Grid AllowDrop="True" Width="200" Height="200" Background="LightBlue" VerticalAlignment="Bottom" Drop="Grid_Drop" DragEnter="Grid_DragEnter" DragOver="Grid_DragOver" DragLeave="Grid_DragLeave">
    
        </Grid>
    </Grid>
    
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }
    
        private async void Grid_DragStarting(UIElement sender, DragStartingEventArgs args)
        {
            Debug.WriteLine("DragStarting");
            var deferral = args.GetDeferral();
            args.Data.RequestedOperation = DataPackageOperation.Copy;
    
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
             {
                 await Task.Delay(10000);
                 StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/StoreLogo.png"));
                 args.Data.SetStorageItems(new[] { file });
                 deferral.Complete();
             });
    
        }
    
        private void Grid_DragLeave(object sender, DragEventArgs e)
        {
            Debug.WriteLine("DragLeave");
        }
    
        private void Grid_DragEnter(object sender, DragEventArgs e)
        {
            Debug.WriteLine("DragEnter");
        }
    
        private void Grid_DragOver(object sender, DragEventArgs e)
        {
            Debug.WriteLine("DragOver");
            if (!e.DataView.Contains(StandardDataFormats.StorageItems))
            {
                e.DragUIOverride.Caption = "There're no StorageItems!";
                e.AcceptedOperation = DataPackageOperation.None;
            }
            else
            {
                e.AcceptedOperation = DataPackageOperation.Copy;
            }
        }
    
        private async void Grid_Drop(object sender, DragEventArgs e)
        {
            Debug.WriteLine("Drop");
            if (e.DataView.Contains(StandardDataFormats.StorageItems))
            {
                var items = await e.DataView.GetStorageItemsAsync();
                if (items.Count > 0)
                {
                    var storageFile = items[0] as StorageFile;
                    Debug.WriteLine(storageFile.DisplayName);
                }
            }
        }
    
        private void Grid_DropCompleted(UIElement sender, DropCompletedEventArgs args)
        {
            Debug.WriteLine("DropCompleted");
            Debug.WriteLine(args.DropResult);
        }
    }
    

    【讨论】:

    • 非常感谢您的详细回答,但我正在寻找的不是在我自己的应用程序中的两个网格之间拖放,而是从我的应用程序到任何其他可以接受二进制的应用程序数据作为拖入(例如,甚至是 Windows 自己的文件资源管理器)
    猜你喜欢
    • 1970-01-01
    • 2017-05-14
    • 2018-12-23
    • 1970-01-01
    • 1970-01-01
    • 2017-12-23
    • 2019-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多