【发布时间】:2020-09-20 17:43:41
【问题描述】:
我正在编写一个天气预报应用程序,但我无法在 ListView 中绑定对象的属性。
集合由具有字符串属性“Temperature”的 WeatherModel 对象组成
Xaml 看到集合并输出它,但找不到对象属性“Temperature”。
如何绑定“温度”属性?
这是我的 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:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="IMAP.View.Weather"
xmlns:local="clr-namespace:IMap.ViewModel;assembly=IMAP">
<ContentPage.Resources>
<local:ForecastWeatherViewModel x:Key="vm"/>
</ContentPage.Resources>
<ContentPage.Content>
<ListView ItemsSource="{Binding items, Source={StaticResource vm}}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal" Padding="2,15">
<Label Text="{Binding Temperature}"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>
</ContentPage>
这是我的视图模型
namespace IMap.ViewModel
{
public class ForecastWeatherViewModel : ViewModel
{
public ObservableCollection<WeatherModel> WeatherList { get; set; }
public ObservableCollection<WeatherModel> items
{
get
{
return WeatherList;
}
}
public ForecastWeatherViewModel()
{
WeatherList = new ObservableCollection<WeatherModel>();
WeatherList.Add(new WeatherModel()
{
Temperature = SomeTemp
});
WeatherList.Add(new WeatherModel()
{
Temperature = SomeTemp
});
OnPropertyChange();
}
}
}
这是我的模特
namespace IMAP.Model
{
public class WeatherModel
{
public string Temperature;
}
}
【问题讨论】:
标签: c# visual-studio xaml xamarin xamarin.forms