Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否在构造函数之前调用NavigateTo()函数?_C#_Xaml_Windows Phone - Fatal编程技术网

C# 是否在构造函数之前调用NavigateTo()函数?

C# 是否在构造函数之前调用NavigateTo()函数?,c#,xaml,windows-phone,C#,Xaml,Windows Phone,我正在开发一个Windows phone应用程序,在MainPage.xaml.cs文件中,有一个私有成员正在被重写的方法OnNavigateTo()中更改。尽管其值已更改,但在主页面构造函数中更改后,其值将重置为0(它是一个int成员)。我猜在构造函数之前调用了OnNavigateTo()方法,但如果是这样,我将有一个nullReferenceException。什么会导致这个问题 OnNavigateTo()函数: if(NavigationContext.QueryString.Conta

我正在开发一个Windows phone应用程序,在MainPage.xaml.cs文件中,有一个私有成员正在被重写的方法
OnNavigateTo()
中更改。尽管其值已更改,但在主页面构造函数中更改后,其值将重置为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.life
值,但在MainPage.xaml中,我仍然得到默认的0值……这就像完全忽略了这行代码一样。我知道代码不是特别清楚,但我认为添加多余的无用c代码行是没有好处的颂歌

以下是
MainPage.xaml.cs
文件: 公共部分类主页:PhoneApplicationPage {

            //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;

        }
公共联系人模型;
private int _firstRunLeftDuration,_firstRunRightDuration;//用于应用程序初始化的成员
公共int FirstRunLeftDuration
{
得到
{
返回_firstRunLeftDuration;
}
设置
{
_firstRunLeftDuration=值;
}
}
公共int FirstRunRightDuration
{
得到
{
返回_firstRunRightDuration;
}
设置
{
_firstRunRightDuration=值;
}
}
公共联系人模型
{
得到
{
收益模型;
}
设置
{
模型=价值;
}
}
//建造师
公共主页()
{
初始化组件();
//本地化ApplicationBar的示例代码
BuildLocalizedApplicationBar();
//应该检查用户是否第一次启动应用程序。。。。
//创建一个新模型
Model=新的ContactLensesModel();
Model.setLeftNewsStartingDate();
Model.setRightNewsStartingDate();
//如果用户第一次未输入,则应加载已保存的模型。。。
//....
//....
loadModel();
//连接数据上下文
LeftLensDaysLeving.DataContext=Model.Left;
RightLensDaysLeving.DataContext=Model.Right;
}
私有int getDurationAsNumber(LensLifetime持续时间)
{
if(持续时间等于(LensLifetime.Day))
返回1;
else if(持续时间等于(LensLifetime.两周))
返回14;
其他的
返回DateTime.DaysInMonth(DateTime.Now.Year,DateTime.Now.Month);
}
受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
//将参数获取为字符串并将其转换为枚举,仅当用户第一次进入应用程序时才为真。
if(NavigationContext.QueryString.ContainsKey(“leftDuration”))
{
//以字符串形式从简介页面获取所选值
var leftReceivedInformation=NavigationContext.QueryString[“leftDuration”];
//将字符串转换为枚举对象
var firstRunLeftChosenDuration=(LensLifetime)Enum.Parse(typeof(LensLifetime),leftReceivedInformation);
//将leftDuration值设置为模型对象
FirstRunLeftDuration=getDurationAsNumber(firstRunLeftChosenDuration);
Model.Left.life=FirstRunLeftDuration;
}
if(NavigationContext.QueryString.ContainsKey(“rightDuration”))
{
//以字符串形式从简介页面获取所选值
var RightReceivedInformation=NavigationContext.QueryString[“rightDuration”];
//将字符串转换为枚举对象
var firstRunRightChosenDuration=(LensLifetime)Enum.Parse(typeof(LensLifetime),rightReceivedInformation);
//将leftDuration值设置为模型对象
_firstRunRightDuration=getDurationAsNumber(firstRunRightChosenDuration);
Model.Right.LifeTime=\u firstRunRightDuration;
}
}
/// 
///从隔离存储加载模型
/// 
私有void loadModel()
{
//加载模型。。。
}
私有void BuildLocalizedApplicationBar()
{
//将页面的ApplicationBar设置为ApplicationBar的新实例。
ApplicationBar=新的ApplicationBar();
//创建一个新按钮,并将文本值设置为AppResources中的本地化字符串。
ApplicationBarIconButton appBarSettingsButton=新的ApplicationBarIconButton(新Uri(“/Assets/Icons/settingsIcon4.png”,UriKind.Relative));
appBarSettingsButton.Text=AppResources.AppBarSettingsButtonText;
appBarSettingsButton.Click+=appBarButton\u Click;
ApplicationBar.Buttons.Add(appBarSettingsButton);
//使用AppResources中的本地化字符串创建新菜单项。
//ApplicationBarMenuItem appBarMenuItem=新的ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
//ApplicationBar.MenuItems.Add(appBarMenuItem);
}
无效appBarButton_单击(对象发送方,事件参数e)
{
NavigationService.Navigate(新Uri(“/SettingsPage.xaml”,UriKind.RelativeOrAbsolute));
}
私有void leftButtonChange_单击(对象发送者,路由目标)
{
model.setLeftNewsStartingDate();
}
私有void rightChangeButton_单击(对象发送者,路由目标)
{
model.setRightNewsStartingDate();
}
}

}

无法在构造函数之前调用OnNavigatedTo方法。构造函数总是先执行。我认为您的
model.Left.life
不会引发
属性更改事件。因此,您的
视图
获胜