【发布时间】:2021-04-21 14:32:20
【问题描述】:
我需要将 Term.TermID 传递给 CoursesDetailsPage.cs 以便我基本上可以将其用作外键,因为 SQLite 本身并没有实现这些。
CoursesDetailsPage 是从 Courses 页面模态调用的,它是从 TermsDetails 页面模态调用的,它是从选择术语的术语页面模态调用的,因此仍然应该选择正确的术语。
我需要在可以传递给 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 类上创建一个属性,它可以从应用程序内的任何地方访问跨度>
-
我见过的唯一例子是静态全局变量。因为这是在选择 Term 时发生变化的东西,有人可以帮我写一些代码吗?或者如果有更简单的方法,告诉我?我是 Xamarin 新手,对编程也很陌生,所以我正在尽我所能。
标签: c# xamarin.forms global-variables selecteditem