C# · 12月 24, 2021

c# – 使用Caliburn拖放文件MVVM

我正在尝试通过拖放功能上传文件.我成功地完成了UI工作,但是我无法访问在后端丢弃的对象.如果我在代码后面做的话,我能够成功地抓住对象,但我正在尝试采用MVVM方法.

AttachmentView.xaml

Cal:Message.Attach=”[Drop] = [SaveFile($eventArgs)]”

Attachmentviewmodel.cs

public virtual async void SaveFile(DragEventArgs e) { var fileStream = new FileStream([File name goes here],FileMode.Open,FileAccess.Read); }

我试过EventArgs,我找不到文件对象属性.测试代码时,DragEventArgs为null.

代码背后的工作解决方案

AttachmentView.xaml.cs

private void ImagePanel_Drop(object sender,DragEventArgs e){ if (e.Data.GetDataPresent(DataFormats.FileDrop)) { // Note that you can have more than one file. string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); // Assuming you have one file that you care about,pass it off to whatever // handling code you have defined. Upload(files); }}解决方法 您可以使用EventTriggerBehavior.您将向系统发送“Drop Event”.可能你需要一个用于事件参数的转换器.以下是使用listview的示例. <core:EventTriggerBehavior EventName=”SelectionChanged”> <core:InvokeCommandAction InputConverter=”{StaticResource SelectionChangedConverter}” InputConverterParameter=”{Binding ElementName=CapturasListView}” Command=”{Binding OpenCapturaCommand}” /> </core:EventTriggerBehavior>

这里有一些链接解释了相同的方法:

> https://blog.xamarin.com/turn-events-into-commands-with-behaviors/
> https://msdn.microsoft.com/en-us/magazine/dn237302.aspx