Xamarin 为所选项目形成全局变量

问题描述

我需要将 Term.TermID 传递给 CoursesDetailsPage.cs 以便我基本上可以将它用作外键,因为 sqlite 本身没有实现这些。

CoursesDetailsPage 从Courses 页面模态调用,从TermsDetails 页面模态调用,从选择术语的Terms 页面模态调用,因此仍应选择正确的术语。

我需要在某个可以传递给 CoursesDetailsPage 的地方设置所选术语的 TermId(甚至只是它的 int 等效项),以便我可以将其设置为 CourseTermId。

其余变量均通过 Xaml 中的绑定设置。

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("terms")]
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("courses")]
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

<TableSection>
     <EntryCell x:Name="ECTermName" Label="Term Name" Text="{Binding TermName}" />
      </TableSection>
            <TableSection>
                <ViewCell>
                    <DatePicker x:Name="DPTermStart" MinimumDate="01/01/2020" Date="{Binding 
          TermStart,Mode=TwoWay}" Format=" 'Term Start Date: ' MM dd,yyyy" />
                </ViewCell>
                <ViewCell>
                    <DatePicker x:Name="DPTermEnd" MinimumDate="01/01/2020" Date="{Binding 
        TermEnd,Mode=TwoWay}" Format=" 'Term End Date: ' MM dd,yyyy"  />
                </ViewCell>
            </TableSection>
            <TableSection>
                <SwitchCell x:Name="SwIsFinished" Text="Is this term completed?" On="{Binding 
       IsFinished}" />
            </TableSection>
            <TableSection>

代码有点不同,但您为我指明了正确的方向。谢谢你。对于其他需要它的人:

//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;
….
}

解决方法

在你的 App 类中创建一个属性

public MyClass MyProperty { get; set; }

然后你可以在你的应用程序的任何地方访问它

// get
var myvalue = ((App)App.Current).MyProperty;

// set
((App)App.Current).MyProperty = myvalue;