如何为音频文件创建拖放界面

时间:2019-06-12 11:54:34

标签: c# wpf xaml

我正在尝试创建一个界面,允许用户拖动mp3或mp4文件并获取文件路径。

我创建了一个矩形来表示放置区域,但是我在为视图模型的代码苦苦挣扎

<Rectangle x:Name="MyRectangle"
               Width="200"
               Height="200"
               Fill="Gray" 
               Drop="MyRectangle_Drop"
               AllowDrop="True"/>

2 个答案:

答案 0 :(得分:3)

如果将MVVM结构与Dependency Injection一起使用,请创建一个公共类。这是我所做的一个例子。

using System.Windows;
using System.Windows.Input;

namespace Test.Common
{
    public class Behaviors
    {
        public static readonly DependencyProperty DropFileCommandProperty =
            DependencyProperty.RegisterAttached("DropFileCommand", typeof(ICommand),
            typeof(Behaviors), new FrameworkPropertyMetadata(
            new PropertyChangedCallback(DropFileCommandChanged)));

        private static void DropFileCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            FrameworkElement element = (FrameworkElement)d;

            element.Drop += Element_DropFile;
        }

        private static void Element_DropFile(object sender, DragEventArgs e)
        {
            FrameworkElement element = (FrameworkElement)sender;

            ICommand command = GeDropFileCommand(element);

            command.Execute(e);
        }


        public static void SetDropFileCommand(UIElement element, ICommand value)
        {
            element.SetValue(DropFileCommandProperty, value);
        }

        public static ICommand GeDropFileCommand(UIElement element)
        {
            return (ICommand)element.GetValue(DropFileCommandProperty);
        }
    }
}

您现在可以在视图中像这样引用您的课程。

<Window x:Class="Test.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:common="clr-namespace:Test.Common"     
        xmlns:prism="http://prismlibrary.com/"        
        prism:ViewModelLocator.AutoWireViewModel="True"
        AllowDrop="True"
        common:Behaviors.DropFileCommand="{Binding DropFile}"
        Title="{Binding Title}">
    <Grid>
    </Grid>
</Window>

现在,在ViewModel上,您可以执行以下操作。

using Prism.Commands;
using Prism.Mvvm;
using System.Windows;

namespace Test.Views
{
    public class MainWindowViewModel : BindableBase
    {        
        private string _title = "TestDrop";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        public MainWindowViewModel()
        {            
            DropFile = new DelegateCommand<DragEventArgs>(dropFile);
        }

        public DelegateCommand<DragEventArgs> DropFile { get; }

        private void dropFile(DragEventArgs obj)
        {
            var files = obj.Data.GetData(DataFormats.FileDrop, true) as string[];

            //implement rest of code here
        }
    }
}

答案 1 :(得分:0)

在您的MyRectangle_Drop EventHandler中,尝试使用以下语句来获取已删除文件的目录。

var directories = (string[])e.Data.GetData(DataFormats.FileDrop);