Node.js API から取得した JSON 応答に従って、名前付きの StackPanel にボタンを動的に追加しようとしています。この JSON 配列を解析して、すべての属性を適切な場所に配置するためのクラスを持つオブジェクト配列にします。次に、foreach で、オブジェクト配列に入力した情報を使用してボタンを作成します。オブジェクト配列には適切な属性が含まれていることに注意してください。foreach で単に a を実行しようとするとMessageBox.Show(item.name)
、すべてのオブジェクトのすべての名前が表示されます。null の値はありません。3 回確認しました。
ボタンの作成方法は次のとおりです。
ItemList[] items = JsonConvert.DeserializeObject<ItemList[]>(jsonString);
Dispatcher.BeginInvoke(new Action(() =>
{
foreach (ItemList item in items)
{
Button ItemName = new Button();
//defining properties of the button, like width, etc..
ItemName.Content = item.title;
ItemName.Name = item.id;
//Will add Click event.
ItemPanel.Children.Add(ItemName);
}
}));
試してみると、 TargetInvocationException が発生します。
[System.Reflection.TargetInvocationException {System.Reflection.TargetInvocationException: 呼び出しのターゲットによって例外がスローされました。---> System.NullReferenceException: オブジェクト参照がオブジェクトのインスタンスに設定されていません。
何故ですか ?神秘。ボタンオブジェクトを作成して、他に何もせずに StackPanel に追加しようとしましたが、エラーは同じです。
私は何を間違っていますか?これを取り除く方法は?私はすでに別のアプリでそのような動的要素を作成しましたが、それは機能していました.
ボタンを作成する関数は、HTTP 要求を使用して API から JSON を取得する関数の最後に呼び出されます。その HTTP 要求関数は OnNavigatedTo メソッドで呼び出されます (ええ、それは奇妙ですが、OnNavigatedTo メソッドからの変数を使用して、ページに到達したときに関数を自動的に呼び出すことがわかった唯一の方法です。私は他のページでそれを行いました。機能しています(ボタンを作成するのではなく、既存のテキストブロックを変更するだけです)。
完全なエラーは次のとおりです。
System.Reflection.TargetInvocationException:
Exception has been thrown by the target of an invocation. --->
System.NullReferenceException: Object reference not set to an instance of an object.
at CubbyHole_Mobile.BrowseFiles.<>c__DisplayClass1.b__0()
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.UnsafeInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Windows.Threading.DispatcherOperation.Invoke()
そして、ここにスタックトレースがあります:
CubbyHole Mobile.DLL!CubbyHole_Mobile.App.Application_UnhandledException(object sender, System.Windows.ApplicationUnhandledExceptionEventArgs e) Ligne 100 C#
System.Windows.ni.dll!MS.Internal.Error.CallApplicationUEHandler(System.Exception e) Inconnu
System.Windows.ni.dll!MS.Internal.Error.IsNonRecoverableUserException(System.Exception ex, out uint xresultValue) Inconnu
System.Windows.ni.dll!System.Windows.Threading.DispatcherOperation.Invoke() Inconnu
System.Windows.ni.dll!System.Windows.Threading.Dispatcher.Dispatch(System.Windows.Threading.DispatcherPriority priority) Inconnu
System.Windows.ni.dll!System.Windows.Threading.Dispatcher.OnInvoke(object context) Inconnu
System.Windows.ni.dll!System.Windows.Hosting.CallbackCookie.Invoke(object[] args) Inconnu
System.Windows.RuntimeHost.ni.dll!System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(System.IntPtr pHandle, int nParamCount, System.Windows.Hosting.NativeMethods.ScriptParam* pParams, System.Windows.Hosting.NativeMethods.ScriptParam* pResult) Inconnu
(申し訳ありませんが、フランスのVSですが、読めるはずです)
また、新しく作成された項目を既存の StackPanel に追加しようとするとエラーがスローされますが、その行で停止しません。UnhandledException メソッドで app.xaml.cs に移動し、Debugger.Break(); で停止します。(これに取り組んでいるときに発生したほぼすべてのエラーと同様)。