[UWP]拖放在Windows 10 Mobile上不起作用 [英] [UWP] Drag and Drop does not work on Windows 10 Mobile

查看:66
本文介绍了[UWP]拖放在Windows 10 Mobile上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。


我使用C#和XAML开发UWP应用程序。我尝试实现拖放功能。


这是一个简单的应用程序来演示我如何尝试这样做。该应用程序有两个边框 - 一个边框被拖动,第二个边框用于拖放。我也写了关于要输出的事件的信息。


版本1(在拖动的项目上使用CanDrag = true)

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

< Grid Background =" {ThemeResource ApplicationPageBackgroundThemeBrush}">
< Grid.RowDefinitions>
< RowDefinition />
< RowDefinition />
< /Grid.RowDefinitions>
< Border x:Name =" draggedItem"
Grid.Row =" 0"
高度="100"
Width =" 150"
Horizo​​ntalAlignment =" Center"
VerticalAlignment =" Center"
背景="红色"
CanDrag =" True"
DragStarting =" draggedItem_DragStarting"
DropCompleted =" draggedItem_DropCompleted">
< TextBlock Text ="拖动此项"
Horizo​​ntalAlignment =" Center"
VerticalAlignment =" Center" />
< / Border>
< Border x:Name =" dropArea"
Grid.Row =" 1"
高度="100"
Width =" 150"
Horizo​​ntalAlignment =" Center"
VerticalAlignment =" Center"
背景="蓝色"
AllowDrop =" True"
DragEnter =" dropArea_DragEnter"
Drop =" dropArea_Drop">
< TextBlock Text =" Drop here"
Horizo​​ntalAlignment =" Center"
VerticalAlignment =" Center" />
< / Border>
< / Grid>
< / Page>


使用System.Diagnostics; 
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;

//空白页项目模板记录在https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

名称空间UWP_DragDrop
{
///< summary>
///一个空页面,可以单独使用,也可以导航到Frame中。
///< / summary>
public sealed partial class MainPage:Page
{
public MainPage()
{
this.InitializeComponent();
}

private void draggedItem_DragStarting(UIElement sender,DragStartingEventArgs args)
{
Debug.WriteLine(" draggedItem_DragStarting");
}

private void draggedItem_DropCompleted(UIElement sender,DropCompletedEventArgs args)
{
Debug.WriteLine(" draggedItem_DropCompleted");
}

private void dropArea_DragEnter(object sender,DragEventArgs e)
{
Debug.WriteLine(" dropArea_DragEnter");
e.AcceptedOperation = Windows.ApplicationModel.DataTransfer.DataPackageOperation.Copy;
}

private void dropArea_Drop(object sender,DragEventArgs e)
{
Debug.WriteLine(" dropArea_Drop");
}
}
}

1)当我在Visual Studio for LocalMachine和Simulator中运行它时,它可以正常工作(但仅适用于鼠标输入)。在输出中,我有以下内容:


draggedItem_DragStarting

dropArea_DragEnter

dropArea_Drop

draggedItem_DropCompleted


2)当我尝试在模拟器(触摸模式)中运行它时 - 我无法拖动项目。没有一个事件被触发(输出为空)


3)当我尝试在Windows 10 Mobile模拟器中运行它时,它不起作用。我在输出中看到的是:


draggedItem_DragStarting

draggedItem_DropCompleted


我移动元素时 - DropCompleted事件触发。


版本2(使用StartDragAsync)


我为拖动的项目移除了CanDrag = true,并开始拖动操作StartDragAsync。

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

< Grid Background =" {ThemeResource ApplicationPageBackgroundThemeBrush}">
< Grid.RowDefinitions>
< RowDefinition />
< RowDefinition />
< /Grid.RowDefinitions>
< Border x:Name =" draggedItem"
Grid.Row =" 0"
高度="100"
Width =" 150"
Horizo​​ntalAlignment =" Center"
VerticalAlignment =" Center"
背景="红色"
PointerMoved =" draggedItem_PointerMoved"
DragStarting =" draggedItem_DragStarting">
< TextBlock Text ="拖动此项"
Horizo​​ntalAlignment =" Center"
VerticalAlignment =" Center" />
< / Border>
< Border x:Name =" dropArea"
Grid.Row =" 1"
高度="100"
Width =" 150"
Horizo​​ntalAlignment =" Center"
VerticalAlignment =" Center"
背景="蓝色"
AllowDrop =" True"
DragEnter =" dropArea_DragEnter"
Drop =" dropArea_Drop">
< TextBlock Text =" Drop here"
Horizo​​ntalAlignment =" Center"
VerticalAlignment =" Center" />
< / Border>
< / Grid>
< / Page>


使用System.Diagnostics; 
使用Windows.ApplicationModel.DataTransfer;
使用Windows.Foundation;
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
使用Windows.UI.Xaml.Input;

//空白页项目模板记录在https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

名称空间UWP_DragDrop
{
///< summary>
///一个空页面,可以单独使用,也可以导航到Frame中。
///< / summary>
public sealed partial class MainPage:Page
{
IAsyncOperation< DataPackageOperation> _dragOperation;

public MainPage()
{
this.InitializeComponent();
}

private void draggedItem_PointerMoved(object sender,PointerRoutedEventArgs e)
{
if(e.Pointer.IsInContact&&(_dragOperation == null))
{
Debug.WriteLine(" draggedItem_StartDragAsync");
_dragOperation = draggedItem.StartDragAsync(e.GetCurrentPoint(draggedItem));
_dragOperation.Completed = DragCompleted;
}
}

private void DragCompleted(IAsyncOperation< DataPackageOperation> asyncInfo,AsyncStatus asyncStatus)
{
_dragOperation = null;
Debug.WriteLine(" draggedItem_DragCompleted");
}

private void draggedItem_DragStarting(UIElement sender,DragStartingEventArgs args)
{
Debug.WriteLine(" draggedItem_DragStarting");
}

private void dropArea_DragEnter(object sender,DragEventArgs e)
{
Debug.WriteLine(" dropArea_DragEnter");
e.AcceptedOperation = DataPackageOperation.Copy;
}

private void dropArea_Drop(object sender,DragEventArgs e)
{
Debug.WriteLine(" dropArea_Drop");
}
}
}

1)桌面和模拟器(鼠标模式)有效。


2)模拟器(触摸模式)现在也可以。


3)Windows 10移动模拟器不起作用。 DropCoartter再次在DragStarting之后立即触发。


如何在Windows 10移动设备上进行拖放操作?为什么版本1在场景2和3中不起作用,版本2在场景3中不起作用?



解决方案

嗨Dmitry Salenko,


我很遗憾地说这是一个已知的问题,拖放不能在Windows
10 Phone Emulator中工作。你可以参考这个链接:
拖放不在WP 10模拟器中工作


最好的问候,


Roy


Hello.

I develop UWP app with C# and XAML. I try to implement drag&drop functionality.

Here is a simple app to demonstrate how I try to do it. The app has two borders - one border is dragged, and second one is for drop. Also I write informaton about events to output.

Version 1 (using CanDrag=true on a dragged item)

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

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Border x:Name="draggedItem"
                Grid.Row="0"
                Height="100"
                Width="150"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                Background="Red"
                CanDrag="True"
                DragStarting="draggedItem_DragStarting"
                DropCompleted="draggedItem_DropCompleted">
            <TextBlock Text="Drag this item"
                       HorizontalAlignment="Center"
                       VerticalAlignment="Center" />
        </Border>
        <Border x:Name="dropArea"
                Grid.Row="1"
                Height="100"
                Width="150"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                Background="Blue"
                AllowDrop="True"
                DragEnter="dropArea_DragEnter"
                Drop="dropArea_Drop">
            <TextBlock Text="Drop here"
                       HorizontalAlignment="Center"
                       VerticalAlignment="Center" />
        </Border>
    </Grid>
</Page>

using System.Diagnostics;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

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

namespace UWP_DragDrop
{
    /// <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 draggedItem_DragStarting(UIElement sender, DragStartingEventArgs args)
        {
            Debug.WriteLine("draggedItem_DragStarting");
        }

        private void draggedItem_DropCompleted(UIElement sender, DropCompletedEventArgs args)
        {
            Debug.WriteLine("draggedItem_DropCompleted");
        }

        private void dropArea_DragEnter(object sender, DragEventArgs e)
        {
            Debug.WriteLine("dropArea_DragEnter");
            e.AcceptedOperation = Windows.ApplicationModel.DataTransfer.DataPackageOperation.Copy;
        }

        private void dropArea_Drop(object sender, DragEventArgs e)
        {
            Debug.WriteLine("dropArea_Drop");
        }
    }
}

1) It works correctly when I run it in Visual Studio for LocalMachine and for Simulator (but only for mouse input). In output I have the following:

draggedItem_DragStarting
dropArea_DragEnter
dropArea_Drop
draggedItem_DropCompleted

2) When I try to run it in Simulator (touch mode) - I can't drag item. No one event is fired (output is empty)

3) When I try to run it in Windows 10 Mobile emulator, it does not work. What I see in output is:

draggedItem_DragStarting
draggedItem_DropCompleted

As soon as I move the element - DropCompleted event fires.

Version 2 (using StartDragAsync)

I removed CanDrag=true for dragged item, and start drag operation by StartDragAsync.

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

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Border x:Name="draggedItem"
                Grid.Row="0"
                Height="100"
                Width="150"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                Background="Red"
                PointerMoved="draggedItem_PointerMoved"
                DragStarting="draggedItem_DragStarting">
            <TextBlock Text="Drag this item"
                       HorizontalAlignment="Center"
                       VerticalAlignment="Center" />
        </Border>
        <Border x:Name="dropArea"
                Grid.Row="1"
                Height="100"
                Width="150"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                Background="Blue"
                AllowDrop="True"
                DragEnter="dropArea_DragEnter"
                Drop="dropArea_Drop">
            <TextBlock Text="Drop here"
                       HorizontalAlignment="Center"
                       VerticalAlignment="Center" />
        </Border>
    </Grid>
</Page>

using System.Diagnostics;
using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;

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

namespace UWP_DragDrop
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        IAsyncOperation<DataPackageOperation> _dragOperation;

        public MainPage()
        {
            this.InitializeComponent();
        }

        private void draggedItem_PointerMoved(object sender, PointerRoutedEventArgs e)
        {
            if (e.Pointer.IsInContact && (_dragOperation == null))
            {
                Debug.WriteLine("draggedItem_StartDragAsync");
                _dragOperation = draggedItem.StartDragAsync(e.GetCurrentPoint(draggedItem));
                _dragOperation.Completed = DragCompleted;
            }
        }

        private void DragCompleted(IAsyncOperation<DataPackageOperation> asyncInfo, AsyncStatus asyncStatus)
        {
            _dragOperation = null;
            Debug.WriteLine("draggedItem_DragCompleted");
        }

        private void draggedItem_DragStarting(UIElement sender, DragStartingEventArgs args)
        {
            Debug.WriteLine("draggedItem_DragStarting");
        }

        private void dropArea_DragEnter(object sender, DragEventArgs e)
        {
            Debug.WriteLine("dropArea_DragEnter");
            e.AcceptedOperation = DataPackageOperation.Copy;
        }

        private void dropArea_Drop(object sender, DragEventArgs e)
        {
            Debug.WriteLine("dropArea_Drop");
        }
    }
}

1) Desktop and Simulator (mouse mode) works.

2) Simulator (touch mode) now works too.

3) Windows 10 mobile emulator does not work. DropCompleter fires right after DragStarting again.

How can I make drag&drop work on Windows 10 mobile? Why version 1 does not work in scenario 2 and 3, and version 2 does not work in scenario 3?

解决方案

Hi Dmitry Salenko,

I’m sorry to say this is a known issue that Drag and Drop not working in Windows 10 Phone Emulator. You could refer this link: Drag and Drop not working in WP 10 emulator.

Best regards,

Roy


这篇关于[UWP]拖放在Windows 10 Mobile上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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