よくわかりませんが、デバッグに何時間も費やしたので、これは私が提供できる問題の最良の説明であるはずです。WinRT アプリを作成しています。メイン ページと詳細ページの 2 つのページがあります。Main Page コンストラクター内で、リストボックスを初期化しました。リストボックスのいずれかの要素をクリックすると、ユーザーは詳細ページに移動します。私はこれらすべてを学んでおり、デザインは最善ではないかもしれませんが、これが私がやったことです. MainPage.cs で静的変数を取得し、ユーザーがクリックした要素を指すように設定しました。詳細ページのコンストラクターで、この静的変数を使用して詳細ページ自体のデータコンテキストを設定しました。
私が期待している流れは次のとおりです:-
ほとんどの場合は機能しますが、5 回ごとに 1 回、詳細ページのコンストラクターは、静的変数がまだ初期化されていないことを示す例外をスローします。アプリの起動時に詳細ページのコンストラクターが実行されるのはなぜですか? なぜ時々だけ?コンストラクターではなく、他のメソッドで詳細ページの DataContext を設定する必要がありますか?
コードはやや複雑で、問題のドメインが多すぎるため、投稿を避けています。しかし、私が問題を説明できていない場合は、教えてください。できる限り関連性を保って投稿します。
CODE:- これは、リストボックス内の項目がクリックされたときに呼び出されるメソッドです。ユーザーは詳細ページに移動します。
private void overviewlistbox_Tapped_1(object sender, TappedRoutedEventArgs e)
{
MatchOverview selectedmatch = (sender as ListBox).SelectedItem as MatchOverview;
matchFullDetails = new ObservableCollection<Match>();
foreach (Match m in UpdateController.matchList)
{
if (m.matchDescription == selectedmatch.matchDesc)
{
matchFullDetails.Add(m);
break;
}
}
if(!(matchFullDetails.Count == 0))
this.Frame.Navigate(typeof(Details));
}
これはメインページのコンストラクタです:-
public static ObservableCollection<Match> matchFullDetails;
public MainPage()
{
matchFullDetails = new ObservableCollection<Match>();
this.InitializeComponent();
UpdateController update = new UpdateController(); // Creating new object will update the overview_list of UpdateController(static list).
overviewlistbox.ItemsSource = UpdateController.overview_list;
}
そして、これは例外が発生する詳細ページのコンストラクターのコードです:-
public static ObservableCollection<Match> matchdetails = new ObservableCollection<Match>();
DispatcherTimer dtm_detailspage = null;
public Details()
{
this.InitializeComponent();
matchdetails = MainPage.matchFullDetails; // matchdetails.Last<>() is take because we only need item which is added latest to the collection.
if (matchdetails.Last<Match>().type == "TEST") // Exception is thrown here--Initialization
// error. When I check MainPage.matchFullDetails,
// no data is shown which means its not yet
// initialized. Also the exception is thrown either at
// the start of the app, or when details page is visited. That too once in 4-5 times, not always.
{
matchdetails.Add(matchdetails.First<Match>() as TestMatch);
}
if (matchdetails.Last<Match>().type == "ODI")
{
matchdetails.Add(matchdetails.Last<Match>() as ODIMatch);
}
if (matchdetails.Last<Match>().type == "T20")
{
matchdetails.Add(matchdetails.Last<Match>() as T20Match);
}
}
例外のスクリーンショット:-
バグ発生時のコール スタック データ:-
主要な更新: ついに欠陥を発見しました。[詳細] ページがまだアクティブで、アプリを再起動すると、問題が発生します。この問題の解決策はありますか??