2011-04-09 88 views
0

嗨,我试图让一个Silverlight页面,在视图模型的事件,但我不知道如何做到这一点的页面加载事件(我无法找到正确的命令)。 我想结合这样的:加载=“RadPane_Loaded”到加载= {结合RadPane_Loaded}。Silverlight的模型,视图 - 视图模型烦恼

查看:

namespace SilverlightTest.Modules.Tree 
{ 
    public partial class OutlookBarView : RadPane 
    { 
     public OutlookBarView(OutlookBarViewModel model) 
     { 
      InitializeComponent(); 
      DataContext = model; 
     } 
    } 
} 

视图模型:

namespace SilverlightTest.Modules.Tree 
{ 
    public class OutlookBarViewModel : DependencyObject 
    { 
     private IEventAggregator _eventAggregator; 
     private IMainPage _shell; 
     private IUnityContainer _container; 

     public OutlookBarViewModel(IEventAggregator eventAggregator, IMainPage shell, IUnityContainer container) 
     { 
      _container = container; 
      _eventAggregator = eventAggregator; 
      _shell = shell; 

     } 


     This is what I would normally do to bind something to a control. 

public ICommand ExampleCommand 
     { 
      get { return (ICommand)GetValue(ExampleCommandProperty); } 
      set { SetValue(ExampleProperty, value); } 
     } 

     /* Here I'd like to bind the page load event but I don't understand how...? */ 


    } 
} 

回答

2
  1. 添加到您的项目组件从混合SDK Microsoft.Expression.Interactions和System.Windows.Interativity(如果你使用棱镜这些组件被包含在内)。
  2. 添加命令视图模型,f.i. InitializeCommand
  3. 而在XAML:

    <RadPane> 
        <i:Interaction.EventTriggers> 
        <i:EventTrigger EventName="Loaded"> 
         <i:InvokeCommandAction Command={Binding InitializeCommand}/> 
        </i:EventTrigger> 
        </i:Interaction.EventTriggers> 
    </RadPane> 
    

所以,你的视图模型的命令InitializeCommand将被加载时引发事件的调用。

+0

嘿弗拉基米尔非常感谢你。这对我来说是非常有用的信息:D这是执行此操作的标准Silverlight方式还是有替代方法?我想知道这是因为这个功能不在标准的Silverlight程序集中。 – BigChief 2011-04-10 22:12:41

+0

是的,如果您想使用MVVM模式,这是标准方式。这些程序集将在Silverlight 5的核心中可用,但现在它们提供了Blend SDK – 2011-04-11 07:04:55

0

我发现有一个很简单的方式EventArgs的发送到视图模型与卡利库。 (http://caliburnmicro.codeplex.com/)

的xmlns:卡利= “CLR-名称空间:Caliburn.Micro;装配= Caliburn.Micro” 的xmlns:ⅰ=“http://schemas.microsoft.com /表达/ 2010 /互动”

  <i:Interaction.Triggers> 
       <i:EventTrigger EventName="Loaded"> 
        <i:InvokeCommandAction Command="{Binding GridViewLoaded}"/> 
       </i:EventTrigger> 
       <i:EventTrigger EventName="SelectionChanged"> 
        <caliburn:ActionMessage MethodName="GridViewSelectionChangedCommandExecute"> 
         <caliburn:Parameter Value="$eventArgs"></caliburn:Parameter> 
        </caliburn:ActionMessage> 
       </i:EventTrigger> 
      </i:Interaction.Triggers> 

视图模型:

public void GridViewSelectionChangedCommandExecute(SelectionChangeEventArgs e) 
{ } 

我不知道但是在视图模型是否知道现在太多的看法。