Tôi đã thử nghiệm với các ứng dụng WP7 ngày hôm nay và đã đạt được một chút tường. Tôi muốn có sự tách biệt giữa giao diện người dùng và mã ứng dụng chính nhưng Ive đã đâm vào tường.Cuộc gọi không đồng bộ trong WP7
Tôi đã thực hiện thành công yêu cầu webclient và nhận kết quả, nhưng vì cuộc gọi không đồng bộ, tôi không biết cách chuyển bản sao lưu này sang cấp UI. Tôi dường như không thể hack trong một chờ đợi để đáp ứng để hoàn thành hoặc bất cứ điều gì. Tôi phải làm điều gì sai.
(đây là thư viện xbox360Voice rằng tôi có để tải về trên trang web của tôi: http://www.jamesstuddart.co.uk/Projects/ASP.Net/Xbox_Feeds/ mà tôi đang porting để WP7 như một thử nghiệm)
đây là đoạn mã backend:
internal const string BaseUrlFormat = "http://www.360voice.com/api/gamertag-profile.asp?tag={0}";
internal static string ResponseXml { get; set; }
internal static WebClient Client = new WebClient();
public static XboxGamer? GetGamer(string gamerTag)
{
var url = string.Format(BaseUrlFormat, gamerTag);
var response = GetResponse(url, null, null);
return SerializeResponse(response);
}
internal static XboxGamer? SerializeResponse(string response)
{
if (string.IsNullOrEmpty(response))
{
return null;
}
var tempGamer = new XboxGamer();
var gamer = (XboxGamer)SerializationMethods.Deserialize(tempGamer, response);
return gamer;
}
internal static string GetResponse(string url, string userName, string password)
{
if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))
{
Client.Credentials = new NetworkCredential(userName, password);
}
try
{
Client.DownloadStringCompleted += ClientDownloadStringCompleted;
Client.DownloadStringAsync(new Uri(url));
return ResponseXml;
}
catch (Exception ex)
{
return null;
}
}
internal static void ClientDownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
ResponseXml = e.Result;
}
}
và điều này là mã giao diện người dùng:
public void GetGamerDetails()
{
var xboxManager = XboxFactory.GetXboxManager("DarkV1p3r");
var xboxGamer = xboxManager.GetGamer();
if (xboxGamer.HasValue)
{
var profile = xboxGamer.Value.Profile[0];
imgAvatar.Source = new BitmapImage(new Uri(profile.ProfilePictureMiniUrl));
txtUserName.Text = profile.GamerTag;
txtGamerScore.Text = int.Parse(profile.GamerScore).ToString("G 0,000");
txtZone.Text = profile.PlayerZone;
}
else
{
txtUserName.Text = "Failed to load data";
}
}
Bây giờ tôi hiểu rằng tôi cần đặt thứ gì đó trong ClientDownloadStringCompleted
nhưng tôi không chắc chắn về điều gì.
"Như bạn có thể thấy lập trình không đồng bộ có thể nhận được thực sự lộn xộn." ... đó là một lý do để sử dụng F #. Với F #, bạn có thể cấu trúc lại mã thành async mà không cần phải thực hiện tất cả các nhào lộn lỗi kiểm soát ngược lại. – Brian
@Brian: Tôi hoàn toàn đồng ý. Tôi chỉ muốn chương trình chức năng có thể được thực hiện dễ tiếp cận hơn, nó có thể là để tâm trí trẻ hơn nhưng không có vấn đề làm thế nào tôi cố gắng tôi không thể có được các khái niệm để dính vào tôi. – AnthonyWJones
bệnh cho ngày mai đi nhờ – JamesStuddart