微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将两个类与不同页面上的 MVVM 链接起来

如何解决将两个类与不同页面上的 MVVM 链接起来

我有一个关于 MVVM 模式的 Xamarin Forms 问题。它太大太多页面,无法在这里展示所有代码,但它非常紧密地基于这个项目: Xamarin.Forms MVVM: How to Work with SQLite DB

主要区别在于我有两个课程,即学期和属于这些学期的课程,因此几乎所有其他课程也都翻了一番。 Solution Explorer screenshot 但是 sqlite 似乎不允许外键,我也想不出如何自动将 TermId 发送到 Course 的 CourseTermId 字段。 (我是为学校做的,很多插件是不允许使用的,所以请不要推荐。)

您可以通过学期详细信息页面上的按钮进入添加课程页面,因此选择了正确的学期,并且添加课程是模态的,因此所选学期页面仍然可用。一切都通过数据绑定绑定,其他一切都正常工作,我所有的表单和页面。但我无法将该 TermId 连接到“课程”页面

我可能需要一个全局变量,因为即使在运行时它应该有 Term.TermId 可用,但在我编写程序时它可能没有意识到这一点。如果是这样,我该怎么做并确保每次我选择不同的术语时都会更新它?

一旦我有了 TermId,我该如何设置 CourseTermId 以匹配它?其他一切都是通过数据绑定设置的,我不知道如何手动设置某些东西,或者等于其他东西。

//Courseviewmodel.cs is where all of the Course variables are set,from the CourseDetails.Xaml page from bindings. 

public Courseviewmodel(Course course)
{    
IDCourse = courseId;
courseId = course.CourseId;
courseName = course.CourseName;
courseTermId = course.CourseTermId;
….
}

private int _courseTermId;
public int courseTermId
{  get { return _courseTermId; }
set
{  SetValue(ref _courseTermId,value);
}}

    //Here is some of the CoursesDetailsPage.Xaml page. It isn’t pulling the Term.TermID this far,which is why I may need the Global Variable,//or a longer pathname? I wasn't trying to set it here,just show it to see if it was coming this far. It is set automatically as the Primary Key for Term.

<TableView Intent="Form" Margin="16,0">
<TableRoot>
<TableSection Title="Course">
<EntryCell Label="Course Name" Text="{Binding Path=Course.CourseName}" />
 <EntryCell Label="Course Term" Text="{Binding Path=Course.CourseTermId}" />
<EntryCell Label="Term ID" Text="{Binding Path=Term.TermId}" />
<EntryCell Label="Course ID" Text="{Binding Path=Course.CourseId}" />
….
</TableSection>


//Classes: There is more to the Course class,but that shouldn't matter.

    public class Term
    {
        
    [PrimaryKey,AutoIncrement]
    public int TermId { get; set; }

    [MaxLength(255),Unique]
    public string TermName { get; set; }
        
    public string TermStart { get; set; }
    public string TermEnd { get; set; }
        
    public bool IsFinished { get; set; }
    }


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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。