title: Xamarin forms global variable for a selected item
body: <p>I need to have the Term.TermID passed to the CoursesDetailsPage.cs so that I can basically use it as a foreign key, since SQLite doesn’t implement those natively.</p>
  <p>The CoursesDetailsPage is called modally from the Courses page, which is called modally from the TermsDetails page, which is called modally from the Terms page which selects the term, so the correct term should still be selected.</p>
  <p>I need to set the TermId of the selected term (or even just the int equivalent of it) somewhere that can be passed to the CoursesDetailsPage so that I can set it to the CourseTermId.</p>
  <p>The rest of the variables are all set through bindings in Xaml.</p>
  <pre><code>TermsPage.xaml.cs
  
  //This is where the term is selected, and the first page that calls the modal stack.
  async void OnTermSelected(object sender, SelectedItemChangedEventArgs e)
  {
      if (e.SelectedItem != null)
      {
        await Navigation.PushModalAsync(new TermsDetailsPage
          {
          BindingContext = e.SelectedItem as Term
          });
      }
  }
  
  
  //The Term is then saved on the TermsDetailsPage:
  TermsDetailsPage.xaml.cs
  
  async void OnSaveTermClicked(object sender, EventArgs e)
  {
  var term = (Term)BindingContext;
  term.TermName = ECTermName.Text;
  term.TermStart = DPTermStart.Date;
  term.TermEnd = DPTermEnd.Date;
  term.IsFinished = SwIsFinished.On;
  
  if (String.IsNullOrWhiteSpace(term.TermName))
  {
  return;
  }
  
  
  //And from here you can call the Courses page, which lists all of the courses:
  //Eventually this will need to only get the courses belonging to this Term, 
  //but for that I need to match the course to the term, which is where the 
  //CourseTermId comes in. 
  
  async void OnShowCoursesClicked(object sender, EventArgs e)
  {
  //Code for selecting CourseTermId = TermId goes here.
  //If there are none, it goes to an empty list with an Add Course button.
  
  await Navigation.PushModalAsync(new CoursesPage()); 
  }
  
  
  //And the Add Course button:
  CoursesPage.xaml.cs
  
  async void OnAddCourseClicked(object sender, EventArgs e)
  {
  await Navigation.PushModalAsync(new CoursesDetailsPage()
  {
  BindingContext = new Course()
  });
  }
  
  //Leads to the page that needs the TermID or the Global int variable passed to it. 
  
  CoursesDetailsPage.xaml.cs
  
  async void OnSaveCourseClicked(object sender, EventArgs e)
  {            
  var course = (Course)BindingContext;
  course.CourseName = ECCourseName.Text;
  course.CourseStatus = PKCourseStatus.SelectedItem.ToString();
  
  //course.CourseTermId = term.TermId; This is what I need to be able to set
  ….
  await App.TrackDB.SaveCourseAsync(course);
  await Navigation.PopModalAsync();
  }
  
  
  //Here are the relevant parts of the Terms and Courses models, and part of the Terms Xaml page to show how I am binding things, although the TermId is the Primary Key, so it is auto increment and not manually set. The Courses is a lot longer, but the rest of it is all working.
  
  Terms.cs
  
  [Table(&quot;terms&quot;)]
  public class Term
  {
  [PrimaryKey, AutoIncrement]
  public int TermId { get; set; }
  
   [MaxLength(255)]
  public string TermName { get; set; }
  
  public DateTime TermStart { get; set; }
  public DateTime TermEnd { get; set; }
  
  public bool IsFinished { get; set; }
  }
  
  Courses.cs
  [Table(&quot;courses&quot;)]
  public class Course
  {
  [PrimaryKey, AutoIncrement]
  public int CourseId { get; set; }
  public int CourseTermId { get; set; }
  
  [MaxLength(250), Unique]
  public string CourseName { get; set; }
  public string CourseStatus { get; set; }
  ….
  }
  
  
  
  TermsDetailsPage.Xaml
  
  &lt;TableSection&gt;
       &lt;EntryCell x:Name=&quot;ECTermName&quot; Label=&quot;Term Name&quot; Text=&quot;{Binding TermName}&quot; /&gt;
        &lt;/TableSection&gt;
              &lt;TableSection&gt;
                  &lt;ViewCell&gt;
                      &lt;DatePicker x:Name=&quot;DPTermStart&quot; MinimumDate=&quot;01/01/2020&quot; Date=&quot;{Binding 
            TermStart, Mode=TwoWay}&quot; Format=&quot; 'Term Start Date: ' MM dd, yyyy&quot; /&gt;
                  &lt;/ViewCell&gt;
                  &lt;ViewCell&gt;
                      &lt;DatePicker x:Name=&quot;DPTermEnd&quot; MinimumDate=&quot;01/01/2020&quot; Date=&quot;{Binding 
          TermEnd, Mode=TwoWay}&quot; Format=&quot; 'Term End Date: ' MM dd, yyyy&quot;  /&gt;
                  &lt;/ViewCell&gt;
              &lt;/TableSection&gt;
              &lt;TableSection&gt;
                  &lt;SwitchCell x:Name=&quot;SwIsFinished&quot; Text=&quot;Is this term completed?&quot; On=&quot;{Binding 
         IsFinished}&quot; /&gt;
              &lt;/TableSection&gt;
              &lt;TableSection&gt;
  </code></pre>
  <p>The code was a little different, but you pointed me in the right direction. Thank you. For anyone else needing it:</p>
  <pre><code>//Global created in the App.xaml.cs page
  public static class MyGlobals
          {
              public static int TermIdInt { get; set; }
          }
  
  // set on TermsDetailsPage when going to the Courses page
  async void OnShowCoursesClicked(object sender, EventArgs e)
          {
              var term = (Term)BindingContext;
              App.MyGlobals.TermIdInt = term.TermId;
              await Navigation.PushModalAsync(new CoursesPage()); 
          }
  
  
  //get when saving the Course
  async void OnSaveCourseClicked(object sender, EventArgs e)
          {
              var termIdInt = App.MyGlobals.TermIdInt;
              var course = (Course)BindingContext;
  
              course.CourseName = ECCourseName.Text;
              course.CourseStatus = PKCourseStatus.SelectedItem.ToString();
  
              course.CourseTermId = termIdInt;
  ….
  }
  </code></pre>