2014-09-03 3 views
0

Windows Phone App을 개발 중이며 내 MainPage.xaml.cs 파일에 우선 메서드가 하나만 변경되어 있습니다. OnNavigateTo(). 값이 변경 되더라도 MainPage 생성자에서 값이 0으로 재설정됩니다 (int 멤버). 나는 OnNavigateTo() 메서드가 생성자보다 먼저 호출되고 있다고 생각하지만, 그렇다면 nullReferenceException을 갖게 될 것입니다. 그 문제를 일으킬 수있는 것은 무엇입니까?NavigateTo() 함수가 생성자보다 먼저 호출되고 있습니까?

OnNavigateTo() 함수 :

(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 :하여 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(); 
    } 
} 

}

+1

생성자와 'OnNavigateTo'메소드를 보면 매우 유용 할 것입니다. –

답변

0

OnNavigatedTo 방법은 생성자 전에 호출 할 수 없습니다

는 여기에 MainPage.xaml.cs 파일입니다. 생성자는 항상 먼저 실행됩니다. 내 생각에 model.Left.LifeTime 님은 PropertyChanged 이벤트를 발생시키지 않습니다. 따라서 귀하의 View은 귀하가 그 가치를 부여하고 있음을 알 수 없습니다. 그러므로 기본값 인 model.Left.Lifetime이 표시 될 것입니다.

반면에 나머지 코드는 보지 않고 말하기가 어렵습니다.

+0

답변 해 주셔서 감사합니다. 그 문제를 일으키는 원인은 무엇입니까? –

관련 문제