MvvmCross - 使用 IMvxNavigationService 传递字符串

MvvmCross - Passing a string with IMvxNavigationService

我目前正在开展一个 Xamarin.iOS 项目,该项目使用 web-api 来收集数据。但是,我 运行 在尝试将用户输入从文本字段传递到从 api 获取结果的 Tableview 时遇到了一些问题。

为此,我遵循了 MvvmCross documentation 上的示例。

问题是来自文本字段的输入永远不会到达我的 TableviewController 视图模型中的 'Filter' 属性。我认为我在调用时没有将字符串对象正确传递到我的 IMvxNavigationService。

澄清一下,在我的 UserinputViewController 中,我像这样绑定文本字段的文本:

    [MvxFromStoryboard(StoryboardName = "Main")]
    public partial class SearchEventView : MvxViewController
    {
        public SearchEventView (IntPtr handle) : base (handle)
        {
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            MvxFluentBindingDescriptionSet<SearchEventView, SearchEventViewModel> set = new MvxFluentBindingDescriptionSet<SearchEventView, SearchEventViewModel>(this);

            set.Bind(btnSearch).To(vm => vm.SearchEventCommand);
            set.Bind(txtSearchFilter).For(s => s.Text).To(vm => vm.SearchFilter);
            set.Apply();
        }
      }

链接到此 ViewController 的视图模型如下所示:

public class SearchEventViewModel : MvxViewModel
{
    private readonly IMvxNavigationService _navigationService;  

    private string _searchFilter;

    public string SearchFilter
    {
        get { return _searchFilter; }
        set { _searchFilter = value; RaisePropertyChanged(() => SearchFilter); }
    }

    public SearchEventViewModel(IMvxNavigationService mvxNavigationService)
    {
        this._navigationService = mvxNavigationService;
    }

    public IMvxCommand SearchEventCommand {
        get {
            return new MvxCommand<string>(SearchEvent);
        }
    }

    private async void SearchEvent(string filter)
    {
        await _navigationService.Navigate<EventListViewModel, string>(filter);
    }
}

最后,TableviewController 的视图模型如下所示:

public class EventListViewModel : MvxViewModel<string>
{
    private readonly ITicketMasterService _ticketMasterService;
    private readonly IMvxNavigationService _navigationService;
    private List<Event> _events;

    public List<Event> Events
    {
        get { return _events; }
        set { _events = value; RaisePropertyChanged(() => Events); }
    }

    private string _filter;

    public string Filter
    {
        get { return _filter; }
        set { _filter = value; RaisePropertyChanged(() => Filter); }
    }


    public EventListViewModel(ITicketMasterService ticketMasterService, IMvxNavigationService mvxNavigationService)
    {
        this._ticketMasterService = ticketMasterService;
        this._navigationService = mvxNavigationService;
    }

    public IMvxCommand EventDetailCommand {
        get {
            return new MvxCommand<Event>(EventDetail);
        }
    }

    private void EventDetail(Event detailEvent)
    {   
        _navigationService.Navigate<EventDetailViewModel, Event>(detailEvent);
    }

    public override void Prepare(string parameter)
    {
        this.Filter = parameter;
    }

    public override async Task Initialize()
    {
        await base.Initialize();
        //Do heavy work and data loading here
        this.Events = await _ticketMasterService.GetEvents(Filter);
    }
}

每当尝试 运行 时,我的 TableviewController 的 Prepare 函数中的字符串对象 'parameter' 仍然是 'null',我不知道如何修复它。非常感谢任何帮助!

我认为问题出在您的命令设置上

new MvxCommand<string>(SearchEvent);

因为这个命令被绑定到一个标准的 UIButton。它不会传递过滤器的参数值,而是传递 null。所以字符串参数generic可以去掉。此外,由于您想执行异步方法,我建议您使用 MvxAsyncCommand

new MvxAsyncCommand(SearchEvent);

然后在SearchEvent方法中你可以去掉这个参数。 filter 的值绑定到你的 SearchFilter 属性。您要将此 属性 的值作为导航参数发送。

private async Task SearchEvent()
{
    await _navigationService.Navigate<EventListViewModel, string>(SearchFilter);
}