현재 웹 API를 사용해야하는 Windows Phone 8 앱을 개발 중입니다. MVVM 모델로 이동하기로 결정했을 때 웹 API에서 별도의 클래스로 데이터를 다운로드하는 데 필요한 코드를 옮겼습니다. 필요한 모든 기능을 구현 한 후에는 스레드가 데이터 다운로드가 완료 될 때까지 기다리지 않고 (또는 적어도 다음 코드를 실행하는 데 필요한 코드를 기다리지 않음) 스레드가 ViewModel 생성자를 통과하여 내 ListListSelector 컨트롤에 바인딩 할 빈 목록 디버깅 할 때 스레드가 실제로 내 ViewModel 내에서 어떤 시점에서 DownloadCompleted 메서드를 통해 갈 것이라고 알았지 만, 항상 LongListSelector의 ItemsSource가 이미 빈 List로 설정된 후에 발생했습니다. 결국 데이터가 제대로 채워진 목록을 얻었으므로 LongListSelector가 빈 List에 이미 바인딩되어있을 것입니다. 어쨌든 내가하고있는 일을 바꿀 수 있기 때문에 LongListSelector가 실제로 데이터로 채워지기 전에 빈 목록에 바인딩하는 대신 웹에서 가져 오는 실제 데이터에 실제로 바인딩 할 수 있습니까? 스레드가 이동하기 전에 필요한 코드를 모두 기다리는 지 또는 내 목록에 데이터가 제대로 채워지는 시점을 업데이트하는지 여부에 관계없이 최종적으로 코드를 얻는 한 모든 제안을 수락합니다. .MVVM에서 HTTP GET 요청으로 작업하기
미리 감사드립니다.
에서 MainPage.xaml.cs를 :
public void Retrieve()
{
MySets.ItemsSource = new MySetsViewModel(CurrentLogin).BindingList;
}
MySetsView 모델은 다음과 같이 정의된다 :
public class MySetsViewModel
{
User CurrentLogin;
List<Sets> SetsList;
public List<Sets> BindingList { get; set; }
public MySetsViewModel(User CurrentLogin)
{
this.CurrentLogin = CurrentLogin;
Retrieve(CurrentLogin);
}
public async void Retrieve(User CurrentLogin)
{
if (IsolatedStorageSettings.ApplicationSettings.Contains("AccessToken"))
{
CurrentLogin.AccessToken = IsolatedStorageSettings.ApplicationSettings["AccessToken"].ToString();
}
if (IsolatedStorageSettings.ApplicationSettings.Contains("UserID"))
{
CurrentLogin.UserId = IsolatedStorageSettings.ApplicationSettings["UserID"].ToString();
}
try
{
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage();
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + CurrentLogin.AccessToken);
client.DefaultRequestHeaders.Add("Host", "api.quizlet.com");
var result = await client.GetAsync(new Uri("https://api.quizlet.com/2.0/users/" + CurrentLogin.UserId + "/sets"), HttpCompletionOption.ResponseContentRead);
string jsonstring = await result.Content.ReadAsStringAsync();
DownloadCompleted(jsonstring);
}
catch
{
}
}
void DownloadCompleted(string response)
{
try
{
//Deserialize JSON into a List called Sets
this.BindingList = Sets;
}
catch
{
}
}
}