2016-11-22 45 views
3

當我在調試模式下建設時,一切正常。在Release中構建時,我的MvxListView不會被填充。MVVMCross發佈版本無法正常工作(LinkerPleaseInclude Listview)

這與鏈接器和MvvmCross做反射魔術有關,因此鏈接器無法知道將綁定鏈接到哪裏。

他們說有一個名爲「LinkerPleaseInclude.cs」的文件來幫助僞造綁定作爲其引用。

不知怎的,我的列表視圖仍然沒有得到填充..請幫我...

Linkerpleaseinclude文件:

class LinkerPleaseInclude 
{ 
    public void Include(ICommand command) 
    { 
     command.CanExecuteChanged += (s, e) => 
     { 
      if (command.CanExecute(null)) 
      { 
       command.Execute(null); 
      } 
     }; 
    } 

    public void Include(MvxListView listview) 
    { 
     listview.ItemsSource = new List<int>(); 
     var itemsSource = listview.ItemsSource; 
    } 

    public void Include(AnimalSearchViewModel viewmodel) 
    { 
     viewmodel.FilteredAnimals = new List<AnimalListInfoViewModel>(); 
    } 
} 

AnimalSearchViewModel

public class AnimalSearchViewModel : ViewModelBase 
{ 
    private string searchString; 
    private MvxCommand<AnimalListInfoViewModel> itemSelectedCommand; 

    private readonly IUserDialogs userDialogs; 
    private readonly IAnimalsStorage animalsStorage; 
    private readonly IMapper mapper; 
    private readonly IDebug logger; 

    public IEnumerable<Animal> Animals { get; set; } 
    public IList<AnimalListInfoViewModel> FilteredAnimals { get; set; } 

    public string SearchString 
    { 
     get 
     { 
      return this.searchString; 
     } 
     set 
     { 
      this.FindResults(value); 
     } 
    } 

    public IMvxCommand ItemSelectedCommand 
    { 
     get 
     { 
      this.itemSelectedCommand = this.itemSelectedCommand ?? new MvxCommand<AnimalListInfoViewModel>(this.DoSelectItem); 
      return this.itemSelectedCommand; 
     } 
    } 

    public AnimalSearchViewModel(
     IMvxMessenger messenger, 
     IUserDialogs dialogs, 
     IAnimalsStorage animalsStorage, 
     IMapper mapper, 
     IDebug logger) 
     : base(messenger, "Dierkaart") 
    { 
     this.userDialogs = dialogs; 
     this.animalsStorage = animalsStorage; 
     this.mapper = mapper; 
     this.logger = logger; 
    } 

    public void DoSelectItem(AnimalListInfoViewModel item) 
    { 
     this.ShowViewModel<AnimalListInfoViewModel>(new { id = item.Id }); 
     this.logger.LogInfo(DebugTag.Core, "Key: " + item.Key + " Value: " + item); 
    } 

    protected override async void InitFromBundle(IMvxBundle parameters) 
    { 
     this.Animals = await this.animalsStorage.GetAnimalsAsync(); 

     base.InitFromBundle(parameters); 
    } 

    private void FindResults(string keyword) 
    { 
     this.searchString = keyword; 
     if (this.searchString.Length >= 3) 
     { 
      var filteredAnimals = this.Animals.Where(i => 
                { 
                 // TODO: Get real displayvalue 
                 var displayValue = i.Key; 
                 return displayValue.IndexOf(this.searchString, StringComparison.OrdinalIgnoreCase) != -1; 
                }).ToArray(); 

      this.FilteredAnimals = this.mapper.Map<List<AnimalListInfoViewModel>>(filteredAnimals); 
     } 
     else 
     { 
      this.FilteredAnimals = new List<AnimalListInfoViewModel>(); 
     } 
    } 
} 

layoutfile:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:local="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/main_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:paddingTop="?android:attr/actionBarSize" 
    android:fitsSystemWindows="true"> 
    <EditText 
     android:id="@+id/search" 
     android:layout_width="fill_parent" 
     android:layout_height="@dimen/md_list_single_line_with_avatar_item_height" 
     android:paddingLeft="@dimen/md_list_item_horizontal_edges_padding" 
     android:paddingRight="@dimen/md_list_item_horizontal_edges_padding" 
     android:layout_alignParentTop="true" 
     android:drawableLeft="@android:drawable/ic_menu_search" 
     android:inputType="number" 
     android:singleLine="true" 
     android:hint="Type om te zoeken..." 
     local:MvxBind="Text SearchString"/> 
    <Mvx.MvxListView 
     android:id="@+id/select_list" 
     android:scrollbars="vertical" 
     android:layout_below="@id/search" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentBottom="true" 
     local:MvxBind="ItemsSource FilteredAnimals; ItemClick ItemSelectedCommand"/> 
</RelativeLayout> 

我可以想象ItemsSource FilteredAnimals會被填充,但這不是在發佈模式下工作..請幫助我。

+0

您是否在使用'Sdk Assemblies Only'或'Sdk and User Assemblies'選項進行鏈接? – Plac3Hold3r

+0

@ Plac3Hold3r SDK Assemblies Only :) – Baklap4

回答

4

我認爲問題不直接關係到你MvxListView而是文本改變你的EditText。如果輸入的值沒有返回到您的ViewModel,它將不會觸發FindResult(string keyword)並更新您的列表FilteredAnimals

您可以將AfterTextChanged事件添加到您的LinkerPleaseInclude以防止鏈接器將其刪除。

public class LinkerPleaseInclude 
{ 
    public void Include(TextView text) 
    { 
     text.AfterTextChanged += (sender, args) => text.Text = "" + text.Text; 
     text.Hint = "" + text.Hint; 
    } 
} 
+0

這是成功的關鍵!然而,我仍然想知道..我正在使用EditText作爲搜索欄..爲什麼我會使用包含到Textview? – Baklap4

+1

@ Baklap4,你也可以使用'EditText'。這只是因爲一個EditText繼承了一個TextView,所以在最底層的基類上進行鏈接可以保證所有繼承的類不會鏈接到屬性/事件。 – Plac3Hold3r

+0

感謝您的解釋! – Baklap4

0

我懷疑你在LinkerPleaseInclude代碼只產生於set訪問的ItemsSource屬性的參考。您也需要參考get以避免它被鏈接。

試試這個:

public void Include(MvxListView listview) 
{ 
    listview.ItemsSource = new List<int>(); 
    var itemsSource = listView.ItemsSource; 
} 
+0

即使包含了getter,我的列表仍然是空的.. – Baklap4

相關問題