Binding ItemTemplate In MvxListView
I'm developing simple Android application with Mvvmcross v.6.1.2 and have a trouble with MvxListView ItemTemplate binding. Here is my ListView, which is correctly binded to MvxObservableCollection Weather in my ViewModel:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<MvxListView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:divider="#000000"
android:dividerHeight="3px"
local:MvxBind="ItemsSource Weather; ItemClick ListItemClickCommand"
local:MvxItemTemplate="ItemTemplateId weatherListItem"
SelectedItem="{mvx:MvxBind CurrentWeather}"/>
</LinearLayout>
and here is my ListView ItemTemplate:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:layout_gravity="center_horizontal"
local:MvxBind="Text WeatherName"/>
</LinearLayout>
What's wrong in local:MvxItemTemplate="ItemTemplateId weatherListItem"
?
Answer
You have a couple of problems in your layout:
<MvxListView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:divider="#000000"
android:dividerHeight="3px"
local:MvxBind="ItemsSource Weather; ItemClick ListItemClickCommand"
local:MvxItemTemplate="ItemTemplateId weatherListItem"
SelectedItem="{mvx:MvxBind CurrentWeather}"/>
First issue here is
local:MvxItemTemplate="ItemTemplateId weatherListItem"
MvxItemTemplate
when used in a axml
/xml
file, it refers to a layout to use as the item template. This will be a layout you have in your android resources. In order to point it at one of your resource layouts you need to write it like as follows:
local:MvxItemTemplate="@layout/nameoflayout"
This will give the ListView a ID that matches your layout when it is inflated.
The other issue here is that you are mixing Xamarin.Forms XAML attributes with Android attributes:
SelectedItem="{mvx:MvxBind CurrentWeather}"
This line won't inflate, because there is no attribute called SelectedItem
in MvxListView
. If you wish to bind a public property, it needs to be done in MvxBind
.
local:MvxBind="ItemsSource Weather; ItemClick ListItemClickCommand; SelectedItem CurrentWeather"
Related Questions
- → How to Fire Resize event after all images resize
- → JavaScript in MVC 5 not being read?
- → URL routing requires /Home/Page?page=1 instead of /Home/Page/1
- → Getting right encoding from HTTPContext
- → How to create a site map using DNN and C#
- → I want integrate shopify into my mvc 4 c# application
- → Bootstrap Nav Collapse via Data Attributes Not Working
- → Shopify api updating variants returned error
- → Get last n quarters in JavaScript
- → ASP.NET C# SEO for each product on detail page on my ECOMMERCE site
- → SEO Meta Tags From Behind Code - C#
- → onchange display GridView record if exist from database using Javascript
- → How to implement search with two terms for a collection?