0
ObservableCollection、SaloonListを持つViewModelがあります。これはデータベースからデータを取り込みます。以下のコードすべてがViewModelにファイル内にあるときに動作します:ViewModelのObservableCollectionにModelメソッドを設定する
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");
};
このメソッドはフェックラスにどのように見えるかです:
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);
}
私は間違って何をしていますか?
EDIT:
は、このようにそれを解決
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) => { });
アクションでSaloonListにアクセスできません。 "list"で表され、メソッド呼び出しでパラメータとして渡されます。 – Josef
その後、リスト内のjsonResponse.Resultsの内容をループでコピーします。 –
これは必要ではありません。私の質問を解決策で更新しました。 – Josef