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

TBa*_*don 0 c# global-variables selecteditem xamarin.forms

我需要将 Term.TermID 传递给 CoursesDetailsPage.cs,这样我基本上就可以将它用作外键,因为 SQLite 本身并没有\xe2\x80\x99t 实现这些。

\n

CoursesDetailsPage 从“课程”页面以模态方式调用,从“TermsDetails”页面以模态方式调用“TermsDetails”页面,从选择术语的“Terms”页面以模态方式调用该页面,因此仍应选择正确的术语。

\n

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

\n

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

\n
TermsPage.xaml.cs\n\n//This is where the term is selected, and the first page that calls the modal stack.\nasync void OnTermSelected(object sender, SelectedItemChangedEventArgs e)\n{\n    if (e.SelectedItem != null)\n    {\n      await Navigation.PushModalAsync(new TermsDetailsPage\n        {\n        BindingContext = e.SelectedItem as Term\n        });\n    }\n}\n\n\n//The Term is then saved on the TermsDetailsPage:\nTermsDetailsPage.xaml.cs\n\nasync void OnSaveTermClicked(object sender, EventArgs e)\n{\nvar term = (Term)BindingContext;\nterm.TermName = ECTermName.Text;\nterm.TermStart = DPTermStart.Date;\nterm.TermEnd = DPTermEnd.Date;\nterm.IsFinished = SwIsFinished.On;\n\nif (String.IsNullOrWhiteSpace(term.TermName))\n{\nreturn;\n}\n\n\n//And from here you can call the Courses page, which lists all of the courses:\n//Eventually this will need to only get the courses belonging to this Term, \n//but for that I need to match the course to the term, which is where the \n//CourseTermId comes in. \n\nasync void OnShowCoursesClicked(object sender, EventArgs e)\n{\n//Code for selecting CourseTermId = TermId goes here.\n//If there are none, it goes to an empty list with an Add Course button.\n\nawait Navigation.PushModalAsync(new CoursesPage()); \n}\n\n\n//And the Add Course button:\nCoursesPage.xaml.cs\n\nasync void OnAddCourseClicked(object sender, EventArgs e)\n{\nawait Navigation.PushModalAsync(new CoursesDetailsPage()\n{\nBindingContext = new Course()\n});\n}\n\n//Leads to the page that needs the TermID or the Global int variable passed to it. \n\nCoursesDetailsPage.xaml.cs\n\nasync void OnSaveCourseClicked(object sender, EventArgs e)\n{            \nvar course = (Course)BindingContext;\ncourse.CourseName = ECCourseName.Text;\ncourse.CourseStatus = PKCourseStatus.SelectedItem.ToString();\n\n//course.CourseTermId = term.TermId; This is what I need to be able to set\n\xe2\x80\xa6.\nawait App.TrackDB.SaveCourseAsync(course);\nawait Navigation.PopModalAsync();\n}\n\n\n//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.\n\nTerms.cs\n\n[Table("terms")]\npublic class Term\n{\n[PrimaryKey, AutoIncrement]\npublic int TermId { get; set; }\n\n [MaxLength(255)]\npublic string TermName { get; set; }\n\npublic DateTime TermStart { get; set; }\npublic DateTime TermEnd { get; set; }\n\npublic bool IsFinished { get; set; }\n}\n\nCourses.cs\n[Table("courses")]\npublic class Course\n{\n[PrimaryKey, AutoIncrement]\npublic int CourseId { get; set; }\npublic int CourseTermId { get; set; }\n\n[MaxLength(250), Unique]\npublic string CourseName { get; set; }\npublic string CourseStatus { get; set; }\n\xe2\x80\xa6.\n}\n\n\n\nTermsDetailsPage.Xaml\n\n<TableSection>\n     <EntryCell x:Name="ECTermName" Label="Term Name" Text="{Binding TermName}" />\n      </TableSection>\n            <TableSection>\n                <ViewCell>\n                    <DatePicker x:Name="DPTermStart" MinimumDate="01/01/2020" Date="{Binding \n          TermStart, Mode=TwoWay}" Format=" 'Term Start Date: ' MM dd, yyyy" />\n                </ViewCell>\n                <ViewCell>\n                    <DatePicker x:Name="DPTermEnd" MinimumDate="01/01/2020" Date="{Binding \n        TermEnd, Mode=TwoWay}" Format=" 'Term End Date: ' MM dd, yyyy"  />\n                </ViewCell>\n            </TableSection>\n            <TableSection>\n                <SwitchCell x:Name="SwIsFinished" Text="Is this term completed?" On="{Binding \n       IsFinished}" />\n            </TableSection>\n            <TableSection>\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n
//Global created in the App.xaml.cs page\npublic static class MyGlobals\n        {\n            public static int TermIdInt { get; set; }\n        }\n\n// set on TermsDetailsPage when going to the Courses page\nasync void OnShowCoursesClicked(object sender, EventArgs e)\n        {\n            var term = (Term)BindingContext;\n            App.MyGlobals.TermIdInt = term.TermId;\n            await Navigation.PushModalAsync(new CoursesPage()); \n        }\n\n\n//get when saving the Course\nasync void OnSaveCourseClicked(object sender, EventArgs e)\n        {\n            var termIdInt = App.MyGlobals.TermIdInt;\n            var course = (Course)BindingContext;\n\n            course.CourseName = ECCourseName.Text;\n            course.CourseStatus = PKCourseStatus.SelectedItem.ToString();\n\n            course.CourseTermId = termIdInt;\n\xe2\x80\xa6.\n}\n
Run Code Online (Sandbox Code Playgroud)\n

Jas*_*son 6

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

public MyClass MyProperty { get; set; }
Run Code Online (Sandbox Code Playgroud)

然后您可以在应用程序中的任何位置访问它

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

// set
((App)App.Current).MyProperty = myvalue;
Run Code Online (Sandbox Code Playgroud)