MVVMCross Release构建不起作用(LinkerPleaseInclude Listview)

时间:2016-11-22 09:32:28

标签: c# android xamarin linker mvvmcross

当我在调试模式下构建时,一切正常。在Release中构建时,我的MvxListView不会被填充。

这与链接器和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将被填充,但是在发布模式下无效..请帮帮我。

2 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:0)

我怀疑LinkerPleaseInclude中的代码只生成对set属性的ItemsSource访问者的引用。您还需要引用get以避免被链接出来。

试试这个:

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