我有一个通过ListView属性绑定项的ItemsSource。我不明白为什么不显示DataTemplate数据。他们仍然正确地从互联网下载,但没有显示。我尝试过从c#和xaml插入ItemsSource属性,但是结果没有改变。
xaml
<yummy:PancakeView x:Name="ViewFrasi" IsVisible="False" Grid.Row="1" CornerRadius="30,30,0,0" BackgroundColor="White" VerticalOptions="FillAndExpand">
<yummy:PancakeView.Border>
<yummy:Border Color="Blue" Thickness="4"/>
</yummy:PancakeView.Border>
<Grid Margin="15">
<ListView x:Name="Frasi"
ItemsSource="{Binding FrasiJsonOnline1}">
<ListView.Header>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<BoxView CornerRadius="100" Grid.Row="0" HorizontalOptions="End" WidthRequest="40">
<BoxView.Background>
<LinearGradientBrush StartPoint="1,0" EndPoint="1,1">
<GradientStop Color="Blue" Offset="0" />
<GradientStop Color="LightBlue" Offset="1.0" />
</LinearGradientBrush>
</BoxView.Background>
</BoxView>
<Image Source="checked.png" Grid.Row="0" HorizontalOptions="End" HeightRequest="20" Margin="0,0,10,0"/>
<Button x:Name="BtSave" IsVisible="False" Clicked="BtSave_Clicked" BackgroundColor="Transparent" Grid.Row="0" HorizontalOptions="End" HeightRequest="25"/>
<ImageButton Source="close.png" Grid.Row="0" HorizontalOptions="Start" HeightRequest="20" Margin="5,0,0,0" Clicked="Close_Clicked" BackgroundColor="Transparent"/>
</Grid>
</ListView.Header>
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<ScrollView>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="5*"/>
</Grid.ColumnDefinitions>
<ImageButton Grid.Row="0" Grid.Column="0" Source="IconCopy.png" Clicked="CopyClipboard_Clicked"/>
<Label Grid.Row="0" Grid.Column="1" Text="Test" TextColor="Black" FontSize="15"/>
</Grid>
</ScrollView>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</yummy:PancakeView>c#
private async void CategoryView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selected = e.CurrentSelection;
ClassCategory model = e.CurrentSelection.FirstOrDefault() as ClassCategory;
WebClient clientw = new WebClient();
clientw.Credentials = new NetworkCredential("xxxxxxx", "xxxxxx");
string Frasi1 = "ftp://epiz_27426656@ftpupload.net/htdocs/" + model.Titolo + ".json";
string contents1 = await clientw.DownloadStringTaskAsync(Frasi1);
ObservableCollection<FraseClass> FrasiJsonOnline1 = JsonConvert.DeserializeObject<ObservableCollection<FraseClass>>(contents1);
ViewFrasi.IsVisible = true;
ViewFrasi.TranslationY = 600;
ViewFrasi.TranslateTo(0, 0, 500, Easing.SinInOut);
}发布于 2021-01-25 21:41:59
您正在使用局部变量,使其成为方法外部的公共属性,并在方法中更改其值,如下所示:
public ObservableCollection<FraseClass> FrasiJsonOnline1 { get; set; }
private async void CategoryView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selected = e.CurrentSelection;
ClassCategory model = e.CurrentSelection.FirstOrDefault() as ClassCategory;
WebClient clientw = new WebClient();
clientw.Credentials = new NetworkCredential("xxxxxxx", "xxxxxx");
string Frasi1 = "ftp://epiz_27426656@ftpupload.net/htdocs/" + model.Titolo + ".json";
string contents1 = await clientw.DownloadStringTaskAsync(Frasi1);
FrasiJsonOnline1 = JsonConvert.DeserializeObject<ObservableCollection<FraseClass>>(contents1);
ViewFrasi.IsVisible = true;
ViewFrasi.TranslationY = 600;
ViewFrasi.TranslateTo(0, 0, 500, Easing.SinInOut);
}发布于 2021-01-26 05:37:33
当您将ListView的ItemsSource绑定到FrasiJsonOnline1时。
这意味着您正在绑定到BindingContext.FrasiJsonOnline1。
ItemsSource="{Binding BindingContext.FrasiJsonOnline1}".所以FrasiJsonOnline1应该是BindingContext的一个属性。例如,如果您的BindingContext是myViewModel,那么FrasiJsonOnline1应该是myViewModel的属性,那么绑定就会成功:
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
BindingContext = new myViewModel();
}
}
public class myViewModel
{
public ObservableCollection<FraseClass> FrasiJsonOnline1 { get; set; }
public myViewModel()
{
FrasiJsonOnline1 = new ObservableCollection<FraseClass>();
//...your codes
}
}如果您按照您的方式定义FrasiJsonOnline1:
public class myViewModel
{
public myViewModel()
{
}
private async void CategoryView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ObservableCollection<FraseClass> FrasiJsonOnline1 = JsonConvert.DeserializeObject<ObservableCollection<FraseClass>>(contents1);
}
}FrasiJsonOnline1是在方法中定义的,它是一个局部变量。BindingContext(myViewModel)没有一个名为FrasiJsonOnline1的属性,并且绑定无法工作。
https://stackoverflow.com/questions/65889747
复制相似问题