我无法使用mvvmcross 4.4.0将可观察集合绑定到axml Android布局文件中。我搜索了很多,但没有任何帮助我。
我的问题是这段代码没有显示任何错误,此外它根本不起作用。
视图模型
public class InitialViewModel : MvxViewModel
{
MvxObservableCollection<Search> _results;
public MvxObservableCollection<Search> results
{
get { return _results; }
set
{
_results = value;
RaisePropertyChanged(() => results);
}
}
String _titleToSearch;
public string titleToSearch
{
get { return _titleToSearch; }
set
{
titleToSearch = value;
RaisePropertyChanged(() => titleToSearch);
}
}
//commands
public IMvxCommand SearchTitle
{
get { return new MvxCommand(async () => await SearchAsync()); }
}
public InitialViewModel()
{
results = new MvxObservableCollection<Search>();
}
//methods
async Task SearchAsync() { await Task.Run(() => searchByTitle()); }
void searchByTitle()
{
/*
here i set value to Observablecollection<search>();
using and api call, its all debbuged and the colletion
"results" recieve the information.
public class search{
public string a { get; set; }
public string b { get; set; }
public string c { get; set; }
public string d { get; set; }
public string e { get; set; }
}
*/
}
}
初始axml
<?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:id="@+id/linearLayout1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:lines="1"
android:layout_weight="1"
local:MvxBind="Text titleToSearch" />
<ImageButton
android:id="@+id/button1"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@mipmap/ic_search_white_24dp"
local:MvxBind="Click SearchTitle" />
</LinearLayout>
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<Mvx.MvxListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
local:MvxBind="ItemsSource results"
local:MvxItemTemplate="@layout/listitem" />
</ScrollView>
</LinearLayout>
listItem axml
<?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:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="20dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
local:MvxBind="Text a" />
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@mipmap/ic_note_add_black_24dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@mipmap/ic_open_in_new_black_24dp" />
</LinearLayout>
</LinearLayout>
答案 0 :(得分:1)
问题是您LinearLayout
中的父Initial.axml
未指定方向。到default orientation is horizontal。您需要将其设置为android:orientation="vertical"
。
您也无需将MvxListView
包裹在ScrollView
中。 MvxListView
将处理滚动本身。
<?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:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:lines="1"
android:textSize="30dp"
local:MvxBind="Text titleToSearch" />
<ImageButton
android:id="@+id/button1"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/icon"
local:MvxBind="Click SearchTitle" />
</LinearLayout>
<Mvx.MvxListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
local:MvxBind="ItemsSource results"
local:MvxItemTemplate="@layout/listitem" />
</LinearLayout>
注意:对于您的命令,您可以使用MvxAsyncCommand
来简化代码。如果需要,也可以异步执行命令。
public IMvxAsyncCommand SearchTitle
{
get { return new MvxAsyncCommand(SearchAsync); }
}