문제

은 문제가 내 안드로이드 응용 프로그램을 내가 알지 못하고 그것을 해결하는 방법과된 이 십자가입니다.

여기는 제 모델

public class Article 
{
    string Label{ get; set; }
    string Remark { get; set; }
}

내 뷰 모델

public class ArticleViewModel: MvxViewModel
{
    public List<Article> Articles;
    ....

}

나의 레이아웃이 있습니다.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>
...

그리고 여기에 내 문제의"article_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"연결된 품목에 의해 주어진 MvxBindableListView.즉,클릭 검색에 대한 명령"MyTest"내 모델에서"문서"대신 이 시점.방법을 변경할 수 있는 행동에 연결하기 위해 내 뷰 모델"ArticleViewModel"책임있는 나의 MvxBindableListView?

어떤 방법이 있나요?

도움이 되었습니까?

해결책

귀하의 분석은 확실히 올바른지에 대해 클릭하고자 하는 이벤트입하려고합니다.

두 가지 방법이 있습니다 내가 일반적으로:

  1. 사용 ItemClick 목록
  2. 를 계속 사용하여 클릭 하지만 일부 리다이렉션 뷰 모델에 측면이다.

그래서...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"
  />

이 방법에 대해서만 수행할 수 있습 ItemClick 에 전체 목록 항목하지 않는 개인에 하위 목록 내에서 항목입니다.


나 2...

이후 우리는 없 RelativeSource 바인딩을 지 mvx,이러한 유형의 리다이렉션에서 수행 할 수 있습 뷰 모델/모델 코드입니다.

이 수행할 수 있습을 제시하여 행동을 사용 래퍼의 모델이 아닌 객체 모델을 개체-예를 들어,를 사용하여 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'}}" />

이 접근 방법의 예는 회의 샘플을 사용하는 WithCommand

그러나...참고로 사용하는 경우 WithCommand<T> 우리가 발견되는 메모리 누수가 기본적으로 정 GarbageCollection 거부를 수집하는 임베디드 MvxRelayCommand -는 이유 WithCommand<T>IDisposable 그 이유 BaseSessionListViewModel 목록을 지우고 삭제합니다 WithCommand 요소할 때 전망이 되었습니다.


한 후 업데이트 코멘트:

데이터 목록은 큰과 데이터 고정(기사가 있는 모델의 없이 PropertyChanged)그리고 당신은 발생하지 않를 만드는 오버헤드 크 List<WrappedArticle> 다음 방법 중 하나 이 사용될 수 있는 WrappingList<T> 클래스입니다.

이것은 매우 비슷한 접근 방식에서 찍은 Microsoft 코드-예를 들어,에서 가상화 목록에 WP7/Silverlight- http://shawnoster.com/blog/post/Improving-ListBox-Performance-in-Silverlight-for-Windows-Phone-7-Data-Virtualization.aspx

에 대한 당신의 기사를 이 수 있습니다:

public class ArticleViewModel: MvxViewModel
{
    public WrappingList<Article> Articles;

    // normal members...
}

public class Article
{
    public string Label { get; set; }
    public string Remark { get; set; }
}

public class WrappingList<T> : IList<WrappingList<T>.Wrapped>
{
    public class Wrapped
    {
        public IMvxCommand Command1 { get; set; }
        public IMvxCommand Command2 { get; set; }
        public IMvxCommand Command3 { get; set; }
        public IMvxCommand Command4 { get; set; }
        public T TheItem { get; set; }
    }

    private readonly List<T> _realList;
    private readonly Action<T>[] _realAction1;
    private readonly Action<T>[] _realAction2;
    private readonly Action<T>[] _realAction3;
    private readonly Action<T>[] _realAction4;

    public WrappingList(List<T> realList, Action<T> realAction)
    {
        _realList = realList;
        _realAction = realAction;
    }

    private Wrapped Wrap(T item)
    {
        return new Wrapped()
            {
                Command1 = new MvxRelayCommand(() => _realAction1(item)),
                Command2 = new MvxRelayCommand(() => _realAction2(item)),
                Command3 = new MvxRelayCommand(() => _realAction3(item)),
                Command4 = new MvxRelayCommand(() => _realAction4(item)),
                TheItem = item
            };
    }

    #region Implementation of Key required methods

    public int Count { get { return _realList.Count; } }

    public Wrapped this[int index]
    {
        get { return Wrap(_realList[index]); }
        set { throw new NotImplementedException(); }
    }

    #endregion

    #region NonImplementation of other methods

    public IEnumerator<Wrapped> GetEnumerator()
    {
        throw new NotImplementedException();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    public void Add(Wrapped item)
    {
        throw new NotImplementedException();
    }

    public void Clear()
    {
        throw new NotImplementedException();
    }

    public bool Contains(Wrapped item)
    {
        throw new NotImplementedException();
    }

    public void CopyTo(Wrapped[] array, int arrayIndex)
    {
        throw new NotImplementedException();
    }

    public bool Remove(Wrapped item)
    {
        throw new NotImplementedException();
    }

    public bool IsReadOnly { get; private set; }

    #endregion

    #region Implementation of IList<DateFilter>

    public int IndexOf(Wrapped item)
    {
        throw new NotImplementedException();
    }

    public void Insert(int index, Wrapped item)
    {
        throw new NotImplementedException();
    }

    public void RemoveAt(int index)
    {
        throw new NotImplementedException();
    }

    #endregion
}   
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top