简体   繁体   English

在 UWP 上完成拖放时,不会触发 Enter 或 over 事件

[英]Enter or over events are not fired when drag and drop is done on UWP

I'm making a desktop app with UWP for Windows. I was making drag and drop features, but it doesn't work.我正在为 Windows 制作一个带有 UWP 的桌面应用程序。我正在制作拖放功能,但它不起作用。 When I drag a file, it keeps showing "Not allowed" symbol,.当我拖动文件时,它一直显示“不允许”符号。 Drag events and over events seem not to be fired.似乎没有触发拖动事件和越过事件。 Any help is greatly appreciated.任何帮助是极大的赞赏。 I'm not running Visual Studio 2022 as an admin.我没有以管理员身份运行 Visual Studio 2022。

Mainpage.xamc.cs Mainpage.xamc.cs

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;

namespace XML2PDFConverter
{
    /// <summary>
    /// Starting from a blank page.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void BackgroundGrid_DragEnter(object sender, DragEventArgs e)
        {
            e.AcceptedOperation = Windows.ApplicationModel.DataTransfer.DataPackageOperation.Move;
        }

        private async void BackgroundGrid_Drop(object sender, DragEventArgs e)
        {
            if (e.DataView.Contains(Windows.ApplicationModel.DataTransfer.StandardDataFormats.StorageItems))
            {
                var items = await e.DataView.GetStorageItemsAsync();
                var filePaths = items.Select(x => x.Path).ToList();
            }
        }

        private void BackgroundGrid_DragOver(object sender, DragEventArgs e)
        {
            e.AcceptedOperation = Windows.ApplicationModel.DataTransfer.DataPackageOperation.Move;
        }
    }
}

MainPage.xaml主页.xaml

<Page
    x:Class="XMLToPDFConverter.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="XMLToPDFConverter"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid x:Name="BackgroundGrid" AllowDrop="True" DragEnter="BackgroundGrid_DragEnter" Drop="BackgroundGrid_Drop" DragOver="BackgroundGrid_DragOver" />
</Page>

The problem is, that your grid does not have a background color.问题是,您的网格没有背景颜色。 It is enought to set the background color to transparent and it will work as expected:将背景颜色设置为透明就足够了,它将按预期工作:

<Grid x:Name="BackgroundGrid" Background="Transparent" AllowDrop="True" DragEnter="BackgroundGrid_DragEnter" Drop="BackgroundGrid_Drop" DragOver="BackgroundGrid_DragOver" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM