【问题标题】:How do I use DomainContext.Load to populate properties of my ViewModel?如何使用 DomainContext.Load 填充 ViewModel 的属性?
【发布时间】:2011-03-05 05:47:33
【问题描述】:

我有一个 Silverlight 页面,它从一个视图模型类中获取数据,该类聚合了来自各种(RIA 服务)域服务的一些数据。

理想情况下,我希望页面能够将其控件数据绑定到视图模型对象的属性,但是由于DomainContext.Load 异步执行查询,因此在页面加载时数据不可用。

我的 Silverlight 页面具有以下 XAML:

<navigation:Page x:Class="Demo.UI.Pages.WidgetPage" 
               // the usual xmlns stuff here...
               xmlns:local="clr-namespace:Demo.UI.Pages" mc:Ignorable="d"
               xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"

                d:DataContext="{d:DesignInstance Type=local:WidgetPageModel, IsDesignTimeCreatable=False}"

               d:DesignWidth="640" d:DesignHeight="480"
               Title="Widget Page">
        <Canvas x:Name="LayoutRoot">
            <ListBox ItemsSource="{Binding RedWidgets}" Width="150" Height="500" />
        </Canvas>
    </navigation:Page>

我的 ViewModel 如下所示:

public class WidgetPageModel
{
    private WidgetDomainContext WidgetContext { get; set; }

    public WidgetPageModel()
    {          
        this.WidgetContext = new WidgetDomainContext();

        WidgetContext.Load(WidgetContext.GetAllWidgetsQuery(), false);            

    }

    public IEnumerable<Widget> RedWidgets
    {
        get
        {
            return this.WidgetContext.Widgets.Where(w => w.Colour == "Red");
        }
    }
}

我认为这种方法从根本上肯定是错误的,因为Load 的异步特性意味着当 ListBox 数据绑定时,小部件列表不一定会被填充。 (我的存储库中的断点显示正在执行要填充到集合的代码,但仅在页面呈现之后。)

有人可以告诉我正确的方法吗?

【问题讨论】:

    标签: silverlight mvvm silverlight-4.0 viewmodel wcf-ria-services


    【解决方案1】:

    谜题中缺少的部分是我需要在属性更改时引发事件。

    我更新的 ViewModel 如下:

    public class WidgetPageModel : INotifyPropertyChanged
    {
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    
        private WidgetDomainContext WidgetContext { get; set; }
    
        public WidgetPageModel()
        {          
            this.WidgetContext = new WidgetDomainContext();
    
            WidgetContext.Load(WidgetContext.GetAllWidgetsQuery(), 
                (result) =>
                {
                    this.RedWidgets = this.WidgetContext.Widgets.Where(w => w.Colour == "Red");
                }, null);            
    
        }
    
        private IEnumerable<Widget> _redWidgets;
        public IEnumerable<Widget> RedWidgets
        {
            get
            {
                return _redWidgets;
            }
            set
            {
                if(value != _redWidgets)
                {
                    _redWidgets = value;
                    RaisePropertyChanged("RedWidgets");
                }
            }
        }
    }
    

    绑定到这些属性的控件会在属性更改事件触发时更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-14
      相关资源
      最近更新 更多