我一直在寻找所有可能性,但仍然找不到我的代码出了什么问题。让我解释一下:我有一个由Datatemplate选择器和2 dataTemplate引导的旋转木马视图,第一个显示在第一页,第二个显示在第二个旋转木马页面。在此数据模板内部,我得到了一个永远不会填充的ListView。我知道ListView在这里,因为当我尝试刷新的位置应该刷新刷新指示器。首先,我认为这是因为绑定,但是即使没有绑定,也只是一个文本单元,同样的问题,什么也不会显示。
这是我的代码:XAML
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:TestDappli"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:control="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.CarouselView"
xmlns:testCarouselView="clr-namespace:TestDappli;assembly=TestDappli"
BackgroundColor="White"
x:Name="Mapage"
x:Class="TestDappli.MainPage">
<ContentPage.Resources>
<ResourceDictionary>
<!--Singapour template-->
<DataTemplate x:Key="Singapour">
<StackLayout Orientation="Vertical" Padding="30" Spacing="40" BackgroundColor="White" >
<Label Text="Singapour"
HorizontalOptions="CenterAndExpand"/>
<Label Text="Date :"
VerticalOptions="Start"/>
<DatePicker HorizontalOptions="Center"
Date="{x:Static sys:DateTime.Now}">
<DatePicker.Format>dd-MM-yyyy</DatePicker.Format>
<DatePicker.MinimumDate>
<sys:DateTime x:FactoryMethod="Parse">
<x:Arguments>
<x:String>Jan 1 2000</x:String>
</x:Arguments>
</sys:DateTime>
</DatePicker.MinimumDate>
<DatePicker.MaximumDate>
<sys:DateTime x:FactoryMethod="Parse">
<x:Arguments>
<x:String>Dec 31 2050</x:String>
</x:Arguments>
</sys:DateTime>
</DatePicker.MaximumDate>
</DatePicker>
<Label Text="Dispo :"/>
<ListView x:Name="HoraireViewSingapour"
IsPullToRefreshEnabled="True"
Refreshing="ListItems_Refreshing"
ItemSelected="OnSelection">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="SHOULD BE TEXT1" TextColor="DarkOrange" Detail="BUT THERE IS NOTHING HERE" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</DataTemplate>
<!--Bangalor template-->
<DataTemplate x:Key="Bangalor">
<StackLayout Orientation="Vertical" Padding="30" Spacing="40" BackgroundColor="White" >
<Label Text="Bangalor"
HorizontalOptions="CenterAndExpand"/>
<Label Text="Date :"
VerticalOptions="Start"/>
<DatePicker HorizontalOptions="Center"
Date="{x:Static sys:DateTime.Now}">
<DatePicker.Format>dd-MM-yyyy</DatePicker.Format>
<DatePicker.MinimumDate>
<sys:DateTime x:FactoryMethod="Parse">
<x:Arguments>
<x:String>Jan 1 2000</x:String>
</x:Arguments>
</sys:DateTime>
</DatePicker.MinimumDate>
<DatePicker.MaximumDate>
<sys:DateTime x:FactoryMethod="Parse">
<x:Arguments>
<x:String>Dec 31 2050</x:String>
</x:Arguments>
</sys:DateTime>
</DatePicker.MaximumDate>
</DatePicker>
<Label Text="Dispo :"/>
<ListView x:Name="HoraireViewBangalor"
IsPullToRefreshEnabled="True"
Refreshing="ListItems_Refreshing"
ItemSelected="OnSelection">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="SHOULD BE SOME TEXT HERE " TextColor="DarkOrange" Detail="BUT THERE IS NOT" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</DataTemplate>
<!--Template selector-->
<testCarouselView:CarouselTemplateSelector x:Key="CarouselTemplateSelector"
Singapour="{StaticResource Singapour}"
Bangalor="{StaticResource Bangalor}" />
</ResourceDictionary>
</ContentPage.Resources>
<control:CarouselView
x:Name="MyCarouselView"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
ItemTemplate="{StaticResource CarouselTemplateSelector}"
>
</control:CarouselView>
</ContentPage>
然后输入后面的代码:
public partial class MainPage : ContentPage
{
public ObservableCollection<Creneau> List = new ObservableCollection<Creneau>();
public ObservableCollection<Salle> Salle = new ObservableCollection<Salle>();
public MainPage()
{
BindingContext = this;
CreateList();
InitializeComponent();
this.MyCarouselView.ItemsSource = Salle;
doRefresh();
}
public async void OnSelection(object sender, SelectedItemChangedEventArgs e)
{
if (e.SelectedItem == null)
{
return;
}
else
{
var answer = await DisplayAlert("Reservation", "Voulez vous reserver la salle SALLE de " + ((Creneau)e.SelectedItem).start + "h à " + ((Creneau)e.SelectedItem).end + "h", "Yes", "No");
if (answer)
{
ChangeOccupe((Creneau)e.SelectedItem);
}
}
}
protected void ListItems_Refreshing(object sender, EventArgs e)
{
doRefresh();
((ListView)sender).EndRefresh();
}
protected void doRefresh()
{
}
protected void CreateList()
{
for (int i = 8; i < 20; i++)
{
Creneau cr = new Creneau("Libre", i, i + 1);
List.Add(cr);
}
Salle BangalorS = new Salle("Bangalor", List);
Salle.Add(BangalorS);
Salle SingapourS = new Salle("Singapour", List);
Salle.Add(SingapourS);
}
protected void ChangeOccupe(Creneau c)
{
foreach (Creneau cre in List)
{
if (c.Equals(cre))
{
if (cre.VarOccupe == "Libre")
{
cre.VarOccupe = "Reservée";
}
else
{
cre.VarOccupe = "Libre";
}
}
}
}
}
这是轮播的templateSelector:
public class CarouselTemplateSelector : DataTemplateSelector
{
public DataTemplate Singapour { get; set; }
public DataTemplate Bangalor { get; set; }
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
Salle salle = (Salle)item;
switch (salle.name)
{
case "Singapour":
return Singapour;
case "Bangalor":
return Bangalor;
default:
throw new ArgumentOutOfRangeException();
}
}
}
萨尔和克雷诺都
public class Salle
{
public string name;
public ObservableCollection<Creneau> list = new ObservableCollection<Creneau>();
public Salle(string name, ObservableCollection<Creneau> cren)
{
this.name = name;
list = cren;
}
}
public class Creneau : INotifyPropertyChanged
{
private string occupe;
public string VarOccupe
{
get
{
return occupe;
}
set
{
occupe = value;
OnPropertyChanged("VarOccupe");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public int start { get; set; }
public int end { get; set; }
public string creneau { get; set; }
public Creneau(string occuper,int start, int end)
{
this.end = end;
this.occupe = occuper;
this.start = start;
creneau = start.ToString() + "h " + "- " + end.ToString() + "h :";
}
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
如果您对问题有任何想法,我会接受所有建议,我尝试了2天。 预先谢谢
[编辑]我尝试了使用TableView的情况,它可能来自列表视图中的datatemplate吗?我的意思是我看不到任何地方都无法将DataTemplate放入DataTemplate
答案 0 :(得分:0)
您的页面缺少INotifyPropertyChanged接口。
public partial class MainPage : ContentPage, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public ObservableCollection<Creneau> _list;
public ObservableCollection<Creneau> List
{
get
{
return _list;
}
set
{
_list = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("List"));
}
}
// ....