Silverlight模型,视图 - ViewModel麻烦

时间:2011-04-09 21:19:43

标签: c# silverlight mvvm prism viewmodel

您好我正在尝试使用ViewModel中的事件创建一个Silverlight页面,但我不明白如何在页面加载事件上执行此操作(我找不到正确的命令)。 我想绑定它:Loaded =“RadPane_Loaded”to Loaded = {Binding RadPane_Loaded}。

查看:

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 个答案:

答案 0 :(得分:2)

  1. 从Blend SDK添加到项目程序集Microsoft.Expression.Interactions和System.Windows.Interativity(如果使用Prism,则包含这些程序集)。
  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>
    
  4. 因此,当Loaded事件引发时,将调用viewmodel的命令InitializeCommand。

答案 1 :(得分:0)

我发现使用Caliburn库将EventArgs发送到ViewModel的方式非常简单。 (http://caliburnmicro.codeplex.com/)

的xmlns:卡利= “CLR-名称空间:Caliburn.Micro;装配= Caliburn.Micro” 的xmlns:I = “http://schemas.microsoft.com/expression/2010/interactivity”

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

我想知道viewmodel现在是否对该视图了解太多。