【问题标题】:MVVMCross Release build not working (LinkerPleaseInclude Listview)MVVMCross Release 构建不起作用(LinkerPleaseInclude Listview)
【发布时间】:2016-11-22 09:32:28
【问题描述】:

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

这与链接器和 MvvmCross 做反射魔术有关,因此链接器无法知道将绑定链接到哪里。

他们说有一个名为“LinkerPleaseInclude.cs”的文件可以帮助伪造绑定,就像它当时引用的那样。

不知何故我的列表视图仍然没有被填充..请帮帮我...

链接器请包含文件:

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>();
        }
    }
}

布局文件:

<?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 将被填充,但在发布模式下不起作用..请帮帮我。

【问题讨论】:

  • 您是否使用Sdk Assemblies OnlySdk and User Assemblies 选项进行链接?
  • @Plac3Hold3r 仅限 SDK 程序集 :)

标签: c# android xamarin linker mvvmcross


【解决方案1】:

我认为该问题与您的 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;
    }
}

【讨论】:

  • 这是成功的关键!但我仍然想知道.. 我使用 EditText 作为搜索栏.. 为什么我要使用包含到 Textview?
  • @Baklap4,您也可以使用EditText。只是因为EditText 继承和TextView,所以链接到最低基类将保证不会为所有继承类链接属性/事件。
  • 感谢您的解释!
【解决方案2】:

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

试试这个:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多