为什么Drop事件不会在我的UWP测试应用程序中触发? [英] Why doesn't the Drop event fire in my UWP test app?

查看:99
本文介绍了为什么Drop事件不会在我的UWP测试应用程序中触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在使用UWP中的应用程序。我意识到拖放没有按预期工作,所以我最终创建了一个新的UWP应用程序并尝试拖放那里的东西。



我可以让DragEnter开火,但我无法让Drop事件发挥作用。无论我拖动什么,我总是得到一个停止标志符号,如果我在Drop事件上设置一个断点,它就不会触发。



这是我的代码:



Hi all,

I'm working on an app in UWP. I realized that drag and drop didn't work as expected so I eventually created a new UWP app and try to drag and drop things there.

I can get DragEnter to fire, but I can't get the Drop event to work. Whatever I drag, I always get a "stop sign" symbol, and if I set a breakpoint on the Drop event it doesn't fire.

Here's my code:

<Page

    x:Class="DragDropExample.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:DragDropExample"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d">

    <Grid x:Name="tb" removed="#FFFD3535">
        <TextBlock x:Name="txtDrag" HorizontalAlignment="Left" Margin="10,136,0,0" TextWrapping="Wrap" Text="This is just a test" VerticalAlignment="Top" CanDrag="True" Foreground="#FF487EF1"/>

        <TextBox x:Name="tbDrop" removed="#FF36DA12" Width="100" Height="100" AllowDrop="True" Drop="tbDrop_Drop" DragEnter="tbDrop_DragEnter" />
        <Grid x:Name="gridDrop" HorizontalAlignment="Left" Margin="100, 100, 0, 0" removed="#FFE81DF1" AllowDrop="True" Height="100" Width="100" VerticalAlignment="Bottom"  />
        <Canvas x:Name="canvasDrag" HorizontalAlignment="Left" Height="50" Margin="230,10,0,0" VerticalAlignment="Top" Width="50" Background="#FFF7F71A" CanDrag="True" Drop="canvasDrag_Drop"/>

    </Grid>
</Page>







using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace DragDropExample
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void tbDrop_Drop(object sender, DragEventArgs e)
        {
            tbDrop.Text = "Dropped";
        }

        private void tbDrop_DragEnter(object sender, DragEventArgs e)
        {
            tbDrop.Text = "Entered";
        }

        private void canvasDrag_Drop(object sender, DragEventArgs e)
        {
            tbDrop.Text = "Dropped on canvas";
        }
    }
}





我缺少什么?



我也想知道如何实际拖动对象而不是仅仅复制它们。 (文本块被拖动,但拖动的是副本,原始文本块仍然存在。)



请注意示例代码很傻 - 我只需要有效的东西,不涉及列表视图中的项目或类似的东西。我希望能够拖动带有图像的图像或画布(用于国际象棋游戏)。



谢谢!



Petter



What am I missing?

I also wonder how I can actually drag the objects rather than just making copies of them. (The textblock gets dragged, but what is dragged is a copy, and the original textblock is still there.)

Note that the example code is silly - I just need something that works and that doesn't involve items in a listview or similiar. Preferably, I'd like to be able to drag images or canvases with images in them (for a chess game).

Thanks!

Petter

推荐答案

我不知道UWP。但我知道Drag&放下Windows并认为这可以回答你的问题。



你错过了你还必须处理 DragOver 事件和处理程序必须设置 DragEventArgs类 [ ^ ] 效果属性 [ ^ ]表示允许的内容。



<$ c $返回的效果c> DragEnter 和 DragOver 表示是否可以删除,如果是,则复制或移动数据(通常通过检查 KeyState p roperty)。系统使用此返回值生成拖动光标(例如,如果不可能掉线则停止符号)。



当释放鼠标按钮时,系统调用一次 DragOver 以获得效果。如果可以删除,则调用 Drop 处理程序。否则调用 DragLeave 处理程序(这只能在你需要执行一些清理时实现)。



当释放鼠标按钮时,系统会调用 DragOver 处理程序来设置效果,然后再调用删除处理程序。



离开时调用可选的 DragLeave 处理程序窗口仍然按下鼠标按钮。它可以用于清理。请注意,调用drop处理程序时不会调用此方法,并且可能需要向drop drop添加类似的清理代码。

[/ EDIT]



拖动源可以使用 DropCompleted 事件来检查发生了什么。当数据被移动时,它应该删除它自己的数据。



[更新]

再次注意我不是知道UWP,这些例子可能不起作用。但他们应该展示如何做到这一点。



所以你应该添加 DragOver 处理程序并修改其他处理程序:

I did not know UWP. But I know about Drag & Drop with Windows and think that this answers your questions.

You are missing that you must also handle the DragOver event and that the handlers must set the DragEventArgs Class[^] Effect Property[^] to indicate what is allowed.

The effect returned by DragEnter and DragOver indicates if dropping is possible and if so if data are copied or moved (usually by checking the KeyState property). This return value is used by the system to generate the drag cursor (e.g. stop sign if dropping not possible).

When the mouse button is released, the system calls once DragOver to get the effect. If dropping is possible, the Drop handler is called. Otherwise the DragLeave handler is called (this must only be implemented when you need to perform some cleanup).

When the mouse button is released, the system calls once the DragOver handler to set the effect and then the Drop handler.

The optional DragLeave handler is called when leaving the window with the mouse button still down. It can be used for cleanup. Note that this is not called when the drop handler has been called and it may be necessary to add similar cleanup code to the drop handler.
[/EDIT]

The drag source can use the DropCompleted event to check what has happened. When data has been moved, it should delete it's own data.

[UPDATE]
Note again that I don't know UWP and these examples might not work. But they should show how to do it.

So you should add the DragOver handler and modify the others:
private void tbDrop_DragEnter(object sender, DragEventArgs e)
{
    // Check here if data can be generally dropped:
    // - Control is active and not read only
    // - Drag source provides data of a matching type (e.g. a canvas and not text)
    // The result may be stored in a class member variable so the check
    //  must not be executed again with each DragOver event.
    // When using such a variable, it must be cleared inside the 
    //  Drop and DragLeave handlers.
    this.canDrop = CanDrop(sender, e);
    // Call DragOver to check at the actual position and set the effect.
    DragOver(sender, e);
}

private void tbDrop_DragOver(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.None;
    // Check here if dropping is possible in general and at the actual position.
    if (this.canDrop && CanDropHere(sender, e))
    {
        // See also MSDN example code for the Effect Property
        if ((e.KeyState & 8) == 8 && 
        (e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) 
        {
            // CTL KeyState for copy.
            e.Effect = DragDropEffects.Copy;
        }
        else
        {
            // Move by default.
            e.Effect = DragDropEffects.Move;
        }
    }
}

private void tbDrop_Drop(object sender, DragEventArgs e)
{
    // DragOver() has been called by the system just before this and
    //  the effect has been set.
    if (e.Effect != DragDropEffects.None)
    {
        // Drop data here.
        // ...
    }
    // Cleanup (similar code as in DragLeave()).
    this.canDrop = false;
}

private void tbDrop_DragLeave(object sender, DragEventArgs e)
{
    // Cleanup when leaving.
    this.canDrop = false;
}





在拖动源端使用 DropCompleted 处理程序检测是否已发生丢弃以及是否应移动或复制数据。





On the drag source side use the DropCompleted handler to detect if dropping has been occured and if data should be moved or copied.

private void tbDragSource_DropCompleted(UIElement elem, DropCompletedEventArgs e)
{
    if (e.DropResult == DataPackageOperation.Move)
    {
        // Delete item here
    }
}



[/ UPDATE]


[/UPDATE]


实际上找到了一个解决方案在MSDN的UWP论坛上的人。基本上,当图像设置为位图并且位图的源在DragOver上设置为null时它起作用。





[UWP]在UWP应用程序中拖放对象 [ ^ ]
Actually a solution was found by people in the UWP forum at MSDN. Basically it worked when the image was set to a bitmap and the bitmap's source was set to null upon DragOver.


[UWP]Drag and drop objects within an UWP app[^]


这篇关于为什么Drop事件不会在我的UWP测试应用程序中触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆