将轮播数据恢复到对象中



我正在我的 Xamarin 应用程序中创建一个动态轮播视图,到目前为止它工作正常,但是......

我的轮播包含学生,每个轮播页面都是指向学生信息页面的链接。我希望能够设置一个包含当前选定学生的对象,以便我抓取所有学生子页面(希望这是有意义的:-/(。

我的脚本如下。

学生模特:

public class StudentData
{
public string id { get; set; }
public string name { get; set; }
public string course { get; set; }
public string schoolclass { get; set; }
public string profileImage { get; set; }
}

旋转木马部分:

ObservableCollection<StudentData> collection = new ObservableCollection<StudentData>();
collection.Add(new StudentData { name = "Soren Hanson", schoolclass = "4. grade", course = "Math" });
collection.Add(new StudentData { name = "Michael Trane", schoolclass = "7. grade", course = "English" });
collection.Add(new StudentData { name = "Tammy Jump", schoolclass = "1. grade", course = "English" });
DataTemplate template = new DataTemplate(() =>
{
var imageBtn = new Button();
imageBtn.Image = "Images/default.png";
imageBtn.Clicked += delegate {
// ADDING THE CURRENT STUDENT TO MY CURRSTUDENT OBJECT //
//App.currStudent = collection.......
var menteeOptions = new MenteeOptions();
imageBtn.Navigation.PushAsync(menteeOptions);
}
}
carousel.ItemTemplate = template;
carousel.PositionSelected += pageChanged;
carousel.ItemsSource = collection;

希望对此有所帮助,并提前表示感谢:-(

好吧,你的问题一开始很混乱,但我会根据我的理解来回答它,你想在你的子页面上保留选定的学生对象:

所以你可以使用SQLite,可以在这里找到

或者,您可以将其维护为 app.xaml 上的静态系统对象.cs并使用它

像这样的东西

应用Xaml.cs

public static object YourDataHolder {get; set;}

在点击的事件中:

imageBtn.Clicked += delegate {
// ADDING THE CURRENT STUDENT TO MY CURRSTUDENT OBJECT //
App.YourDataHolder = _yourCollection;
var menteeOptions = new MenteeOptions();
imageBtn.Navigation.PushAsync(menteeOptions);
}

使用它像这样:

fooObject=App.YourDataHolder as FooType;

最新更新