MVVMSCROSS MVXBindablEListView内のViewModelを変更します
-
12-12-2019 - |
質問
Androidアプリケーションのほとんど問題で、私はMVVMクロスでそれを解決する方法がわかりません。
これは私のモデルです
public class Article
{
string Label{ get; set; }
string Remark { get; set; }
}
.
私のViewModel
public class ArticleViewModel: MvxViewModel
{
public List<Article> Articles;
....
}
.
私のlayout.axml. ...
<LinearLayout
android:layout_width="0dip"
android:layout_weight="6"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/layoutArticleList">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editSearch"
android:text=""
android:singleLine="True"
android:selectAllOnFocus="true"
android:capitalize="characters"
android:drawableLeft="@drawable/ic_search_24"
local:MvxBind="{'Text':{'Path':'Filter','Mode':'TwoWay'}}"
/>
<Mvx.MvxBindableListView
android:id="@+id/listviewArticle"
android:choiceMode="singleChoice"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
local:MvxItemTemplate="@layout/article_rowlayout"
local:MvxBind="{'ItemsSource':{'Path':'Articles'}}" />
</LinearLayout>
...
.
そしてここに私の問題があり、 "Artical_rowlayout"
...
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/blue">
<TextView
android:id="@+id/rowArticleLabel"
android:layout_width="0dip"
android:layout_weight="14"
android:layout_height="wrap_content"
android:textSize="28dip"
local:MvxBind="{'Text':{'Path':'Label'}}" />
<ImageButton
android:src="@drawable/ic_modify"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/rowArticleButtonModify"
android:background="@null"
android:focusable="false"
android:clickable="true"
local:MvxBind="{'Click':{'Path':'MyTest'}}"
/>
...
.
"mytest"という "Click"コマンドは、MVxBindablEListViewによって指定された項目にリンクされています。つまり、ViewModelではなく、My Model "記事"のコマンド "mytest"の検索をクリックします。MVXBindablEListViewを担当するMy ViewModelの "ArticalViewModel"をリンクするために、その動作を変更するにはどうすればよいですか?
任意の提案?
解決
あなたの分析は、クリックイベントがバインドしようとしている場所について間違いなく正しいです。
一般的に2つのアプローチがあります:
- list の項目を使う
- クリックを継続して、ビューモデルサイドにリダイレクトを実行します。
だから... 1
チュートリアルのメインメニュービューモーデルがビットモーデルを持っています。
.public class MainMenuViewModel : MvxViewModel { public List<T> Items { get; set; } public IMvxCommand ShowItemCommand { get { return new MvxRelayCommand<T>((item) => /* do action with item */ ); } } }
これはAXMLで使用されています。
.<Mvx.MvxBindableListView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:local="http://schemas.android.com/apk/res/Tutorial.UI.Droid" android:layout_width="fill_parent" android:layout_height="fill_parent" local:MvxBind="{'ItemsSource':{'Path':'Items'},'ItemClick':{'Path':'ShowItemCommand'}}" local:MvxItemTemplate="@layout/listitem_viewmodel" />
このアプローチは、リスト項目全体での項目Clickでのみ行うことができます - リスト項目内の個々のサブビューではありません。
または... 2
MVXに
RelativeSource
バインディング命令がありませんので、このタイプのリダイレクトはViewModel / Modelコードで行うことができます。これは、モデルオブジェクト自体ではなく、モデルオブジェクトの動作対応のラッパーを提示することによって行うことができます。
List<ActiveArticle>
を使用して:
.public ActiveArticle { Article _article; ArticleViewModel _parent; public WrappedArticle(Article article, ArticleViewModel parent) { /* assignment */ } public IMvxCommand TheCommand { get { return MvxRelayCommand(() -> _parent.DoStuff(_article)); } } public Article TheArticle { get { return _article; } } }
あなたのAXMLは、次のようなバインディングを使用する必要があります。
.<TextView ... local:MvxBind="{'Text':{'Path':'TheArticle.Label'}}" />
と
.<ImageButton ... local:MvxBind="{'Click':{'Path':'TheCommand.MyTest'}}" />
このアプローチの一例は、 withコマンド
しかしながら、
WithCommand<T>
を使用する場合、メモリリークを発見した場合 - 基本的にGarbageCollectionが埋め込まれたMvxRelayCommand
の収集を拒否しました。スロッジ/ mvvmcross / blob / master / sample / sample.curious.confore.core.core / beabeSessionListViewModel.cs "rel=" noreferrer "> basesessionListViewModel ビューのときにIntemMand要素を解除します。切り離されています。
コメント後の更新:
データリストが大きい場合 - あなたのデータが修正されています(あなたの記事はPropertyChangedのないモデルです)、大きな
WithCommand<T>
を作成するオーバーヘッドを獲得したくない場合は、これを回避する1つの方法はIDisposable
クラスを使用することです。 。これは、Microsoftコードで取られたアプローチと非常によく似ています。 WP7 / Silverlightの仮想化リストで - http://shawnoster.com/blog/post/improving-ListBox-performance-in-Silverlight-Silverlight-For-Windows-phone-7-Data-virtualization.aspx