我正在开发Windows手机应用程序,在我的MainPage.xaml.cs文件中,我有一个私有成员正在覆盖方法OnNavigateTo()
中进行更改。虽然它的值已更改,但在MainPage构造函数中,其值将重置为0(它是一个int成员)。我想在构造函数之前调用OnNavigateTo()
方法但是如果是这样的话我会得到nullReferenceException。什么可能导致这个问题?
OnNavigateTo()函数:
if(NavigationContext.QueryString.ContainsKey(“leftDuration”)) {
//Get the selected value from IntroductionPage as a string
var leftRecievedInformation = NavigationContext.QueryString["leftDuration"];
//Convert the string to an enum object
var firstRunLeftChosenDuration = (LensLifetime)Enum.Parse(typeof(LensLifetime), leftRecievedInformation);
//Set the leftDuration value to the model object
_firstRunLeftDuration = getDurationAsNumber(firstRunLeftChosenDuration);
MessageBox.Show(_firstRunLeftDuration + "");
model.Left.LifeTime = _firstRunLeftDuration;
}
我有问题的成员是_firstRunLeftDuration值。虽然,正如你所看到的,我设置了model.Left.LifeTime
值,在MainPage.xaml中我仍然得到默认的0值...它就像完全忽略了这行代码..我知道代码不是特别的清楚,但我不认为添加额外的无用代码行是有益的。
这是MainPage.xaml.cs
文件:
public partial class MainPage:PhoneApplicationPage
{
public ContactLensesModel model;
private int _firstRunLeftDuration, _firstRunRightDuration; //Members used for the initialization of the app
public int FirstRunLeftDuration
{
get
{
return _firstRunLeftDuration;
}
set
{
_firstRunLeftDuration = value;
}
}
public int FirstRunRightDuration
{
get
{
return _firstRunRightDuration;
}
set
{
_firstRunRightDuration = value;
}
}
public ContactLensesModel Model
{
get
{
return model;
}
set
{
model = value;
}
}
// Constructor
public MainPage()
{
InitializeComponent();
// Sample code to localize the ApplicationBar
BuildLocalizedApplicationBar();
//Should check if the user starts the app for the first time....
//Create a new model
Model = new ContactLensesModel();
Model.setLeftNewStartingDate();
Model.setRightNewStartingDate();
//Should load the already saved model if the user in not entering for the first time...
//....
//....
loadModel();
//Connect the data Context
leftLensDaysRemaining.DataContext = Model.Left;
rightLensDaysRemaining.DataContext = Model.Right;
}
private int getDurationAsNumber(LensLifetime duration)
{
if (duration.Equals(LensLifetime.Day))
return 1;
else if (duration.Equals(LensLifetime.Two_Weeks))
return 14;
else
return DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
//Get the arguments as strings and convert them to an enum, is true only when the user enters app for the first time.
if (NavigationContext.QueryString.ContainsKey("leftDuration"))
{
//Get the selected value from IntroductionPage as a string
var leftRecievedInformation = NavigationContext.QueryString["leftDuration"];
//Convert the string to an enum object
var firstRunLeftChosenDuration = (LensLifetime)Enum.Parse(typeof(LensLifetime), leftRecievedInformation);
//Set the leftDuration value to the model object
FirstRunLeftDuration = getDurationAsNumber(firstRunLeftChosenDuration);
Model.Left.LifeTime = FirstRunLeftDuration;
}
if (NavigationContext.QueryString.ContainsKey("rightDuration"))
{
//Get the selected value from IntroductionPage as a string
var rightRecievedInformation = NavigationContext.QueryString["rightDuration"];
//Convert the string to an enum object
var firstRunRightChosenDuration = (LensLifetime)Enum.Parse(typeof(LensLifetime), rightRecievedInformation);
//Set the leftDuration value to the model object
_firstRunRightDuration = getDurationAsNumber(firstRunRightChosenDuration);
Model.Right.LifeTime = _firstRunRightDuration;
}
}
/// <summary>
/// Loads the model from the isolated Storage
/// </summary>
private void loadModel()
{
//Load the model...
}
private void BuildLocalizedApplicationBar()
{
// Set the page's ApplicationBar to a new instance of ApplicationBar.
ApplicationBar = new ApplicationBar();
// Create a new button and set the text value to the localized string from AppResources.
ApplicationBarIconButton appBarSettingsButton = new ApplicationBarIconButton(new Uri("/Assets/Icons/settingsIcon4.png", UriKind.Relative));
appBarSettingsButton.Text = AppResources.AppBarSettingsButtonText;
appBarSettingsButton.Click += appBarButton_Click;
ApplicationBar.Buttons.Add(appBarSettingsButton);
// Create a new menu item with the localized string from AppResources.
//ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
//ApplicationBar.MenuItems.Add(appBarMenuItem);
}
void appBarButton_Click(object sender, EventArgs e)
{
NavigationService.Navigate(new Uri("/SettingsPage.xaml", UriKind.RelativeOrAbsolute));
}
private void leftButtonChange_Click(object sender, RoutedEventArgs e)
{
model.setLeftNewStartingDate();
}
private void rightChangeButton_Click(object sender, RoutedEventArgs e)
{
model.setRightNewStartingDate();
}
}
}
答案 0 :(得分:0)
在构造函数之前无法调用OnNavigatedTo
方法。始终首先执行构造函数。我认为您的model.Left.LifeTime
不会引发PropertyChanged
事件。因此,您的View
不会知道您给它一个值。因此,它将显示model.Left.Lifetime
的默认值,该值可能为0.
另一方面,如果没有看到其余的代码,很难说清楚。