プロジェクトで Async CTP ライブラリを使用する際に問題があります。コードは次のとおりです。
ブックページ
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (this.DataContext is BookViewModel)
{
var bookViewModel = this.DataContext as BookViewModel;
bookViewModel.UpdateReviews();
}
}
BookViewModel
public async void UpdateReviews()
{
Reviews.Clear();
IEnumerable<Review> newReviews = null;
try
{
newReviews = await BooksManager.GetBookReviews(this.Book.Sysno, 10, 0);
}
catch (Exception ex)
{
}
if (newReviews != null)
{
foreach (var review in newReviews)
{
Reviews.Add(review);
}
}
}
書籍マネージャー
public static async Task<IEnumerable<Review>> GetBookReviews(string sysno, uint limit, uint offset)
{
if (sysno == null)
throw new ArgumentNullException("sysno");
if (string.IsNullOrWhiteSpace(sysno))
throw new ArgumentException("sysno");
string url = CreateBookReviewsURL(sysno, limit, offset);
var reviews = await DownloadDataAsync<IEnumerable<Review>>(url);
return reviews;
}
public static async Task<T> DownloadDataAsync<T>(string url)
{
if (url == null)
throw new ArgumentNullException("url");
var newUrl = url.Contains("?") ?
string.Format("{0}d={1}", url, DateTime.Now) :
string.Format("{0}?d={1}", url, DateTime.Now); //to avoid caching
string data = null;
WebRequest webRequest = WebRequest.CreateHttp(newUrl);
using (WebResponse response = await webRequest.GetResponseAsync())
{
if (response.Headers["StatusCode"] == "200")
{
using (var stm = response.GetResponseStream())
{
using (var reader = new StreamReader(stm))
{
data = await reader.ReadToEndAsync();
}
}
}
}
var books = await ParseDataAsync<T>(data);
return books;
}
NullReferenceException をスローします
System.Reflection.RuntimeMethodInfo.InternalInvoke (
RuntimeMethodInfo rtmi 、オブジェクト obj、BindingFlags invokeAttr、 System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder バインダー, Object[] パラメータ, CultureInfo カルチャ, StackCrawlMark& stackMark) System.Reflection.MethodBase.Invoke (オブジェクト obj、オブジェクト [] パラメーター) で
System.Delegate.DynamicInvokeOne(Object[] args) で System.MulticastDelegate.DynamicInvokeImpl(Object[] args) で System.Delegate.DynamicInvoke(Object[] args) で System.Windows.Threading.DispatcherOperation.Invoke() で System.Windows.Threading.DispatcherOperation.Invoke() で.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority 優先順位) で System.Windows.Threading.Dispatcher.OnInvoke(オブジェクト コンテキスト) で System.Windows.Hosting.CallbackCookie.Invoke(Object[] args) で System.Windows.Hosting.DelegateWrapper .InternalInvoke(Object[] args)
at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)
App.xaml.cs では、BooksManager.GetBookReviews() の呼び出しが正しく機能します。