使用Model方法在ViewModel中填充ObservableCollection

时间:2012-05-12 20:18:07

标签: windows-phone-7 mvvm viewmodel observablecollection

我有一个带有ObservableCollection的ViewModel,SaloonList,我用数据库填充数据。以下代码适用于ViewModel文件中的所有内容:

    public MainViewModel()
    {
        PopulateList();
    }

    private void PopulateList()
    {
        WebClient webClient = new WebClient();
        Uri site = new Uri(_URL);
        UriBuilder uriBuilder = new UriBuilder(site);
        uriBuilder.Path += String.Format("{0}", "all");
        webClient.OpenReadCompleted += SaloonsCompleted;
        webClient.OpenReadAsync(uriBuilder.Uri);
    }

    private void SaloonsCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        if (e.Error != null) { return; }

        DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DBSaloons));
        DBSaloons jsonResponse = (DBSaloons)ser.ReadObject(e.Result);
        this.SaloonList = jsonResponse.Results;
        e.Result.Close();
    }

但是,当我尝试从ViewModel中分离PopulateList方法并使用下面的代码时,即使填充了动作中的列表并且在调试器窗口中打印了取出的沙龙数量,我的SaloonList也不会填充:

   public MainViewModel()
   {      
      Models.Fetcher fetch = new Fetcher();
      fetch.PopulateList(this.SaloonList, onComplete);
   }

   Action<ObservableCollection<Saloon>, OpenReadCompletedEventArgs> onComplete = (ObservableCollection<Saloon> list, OpenReadCompletedEventArgs e) =>
   {
        if (e.Error != null) { Debug.WriteLine("Fetch Error"); return; }
        DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DBSaloons));
        DBSaloons jsonResponse = (DBSaloons)ser.ReadObject(e.Result);
        list = jsonResponse.Results;
        e.Result.Close();
        Debug.WriteLine("Fetched: " + list.Count.ToString() + " saloons");
    };

这是该方法在fetcher类中的样子:

 public void PopulateList(ObservableCollection<Saloon> list, Action<ObservableCollection<Saloon>, OpenReadCompletedEventArgs> onComplete)
    {
        Debug.WriteLine("Fetching...");
        WebClient webClient = new WebClient();
        Uri site = new Uri(_URL);
        UriBuilder uriBuilder = new UriBuilder(site);
        uriBuilder.Path += String.Format("{0}", "all");

        webClient.OpenReadCompleted += (object sender, OpenReadCompletedEventArgs e) => onComplete(list, e);
        webClient.OpenReadAsync(uriBuilder.Uri, list);
    }

我做错了什么?

编辑:

像这样解决了,

public void PopulateList(Action<DBSaloons> callback, Action<Exception> exception)
    {
        Debug.WriteLine("Fetching...");
        WebClient webClient = new WebClient();
        Uri site = new Uri(_URL);
        UriBuilder uriBuilder = new UriBuilder(site);
        uriBuilder.Path += String.Format("{0}", "all");

        webClient.OpenReadCompleted += ((sender, e) =>
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DBSaloons));
            DBSaloons jsonResponse = (DBSaloons)ser.ReadObject(e.Result);
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
               if (e.Error == null) callback(jsonResponse);
                else exception(e.Error);
            });
            e.Result.Close();
        }
        );
        webClient.OpenReadAsync(uriBuilder.Uri);
    }

然后在我的ViewModel中,我得到了这样的沙龙列表:

       fetch.PopulateList( (list) =>
        {
            Debug.WriteLine("Fetched: " + list.Results.Count.ToString() + " saloons");
            this.SaloonList = list.Results;

        }, (exception) => { });

1 个答案:

答案 0 :(得分:0)

list = jsonResponse.Results;

您正在覆盖列表中的引用,因此MainViewModel.SaloonList和列表不再是相同的对象。替换为:

this.SaloonList = jsonResponse.Results;

或者手动将jsonResponse.Results的内容复制到列表中。