使用Caliburn拖放文件MVVM

时间:2016-06-13 15:49:59

标签: c# wpf mvvm caliburn

我正在尝试通过拖放功能上传文件。我成功地完成了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);
    }
}

2 个答案:

答案 0 :(得分:6)

您可以使用EventTriggerBehavior。您将发送" Drop Event"一个命令。可能你需要一个用于事件参数的转换器。以下是使用listview的示例。

 <core:EventTriggerBehavior EventName="SelectionChanged">
      <core:InvokeCommandAction InputConverter="{StaticResource SelectionChangedConverter}" 
      InputConverterParameter="{Binding ElementName=CapturasListView}"
      Command="{Binding OpenCapturaCommand}" />

 </core:EventTriggerBehavior>

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

答案 1 :(得分:4)

查看我从未使用过的用于校准的文档,您似乎错过了事件和操作:

Cal:Message.Attach="[Event Drop] = [Action SaveFile($eventArgs)]"

根据此处的文件备忘单http://caliburnmicro.com/documentation/cheat-sheet

相关问题