[英]Cannot display data in ListView Xamarin Forms
I am trying to display data inside a ListView in my XamarinApp, my issue is that I cannot show the data.我试图在我的 XamarinApp 的 ListView 中显示数据,我的问题是我无法显示数据。 I made all bindings and I do not think I made mistakes in binding the listview..我做了所有的绑定,我不认为我在绑定列表视图时犯了错误。
Can someone help?有人可以帮忙吗?
Here is how my UrlBindingModel looks like:这是我的UrlBindingModel的样子:
public class UrlBindingModel
{
public IList<Urls> UrlsList { get; set; }
public UrlBindingModel() // In this constructor we add a few items
{
try
{
UrlsList = new List<Urls>
{
new Urls() { Name = "TestName", Url = "localhost" },
new Urls() { Name = "TestName", Url = "localhost" },
new Urls() { Name = "Test", Url = "Test" },
new Urls() { Name = "Sas", Url = "Sas" }
};
}
catch (Exception ex)
{
}
}
public class Urls
{
public string Name { get; set; }
public string Url { get; set; }
}
}
And here is how my Page.Xaml looks like:这是我的 Page.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:models="clr-namespace:TestFormApp.Models;assembly=TestFormApp"
xmlns:views="clr-namespace:TestFormApp.Views;assembly=TestFormApp"
x:Class="TestFormApp.Pages.SettingsPage"
Title="Settings">
<ContentPage.BindingContext>
<views:UrlBindingModel></views:UrlBindingModel>
</ContentPage.BindingContext>
<ContentPage.Content>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40" />
<RowDefinition Height="40" />
<RowDefinition Height="40" />
<RowDefinition Height="10" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label Text="Urls" Grid.Row="0" FontAttributes="Bold" FontSize="Large" HorizontalOptions="CenterAndExpand"></Label>
<StackLayout Grid.Row="1">
<Button x:Name="btnUrlPopUp" Text="Add Url" Clicked="BtnUrlPopUp_OnClicked"></Button>
</StackLayout>
<Grid Grid.Row="2" VerticalOptions="CenterAndExpand" Margin="10" Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*" />
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="2.5*" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1.5*" />
</Grid.ColumnDefinitions>
<Label Text="Name" Grid.Column="0" Grid.Row="0" Style="{DynamicResource headerTablet}" />
<Label Text=" Host Url" Grid.Column="1" Grid.Row="0" Style="{DynamicResource headerTablet}" />
<Label Text="Edit" Grid.Column="2" Grid.Row="0" Style="{DynamicResource headerTablet}" />
<Label Text="Delete" Grid.Column="3" Grid.Row="0" Style="{DynamicResource headerTablet}" />
<!--The box view creates a line under labels-->
<BoxView Grid.Row="3" HorizontalOptions="FillAndExpand"
VerticalOptions="Center" HeightRequest="2" BackgroundColor="DarkGray"></BoxView>
<ListView IsPullToRefreshEnabled="True" x:Name="lstUrlList" Grid.Row="4" ItemsSource="{Binding UrlsList}" Margin="4">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid VerticalOptions="CenterAndExpand" BackgroundColor="White" ColumnSpacing="0">
<Grid.RowDefinitions>
<RowDefinition Height="40" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*" />
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="3*" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Label Text="{Binding Name, Mode=TwoWay}" Grid.Column="0" Grid.Row="0" Style="{DynamicResource detailTablet}" />
<Label Text="{Binding Url, Mode=TwoWay}" Grid.Column="1" Grid.Row="0" Style="{DynamicResource detailTablet}" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Grid>
</ContentPage.Content>
</ContentPage>
What do I miss??我想念什么??
Thanks in advance!提前致谢!
At first look your viewmodel doesn't implement INotifyPropertyChanged, thus cannot notify your xaml that any property, including one for itemssource, changed.乍一看,您的视图模型没有实现 INotifyPropertyChanged,因此无法通知您的 xaml 任何属性(包括 itemssource 的属性)已更改。
1 inherit your viewmodel from BindableObject 1 从 BindableObject 继承您的视图模型
public class UrlBindingModel : BindableObject
2 change your prop to 2 改变你的道具
private IList<Urls> _urlsList;
public IList<Urls> UrlsList
{
get { return _urlsList; }
set
{
if (_IsProperty != value)
{
_IsProperty = value;
OnPropertyChanged();
}
}
}
Be aware that any property that will change at runtime and doesn't call OnPropertyChanged();请注意,任何将在运行时更改且不调用 OnPropertyChanged(); 的属性; when it changes just will not notify its changes to your UI.当它发生变化时,它不会将其更改通知给您的 UI。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.