Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drag and Drop Files MVVM with Caliburn

I'm trying to have upload files through drag and drop functionality. I was successfully able to complete the UI work but I'm having trouble accessing the object that was dropped in the backend. I was able to succesfully grab the object if I did behind code but I'm trying to take the MVVM approach.

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);
 }

I've tried EventArgs, I couldn't find the file object property. DragEventArgs is null when the code is tested.

Working solution for behind code

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);
    }
}
like image 457
Master Avatar asked Jun 13 '16 15:06

Master


2 Answers

You can use a EventTriggerBehavior. You will dispatch "Drop Event" to a command. Probably you will need a converter for the event arguments. Here is an example using a listview.

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

 </core:EventTriggerBehavior>

Here some links which explain the same approach:

  • https://blog.xamarin.com/turn-events-into-commands-with-behaviors/
  • https://msdn.microsoft.com/en-us/magazine/dn237302.aspx
like image 181
Alcruz Avatar answered Nov 19 '22 11:11

Alcruz


Checking out the documentation for caliburn, which I've never used, it appears you're missing Event and Action:

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

According to the documentation cheat sheet here http://caliburnmicro.com/documentation/cheat-sheet

like image 5
Patrick Graham Avatar answered Nov 19 '22 13:11

Patrick Graham