在XAML中初始化对象的流程?
我不确定,但从我调试的时间来看,这应该是对我的问题的最好描述。 我正在创建一个WinRT应用程序,有两个页面-主页和详细信息页面。在主页构造函数中,我初始化了一个列表框。单击listbox的任何元素,用户将进入详细信息页面。 我只是在学习这一切,设计可能不是最好的,但这就是我所做的。 我在MainPage.cs中获取了一个静态变量,并将其设置为指向用户单击的元素。现在在Details页面的构造函数中,我使用这个静态变量来设置Details页面本身的datacontext 我期待的是:-在XAML中初始化对象的流程?,xaml,windows-runtime,winrt-xaml,Xaml,Windows Runtime,Winrt Xaml,我不确定,但从我调试的时间来看,这应该是对我的问题的最好描述。 我正在创建一个WinRT应用程序,有两个页面-主页和详细信息页面。在主页构造函数中,我初始化了一个列表框。单击listbox的任何元素,用户将进入详细信息页面。 我只是在学习这一切,设计可能不是最好的,但这就是我所做的。 我在MainPage.cs中获取了一个静态变量,并将其设置为指向用户单击的元素。现在在Details页面的构造函数中,我使用这个静态变量来设置Details页面本身的datacontext 我期待的是:- 主页首先
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));
}
private void概览列表框\u Tapped\u 1(对象发送方,tappedroutedventargs e)
{
MatchOverview selectedmatch=(发件人作为列表框)。SelectedItem作为MatchOverview;
matchFullDetails=新的ObservableCollection();
foreach(在UpdateController.matchList中匹配m)
{
if(m.matchDescription==selectedmatch.matchDesc)
{
匹配详细信息。添加(m);
打破
}
}
如果(!(matchFullDetails.Count==0))
此.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;
}
公共静态ObservableCollection匹配详细信息;
公共主页()
{
matchFullDetails=新的ObservableCollection();
this.InitializeComponent();
UpdateControl update=new UpdateControl();//创建新对象将更新UpdateControl的概览列表(静态列表)。
overviewlistbox.ItemsSource=UpdateControl.overview\u列表;
}
这是详细信息页面的构造函数代码,出现异常:-
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);
}
}
公共静态ObservableCollection matchdetails=新ObservableCollection();
Dispatchermer dtm_detailspage=null;
公开资料()
{
this.InitializeComponent();
matchdetails=MainPage.matchFullDetails;//matchdetails.Last()为空,因为我们只需要最新添加到集合中的项。
如果(matchdetails.Last().type==“TEST”)//这里抛出异常——初始化
//错误。当我检查MainPage.matchFullDetails时,
//没有数据显示,这意味着它还没有
//已初始化。此外,在
//应用程序的启动,或访问详细信息页面的时间。这也是4-5次中的一次,但并不总是如此。
{
添加(matchdetails.First()作为TestMatch);
}
if(matchdetails.Last().type==“ODI”)
{
添加(matchdetails.Last()作为匹配项);
}
if(matchdetails.Last().type==“T20”)
{
添加(matchdetails.Last()作为T20Match);
}
}
异常屏幕截图:-
遇到错误时的调用堆栈数据:-
是否有此问题的解决方案???您可以通过导航调用传递有关需要在详细信息页面上显示的内容的信息,并在OnNavigatedTo中设置DataContext以覆盖以避免使用静态变量。除非您专门这样做(例如,通过导航到某个页面),否则不会创建页面。如果页面的NavigationCacheMode已从默认设置(已禁用)更改,因此可以在导航调用期间重用该页面的实例。最终很难说问题出在哪里,但这似乎是代码中的某些内容,如果您不共享重现该问题的示例,我们将无法帮助您 *编辑 调试在
MainPage
之前创建的Details
的一种方法是在Details
构造函数的开头添加此代码:
if (MainPage.matchFullDetails == null)
{
System.Diagnostics.Debugger.Break();
}
然后查看VisualStudio中的调用堆栈
面板,了解它是如何构造的
查看matchFullDetails
是否设置为null的一种方法是搜索其赋值(将光标放在Visual Studio代码编辑器中的matchFullDetails
上,然后按Shift+F12)
另一种方法是将匹配的细节制作成
private static ObservableCollection<Match> _matchFullDetails;
public static ObservableCollection<Match> matchFullDetails
{
get
{
return _matchFullDetails;
}
set
{
if (value == null)
{
System.Diagnostics.Debugger.Break();
}
_matchFullDetails = value;
}
}
public static ObservableCollection<Match> matchFullDetails;
static MainPage()
{
matchFullDetails = new ObservableCollection<Match>();
}
public MainPage()
{
this.InitializeComponent();
UpdateController update = new UpdateController(); // Creating new object will update the overview_list of UpdateController(static list).
overviewlistbox.ItemsSource = UpdateController.overview_list;
}