2011-04-09 85 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) 
{ } 

我不知道但是在視圖模型是否知道現在太多的看法。