XAML 在UWP上执行拖放操作时,不会触发Enter或over事件

1dkrff03  于 2023-01-18  发布在  其他
关注(0)|答案(1)|浏览(257)

我正在使用UWP for Windows制作一个桌面应用程序。我正在制作拖放功能,但它不起作用。当我拖动文件时,它一直显示“不允许”符号🚫。拖动事件和over事件似乎没有被激发。任何帮助都非常感谢。我没有以管理员身份运行Visual Studio 2022。
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

<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>
fdx2calv

fdx2calv1#

问题是,您的网格没有背景色。将背景色设置为透明就足够了,它将按预期工作:

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

相关问题