Windows phone 7 用模型方法填充ViewModel中的ObservableCollection

Windows phone 7 用模型方法填充ViewModel中的ObservableCollection,windows-phone-7,mvvm,viewmodel,observablecollection,Windows Phone 7,Mvvm,Viewmodel,Observablecollection,我有一个ViewModel,它有一个ObservableCollection,SaloonList,我用数据库中的数据填充它。当所有内容都在ViewModel文件中时,以下代码起作用: public MainViewModel() { PopulateList(); } private void PopulateList() { WebClient webClient = new WebClient();

我有一个ViewModel,它有一个ObservableCollection,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不会填充,即使操作中的列表已填充,并且调试器窗口中已打印获取的Saloon数:

   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");
    };
您正在覆盖列表上的引用,因此MainViewModel.SaloonList和list不再是相同的对象。替换为:

this.SaloonList = jsonResponse.Results;

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

SaloonList在操作中不可访问。它通过“list”表示,并在方法调用中作为参数传递。然后用循环将jsonResponse.Results的内容复制到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);
    }
       fetch.PopulateList( (list) =>
        {
            Debug.WriteLine("Fetched: " + list.Results.Count.ToString() + " saloons");
            this.SaloonList = list.Results;

        }, (exception) => { });
list = jsonResponse.Results;
this.SaloonList = jsonResponse.Results;