C# 如何手动构建一个有效的DragEventArgs对象以调用Window_Drop()方法?

C# 如何手动构建一个有效的DragEventArgs对象以调用Window_Drop()方法?,c#,wpf,C#,Wpf,我正在用一个旧的WPF应用程序构建一个软件集成。此WPF应用程序具有drop事件的事件处理程序: public void Window_Drop(object sender, DragEventArgs e) { // Spaghetti code } Window_Drop事件获取已删除的文件,对其进行详细说明,并将结果发送到web服务 我的目标是使用当前逻辑将文件放到特定文件夹中 我写了这段代码: 观察者类 public class Watcher { pub

我正在用一个旧的WPF应用程序构建一个软件集成。此WPF应用程序具有drop事件的事件处理程序:

public void Window_Drop(object sender, DragEventArgs e)
{
   // Spaghetti code
}
Window_Drop事件获取已删除的文件,对其进行详细说明,并将结果发送到web服务

我的目标是使用当前逻辑将文件放到特定文件夹中

我写了这段代码:

观察者类

public class Watcher
    {
        public delegate void ActionDelegate(string fileName, string fullPath);
        private ActionDelegate Action;

        private readonly FileSystemWatcher _watcher;
        public Watcher(string path)
        {
            _watcher = new FileSystemWatcher();
            _watcher.Path = path;
            _watcher.Created += _watcher_Created;
            _watcher.EnableRaisingEvents = true;
        }

        public void SubscribeOnCreate(ActionDelegate action)
        {
            Action = action;
        }

        private void _watcher_Created(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine("Creato " + e.Name);
            Action(e.Name, e.FullPath, string.Empty);
        }
    }
}
public partial class App : Application, ISingleInstanceApp
{
    private Watcher _watcher;

    public App()
    {
        _watcher = new Watcher(@"C:\Users\Administrator\Desktop\Monitoraggio");
        _watcher.SubscribeOnCreate(LoadFile);
    }

    private void LoadFile(string fileName, string fullPath)
    {
        var droppedFile = new DragEventArgs(); // How can I build one DragEventArgs object with dropped file?
        var currentWindow = MainWindow.AppWindow; // this is a static reference of current MainWindow
        a.Window_Drop(this, droppedFile);
    }
}
App.cs

public class Watcher
    {
        public delegate void ActionDelegate(string fileName, string fullPath);
        private ActionDelegate Action;

        private readonly FileSystemWatcher _watcher;
        public Watcher(string path)
        {
            _watcher = new FileSystemWatcher();
            _watcher.Path = path;
            _watcher.Created += _watcher_Created;
            _watcher.EnableRaisingEvents = true;
        }

        public void SubscribeOnCreate(ActionDelegate action)
        {
            Action = action;
        }

        private void _watcher_Created(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine("Creato " + e.Name);
            Action(e.Name, e.FullPath, string.Empty);
        }
    }
}
public partial class App : Application, ISingleInstanceApp
{
    private Watcher _watcher;

    public App()
    {
        _watcher = new Watcher(@"C:\Users\Administrator\Desktop\Monitoraggio");
        _watcher.SubscribeOnCreate(LoadFile);
    }

    private void LoadFile(string fileName, string fullPath)
    {
        var droppedFile = new DragEventArgs(); // How can I build one DragEventArgs object with dropped file?
        var currentWindow = MainWindow.AppWindow; // this is a static reference of current MainWindow
        a.Window_Drop(this, droppedFile);
    }
}
如何手动构建一个vali DragEventArgs

每一个建议都是受欢迎的,
谢谢

您不能在不使用反射的情况下创建
DragEventArgs
类的实例,因为它没有定义
public
构造函数

您应该做的是重构
窗口\u Drop
事件处理程序,以便它从框架创建的
DragEventArgs
获取所需的数据,然后将该数据传递给您的方法,您也可以从
LoadFile
方法调用该方法

因此,如果您明白我的意思,您可以调用与事件处理程序调用相同的方法,而不是尝试调用实际的事件处理程序

另一个不推荐的选项是使用反射创建
DragEventArgs
类的实例:


@iMattion:你明白我的答案还是发生了什么?