简体   繁体   中英

Drag & Drop StorageFolders in UWP Windows 10 Apps

At this moment the examples of Drag and Drop are just for StorageFiles like images, etc. But what happens with Folders.

I have success implemented file and multiple files but when I try with a folder and read its items it returns 0 files, my code:

if (e.DataView.Contains(StandardDataFormats.StorageItems))
{
var items = await e.DataView.GetStorageItemsAsync();

bool hasfolders = false;

foreach (var item in items)
{
    if (item is StorageFile)
    {
        var newFavorite = await FavoriteFromFile(item as StorageFile, groupcategory.Key);
        if (newFavorite != null)
            newFavorites.Add(newFavorite);
    }
    else if(item is StorageFolder)
    {
        var favs = await FavoritesFromFolder(item as StorageFolder, groupcategory.Key);
        hasfolders = true;
    }
}
...

And inside FromFolder:

  private async Task<List<Favorite>> FavoritesFromFolder(StorageFolder folder, Category cat)
    {
        List<Favorite> ret = new List<Favorite>();

        foreach (var item in await folder.GetItemsAsync())
        {

Here returns 0 items, so might be it is not implemented or supported.

Hi I know it's a bit late for answer this question but maybe it can be helpful at this time for you . 1 . check the Allow Drop on the object you want . 2 . go to the events and add DropOver and Drop events for your object . 3 . use this code for your DropOver :

var d = e.GetDeferral();
        try
        {
            var items = await e.DataView.GetStorageItemsAsync();

            if (items != null)
                e.AcceptedOperation = Windows.ApplicationModel.DataTransfer.DataPackageOperation.Copy;

        }
        catch { }
        finally
        { 
            d.Complete();
        }

and then use this code for drop event

var deferral = e.GetDeferral();

        var filesAndFolders = await e.DataView.GetStorageItemsAsync();
        MessageDialog msg = new MessageDialog("");

        foreach (var item in filesAndFolders)
        {
            if (item is IStorageFile)
                msg.Content += "\nFile:" + item.Name;

            if (item is IStorageFolder)
            {
                msg.Content += "\nFolder:" + item.Name;
                var folder = await ((StorageFolder)item).GetFilesAsync();
                foreach (var fold in folder)
                {
                    msg.Content += "\nFolderContent:" + fold.Name;
                }
            }
        }
        await msg.ShowAsync();
        deferral.Complete();

ok now debug your app and drop multiple storage files and folders . you can see contents of the StorageFolder Root (not subfolders you have to expand this code to see subfolders too) and storage files you dropped and the name of storage folders . you can see a sample for Drop Storage Items here : (Named file drop) https://onedrive.live.com/?id=D1BA3C73BE854A97%21370578&cid=D1BA3C73BE854A97&group=0&parId=D1BA3C73BE854A97%21227&authkey=%21AF52vPagWyrz%2DPA&action=locate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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