MVVM Light学习笔记(二):我的第一个MVVM Light程序之拖动文本文件在Silverlight显示...

本文介绍如何在MVVM Light框架中使用RelayCommand,并通过Blend自带的EventToCommand行为将其与UI交互绑定。示例展示了如何创建响应拖放事件的RelayCommand。

本篇主要学习MVVMLight中RelayCommand和如何将Blend中自带的EventToCommand行为应用到RelayCommand中。下面是实例代码。

(1)在MainPage.xaml中写下如下代码。设置Grid的AllowDrop="True",代码如下

  <Grid x:Name="LayoutRoot"
AllowDrop
="True"
Background
="#FF9F9F9F">
<TextBlock FontSize="36"
FontWeight
="Bold"
Foreground
="Purple"
Text
="{Binding DroppedFileContent}"
VerticalAlignment
="Center"
HorizontalAlignment
="Center"
TextWrapping
="Wrap"
TextTrimming
="WordEllipsis" />
</Grid>

(2)在MainViewModel.cs中定义DroppedFileContent属性和RelayCommand属性

 private const string DroppedFileContentPropertyName = "DroppedFileContent";
private string _droppedFile = "Drop file here";
public string DroppedFileContent
{
get
{
return _droppedFile;
}
set
{
if (_droppedFile == value)
{
return;
}
_droppedFile
= value;
RaisePropertyChanged(DroppedFileContentPropertyName);

}
}
public RelayCommand<DragEventArgs> HandleDropCommand
{
get;
private set;
}

(3)在MainViewModel.cs中的构造函数中添加,实现RelayCommand,并定义读取文件方法

  public MainViewModel()
{
if (IsInDesignMode)
{
// Code runs in Blend --> create design time data.
}
else
{
HandleDropCommand
= new RelayCommand<DragEventArgs>(MessageBoxShow);


}

}
public void MessageBoxShow(DragEventArgs e)
{
//MessageBox.Show("Drop");
if (e.Data == null)
{
return;
}

var files
= e.Data.GetData(DataFormats.FileDrop)
as FileInfo[];

// This works with multiple files, but in that
// simple case, let's just handle the 1st one
if (files == null
|| files.Length == 0)
{
DroppedFileContent
= "No files";
return;
}

var file
= files[0];

if (!file.Extension.ToLower().Equals(".txt"))
{
DroppedFileContent
= "Not a TXT file";
return;
}

using (var stream = file.OpenRead())
{
using (var reader = new StreamReader(stream))
{
// Read the first line
var line = reader.ReadLine();
DroppedFileContent
= line;
}
}
}

 (4)让EventToCommand与RelayCommand关联。

          (A)用Blend的方式完成:设置ElementName为Drop,并勾选PassEventArgsToCommand为True.

         r_mvvmlight10.png

点Command后弹出上面的菜单,并点击"Data Binding"菜单相,会弹出如下对话框,选择"Data Context"中的HandleDropCommand:[RelayCommand<DragEventArgs>]

      r_mvvmlight9.png

设置完成后出现如下显示,Command显示黄色

r_mvvmlight12.png

        (B)如果你没有Blend,直接用VS2010完成

              在MainPage.xaml中添加两个命名空间      

  xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.SL4"

             在MainPage.xaml中的Grid中添加如下代码 

<i:Interaction.Triggers>
<i:EventTrigger EventName="Drop">
<cmd:EventToCommand Command="{Binding HandleDropCommand, Mode=OneWay}"
PassEventArgsToCommand
="True" />
</i:EventTrigger>
</i:Interaction.Triggers>

参资资料

http://blog.galasoft.ch/archive/2009/12/17/silverlight-4-dragampdrop-with-eventtocommand.aspx

源代码下载

r_WelcomeScan.jpg
在线演示

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值