Search code examples
c#.netwpfdrag-and-dropmsix

Drag and drop from explorer not working in WPF app packaged with MSIX


The setup:

I have a DataGrid with AllowDrop="True", and Drop="MyDropEvent". Let's ignore the handling of the drop event, because it boils down to this:

If I attempt to drag/drop files from the Windows explorer onto my DataGrid while running my application through VS or after building to a .exe, it works: when dragging over the DataGrid, the cursor is correctly updated, and upon dropping, MyDropEvent is called.

However, if I package the same WPF application with MSIX, install the resulting msixbundle and run the installed app, dragging files from the explorer into my app just gives me the standard, red crossed out circle, and no DragOver or Drop event is ever called.

While I've been using DataGrid as an example, the same goes for other elements. It seems that drag/drop from the explorer is completely blocked somewhere.

I suspect this is some kind of permissions/security issue, though I don't understand what exactly is going on or how to fix it.

I've checked the integrity levels for both my app and explorer.exe through Process Explorer, and both are at medium where I would expect them.

This is what my .appxmanifest looks like:


<Package
  xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
  xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
  xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
  xmlns:uap3="http://schemas.microsoft.com/appx/manifest/uap/windows10/3"
  IgnorableNamespaces="uap rescap uap3">

  <Identity
    ...

  <Properties>
    ...
  </Properties>

  <Dependencies>
    <TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.0.0" MaxVersionTested="10.0.0.0" />
    <TargetDeviceFamily Name="Windows.Desktop" MinVersion="10.0.14393.0" MaxVersionTested="10.0.14393.0" />
  </Dependencies>

  <Resources>
    <Resource Language="x-generate"/>
  </Resources>

  <Applications>
    <Application Id="App"
      Executable="$targetnametoken$.exe"
      EntryPoint="$targetentrypoint$">
      <uap:VisualElements
        DisplayName=...
        Description=...
        BackgroundColor="transparent"
        Square150x150Logo="Images\Square150x150Logo.png"
        Square44x44Logo="Images\Square44x44Logo.png">
        <uap:DefaultTile Wide310x150Logo="Images\Wide310x150Logo.png"  Square71x71Logo="Images\SmallTile.png" Square310x310Logo="Images\LargeTile.png"/>
        <uap:SplashScreen Image="Images\SplashScreen.png" />
      </uap:VisualElements>
    </Application>
  </Applications>

  <Capabilities>    
    <rescap:Capability Name="runFullTrust" />
    <rescap:Capability Name="broadFileSystemAccess" />
    <uap:Capability Name="removableStorage"/>
    <uap:Capability Name="musicLibrary"/>
    <uap3:Capability Name="backgroundMediaPlayback"/>
  </Capabilities>
</Package>

broadFileSystemAccess was my latest attempt at fixing this, but to no success.

What am I missing? I'm curious what exactly is causing this, and whether there is a fix that ideally doesn't involve completely restructuring how I build my app. I do need an MSIX package to upload to the MS store, unfortunately.

Cheers

Edit:

I'm still having no luck. As soon as the msixbundle is installed, it just refuses to handle any and all file drag/drop operations.

I tried ticking every last "capability" in the appxmanifest editor, tried running as admin/non admin, but no success.

I also tried subscribing to Drop, DragOver, DragEnter, DragLeave, PReviewDragLeave, PreviewDragOver, PreviewDrop, PreviewDragEnter, QueryContinueDrag, PreviewQueryContinueDrag, GiveFeedback and PreviewGiveFeedback of my main Window, but not a single one of them ever fires in the installed app (works fine running through VS or when building a .exe)

Interestingly, gong-wpf-dragdrop also fails in my packaged application. It just crashes whenever I try to drag anything it's supposed to handle. I can't even think of a way to get at the exception: try/catching inside the DropHandler is already too late, the exception avoids all my handlers for unhandled events and VS refuses to let me attach the debugger to the application as soon as it's installed.

I have absolutely no problems with accessing arbitrary files on the system though... the file explorer works flawlessly, I can read arbitrary files etc.


Solution

  • Okay, I finally solved it: this seems to be related to the TargetFramework I had defined in my .csproj:

    1. This version doesn't allow drag/drop in my MSIX app (I have the same issue in a completely new WPF project):

    <TargetFramework>net6.0-windows10.0.22621.0</TargetFramework>
    

    This version works without issues:

    <TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
    

    The former being a Windows 11 .net, which I guess somehow doesn't allow drag/drop for some reason (at least for me.)

    2. While this worked to fix the issue on my empty test project, it didn't suffice for my real project.

    I noticed that I had been building the package as Neutral/AnyCPU. However, I eventually realized that the issue disappeared if I built as x64 or even both: x86_x64. Building x86 on its own, or AnyCPU consistently disabled drag and drop again.

    I bet there's a great explanation for why this happens, but I'm just glad I got it to work for x64.