Note that there are some explanatory texts on larger screens.

plurals
  1. POFlow of initializing objects in XAML?
    primarykey
    data
    text
    <p>I'm not sure but from my hours of debugging, this should be the best description of my problem I can give. I'm creating a WinRT app, there are two pages- Main Page and Details Page. Inside Main Page constructor, I have initialized a listbox. On click of any of the element of listbox, user is taken to the Details page. I'm just learning all this and design may not be best but here is what I did. I took a static variable in MainPage.cs, and set it to point to the element which is clicked by the user. Now in the constructor of the Details page, I used this static variable to set the datacontext of Details Page itself.</p> <p>What flow I'm expecting is:- <li>MainPage is created first. Listbox is setup.</li> <li>User will click on any of the element of listbox. Itemclick event handler runs. It will set the static variable (of Mainpage.cs) to hold the infomation which item is clicked and navigate user to the Details page.</li> <li>In Details page constructor, I have set the datacontext to point to some information based on the value of static variable mentioned in the previous step.</li></p> <p>It works for most of the times, but once in like every 5 times, The Details page constructor throws an exception stating the static variable is not initialized yet. Why is Details page's constructor running when I'm starting the app? and why only sometimes? Do I need to set DataContext of Details Page in some other method instead of constructor?</p> <p>The code is somewhat complex and too much in terms of domain of the problem so I'm avoiding posting it. But if I'm failing to explain the problem please tell, I'll post it keeping it as related as I can.</p> <p>CODE:- This is the method called when an item in listbox is clicked--will take user to the Details page.</p> <pre><code>private void overviewlistbox_Tapped_1(object sender, TappedRoutedEventArgs e) { MatchOverview selectedmatch = (sender as ListBox).SelectedItem as MatchOverview; matchFullDetails = new ObservableCollection&lt;Match&gt;(); foreach (Match m in UpdateController.matchList) { if (m.matchDescription == selectedmatch.matchDesc) { matchFullDetails.Add(m); break; } } if(!(matchFullDetails.Count == 0)) this.Frame.Navigate(typeof(Details)); } </code></pre> <p>This is the constructor for Main Page:-</p> <pre><code>public static ObservableCollection&lt;Match&gt; matchFullDetails; public MainPage() { matchFullDetails = new ObservableCollection&lt;Match&gt;(); this.InitializeComponent(); UpdateController update = new UpdateController(); // Creating new object will update the overview_list of UpdateController(static list). overviewlistbox.ItemsSource = UpdateController.overview_list; } </code></pre> <p>And this is the code for constructor of details page, where the exception occurs:-</p> <pre><code>public static ObservableCollection&lt;Match&gt; matchdetails = new ObservableCollection&lt;Match&gt;(); DispatcherTimer dtm_detailspage = null; public Details() { this.InitializeComponent(); matchdetails = MainPage.matchFullDetails; // matchdetails.Last&lt;&gt;() is take because we only need item which is added latest to the collection. if (matchdetails.Last&lt;Match&gt;().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&lt;Match&gt;() as TestMatch); } if (matchdetails.Last&lt;Match&gt;().type == "ODI") { matchdetails.Add(matchdetails.Last&lt;Match&gt;() as ODIMatch); } if (matchdetails.Last&lt;Match&gt;().type == "T20") { matchdetails.Add(matchdetails.Last&lt;Match&gt;() as T20Match); } } </code></pre> <p>Exception Screenshot:- <img src="https://i.stack.imgur.com/QHe0n.jpg" alt="The exception thrown at run-time"></p> <p>Call Stack data on bug encounter:- <li>[Cricket Expert.exe!Cricket_Expert.Details.Details() Line 33 + 0x5 bytes</li> <li>[External Code]</li> <li>Cricket Expert.exe!Cricket_Expert.Common.SuspensionManager.RestoreFrameNavigationState(Windows.UI.Xaml.Controls.Frame frame) Line 236 + 0x5 bytes</li> <li>Cricket Expert.exe!Cricket_Expert.Common.SuspensionManager.RestoreAsyn() Line 124 0x8 bytes</li> <li>Cricket Expert.exe!Cricket_Expert.App.OnLaunched(Windows.ApplicationModel.Activation.LaunchActivatedEventArgs args) Line 74 + 0x5 bytes</li> <li>[External Code]</li></p> <p><strong>MAJOR UPDATE: I finally found the flaw. If the Details page is still active, and the app is restarted, the problem occurs. Is there a solution to this problem??</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload