加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用Caliburn拖放文件MVVM

发布时间:2020-12-15 04:19:12 所属栏目:百科 来源:网络整理
导读:我正在尝试通过拖放功能上传文件.我成功地完成了UI工作,但是我无法访问在后端丢弃的对象.如果我在代码后面做的话,我能够成功地抓住对象,但我正在尝试采用MVVM方法. AttachmentView.xaml Cal:Message.Attach="[Drop] = [SaveFile($eventArgs)]" AttachmentVie
我正在尝试通过拖放功能上传文件.我成功地完成了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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读