今日は来ています。私は現在自分のクラスとその場所で自分を助ける愚かなアプリを作っています。他の情報もアプリに入っています。のObservableCollection <>とListViewの結合問題
だから、私は4クラスの簡単なListView
あり、この設計を、持っています。
MainPage.xamlを:
MainPage.xaml.cs:
public partial class MainPage : ContentPage
{
public ObservableCollection<Course> Courses { get; set; }
public MainPage()
{
base.BindingContext = this;
Debug.WriteLine("1.");
InitCoursesList();
Debug.WriteLine("2.");
InitializeComponent();
Debug.WriteLine("3.");
//CoursesListView.ItemsSource = Courses;
}
private void InitCoursesList()
{
Debug.WriteLine("1.1");
this.Courses = new ObservableCollection<Course>()
{
new Course()
{
Days = new List<Course.Day>() { Course.Day.M, Course.Day.W },
Title = "Object Oriented Programming",
Type = "CECS",
Number = "274",
Sections = new List<Section>()
{
new Section()
{
Location = "VEC-419",
TeacherName = "Foss S",
Time = "1-1:50pm"
},
new Section()
{
Location = "ECS-414",
TeacherName = "Foss S",
Time = "2-3:15pm"
}
}
},
new Course()
{
Days = new List<Course.Day>() { Course.Day.M, Course.Day.W },
Title = "Adv Artificial Intelligence",
Type = "CECS",
Number = "551",
Sections = new List<Section>()
{
new Section()
{
Location = "VEC-331",
TeacherName = "Todd Ebert",
Time = "3:30-4:45pm"
}
}
},
new Course()
{
Days = new List<Course.Day>() { Course.Day.Tu, Course.Day.Th },
Title = "Operating Syetems",
Type = "CECS",
Number = "326",
Sections = new List<Section>()
{
new Section()
{
Location = "VEC-330",
TeacherName = "Lam S",
Time = "9:30-10:20am"
},
new Section()
{
Location = "ECS-416",
TeacherName = "Lam S",
Time = "10:30-11:45am"
}
}
},
new Course()
{
Days = new List<Course.Day>() { Course.Day.Tu, Course.Day.Th },
Title = "C++ for Java Developers",
Type = "CECS",
Number = "282",
Sections = new List<Section>()
{
new Section()
{
Location = "VEC-518",
TeacherName = "Nachawati S",
Time = "11-11:50am"
},
new Section()
{
Location = "ECS-403",
TeacherName = "Nachawati S",
Time = "12-1:15pm"
}
}
},
};
Debug.WriteLine("1.2");
}
}
すべてがに来ているCSの一部で
<ContentPage.Content>
<AbsoluteLayout>
<Label
AbsoluteLayout.LayoutBounds="0.5, 0, 1, 0.1"
AbsoluteLayout.LayoutFlags="All"
Text="My CSULB Schedule" />
<ListView x:Name="CoursesListView" ItemsSource="{Binding Courses}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="2*" />
<RowDefinition Height="6*" />
<RowDefinition Height="2*" />
</Grid.RowDefinitions>
<!-- TOP -->
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="6*" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions>
<Label
Grid.Column="0"
HorizontalTextAlignment="Center"
Text="{Binding DaysToString}"
VerticalTextAlignment="Center" />
<Label
Grid.Column="1"
HorizontalTextAlignment="Center"
Text="{Binding Title}"
VerticalTextAlignment="Center" />
<Label
Grid.Column="2"
HorizontalTextAlignment="Center"
Text="{Binding TypeAndNumber}"
VerticalTextAlignment="Center" />
</Grid>
<!-- Top -->
<!-- Center -->
<ListView Grid.Row="1" ItemsSource="{Binding Sections}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<ColumnDefinition Width="25*" />
<ColumnDefinition Width="5*" />
<ColumnDefinition Width="25*" />
</Grid.RowDefinitions>
</Grid>
<Label
Grid.Column="0"
HorizontalTextAlignment="Center"
Text="{Binding Location}"
VerticalTextAlignment="Center" />
<Label
Grid.Column="1"
HorizontalTextAlignment="Center"
Text="{Binding TeacherName}"
VerticalTextAlignment="Center" />
<Label
Grid.Column="2"
HorizontalTextAlignment="Center"
Text="{Binding Time}"
VerticalTextAlignment="Center" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<!-- Center -->
<!-- Bottom -->
<Grid Grid.Row="2">
<Grid.RowDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="8*" />
<ColumnDefinition Width="1*" />
</Grid.RowDefinitions>
<!-- Useless stuff at the moment -->
</Grid>
<!-- Bottom -->
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</AbsoluteLayout>
</ContentPage.Content>
、私はこれを持っています良い方法、私はDebug.WriteLine();
の表示を意味します。今私が理解できないことが、私のリストビューが空である理由です。私にとって、それは意味をなさないが、誰もが、しばしば、コンピュータと椅子の間に問題が生じることを知っている。どこが間違っているのか?
私は<ListView x:Name="CoursesListView" ...>
である私のメインListView
を私のpublic ObservableCollection<Course> Courses { get; set; }
と結びつけます。私はそれを埋める、すべてを初期化する、私はバインディングのコンテキストを設定しますが、最後に..ページは空です、なぜですか?
また私は別の質問をしていました。コレクションをリストにバインドすると、このリストビューのすべての要素の中に別のリストをバインドできますか?混同している場合は、Course
オブジェクトのList<Section> Sections
をご覧ください。 xamlの部分では、各要素の中央にリストがあります。私は私の質問は明らかであると思います
は、多分それは多分この質問が面白いです、愚かな質問ですが、私は知らないが、確かに、私はしませんでしたし、私はまだ私が考える何かを理解していません。
ありがとうございました。
あなたが本当にビューの背後にあるコードの代わりにビューモデルを使用して検討すべきです。これは、コードをきれいにして前進させ、ビヘイビアとビジネスロジックをビュー自体から切り離します。 – Cheesebaron