2016-08-12 25 views
1

我在iOS中有一個TableView,在我的ViewModel中,我有一個屬性到TableView中的選定項目,但我不知道如何綁定此屬性的選定項目。我怎樣才能做到這一點?我的項目是跨平臺的。我有一個Android項目和一個iOS項目。在Android項目中,我做了綁定:MvvmCross - 如何綁定到iOS TableView中的SelectedItem?

<Mvx.MvxListView 
        android:id="@+id/lstViewTasks" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:clickable="true" 
        android:focusableInTouchMode="true" 
        android:choiceMode="multipleChoice" 
        local:MvxBind="ItemsSource Tasks; SelectedItem SelectedTask; ItemClick ShowTaskCommand" 
        local:MvxItemTemplate="@layout/projectmytasksitem" /> 

但我無法在iOS中執行等效綁定。

這是我TableViewController:

[Register("ProjectMyTasksViewc")] 
public class ProjectMyTasksViews : MvxTableViewController<ProjectMyTasksViewModel> 
{ 
     //other things 

     var source = new MvxSimpleTableViewSource(TableView, ProjectMyTasksItem.Key, ProjectMyTasksItem.Key); 
     TableView.Source = source; 

     this.CreateBinding(source).To<ProjectMyTasksViewModel>(viewModel => viewModel.Tasks).Apply(); 
     this.CreateBinding(source).For(s => s.SelectedItem).To<ProjectMyTasksViewModel>(viewModel => viewModel.SelectedTask).Apply(); 
     this.CreateBinding(source).For(tableSource => tableSource.SelectionChangedCommand).To<ProjectMyTasksViewModel>(viewModel => viewModel.ShowTaskCommand).Apply(); 

} 

這裏是我的ViewModel:

public class ProjectMyTasksViewModel : MvxViewModel 
{ 

public Action ShowTaskCommandAction { get; set; } 

private IList<Task> _tasks; 
public IList<Task> Tasks 
{ 
    get { return _tasks; } 
    set { _tasks = value; RaisePropertyChanged(() => Tasks); } 
} 

private Task _selectedTask; 
public Task SelectedTask 
{ 
    get { return _selectedTask; } 
    set { _selectedTask = value; RaisePropertyChanged(() => SelectedTask); } 
} 

private MvxCommand _showTaskCommand; 
public MvxCommand ShowTaskCommand 
{ 
    get 
    { 
     _showTaskCommand = _showTaskCommand ?? (_showTaskCommand = new MvxCommand(ExecuteShowTaskCommand)); 
     return _showTaskCommand; 
    } 
} 

private void ExecuteShowTaskCommand() 
{ 
    if (!SelectedTask.IsCompleted) 
    { 
     ShowTaskCommandAction?.Invoke(); 
    } 
} 
} 
+0

您在使用'SelectedItem'綁定時遇到了什麼問題?任何編譯或運行時錯誤?或者'SelectedTask'集合剛剛被擊中? – Plac3Hold3r

+0

我有一個NullReferenceException。 ViewModel中的SelectedTask屬性爲null。 –

+0

請問您可以使用'SelectedItem','ShowTaskCommand'以及NullReferenceException發生的地方包含ViewModel的一部分嗎? – Plac3Hold3r

回答

0

我相信這與你的ShowTaskCommand的獲取執行VS集SelectedTask時機做。因此,如果您註釋掉ExecuteShowTaskCommand中的代碼並在ExecuteShowTaskCommand以及SelectedTask的集合中放置斷點,您會發現ExecuteShowTaskCommand正在運行,然後是SelectedTask的集合。


替代實現

爲了避免計時問題,你可以選擇任務,而不是傳遞到您的命令作爲參數。

MvxCommand<Task> _showTaskCommand; 
public MvxCommand<Task> ShowTaskCommand => 
    _showTaskCommand ?? (_showTaskCommand = new MvxCommand<Task>(ExecuteShowTaskCommand)); 

private void ExecuteShowTaskCommand(Task selectedTask) 
{ 
    if (!selectedTask.IsCompleted) 
    { 
     ShowTaskCommandAction?.Invoke(); 
    } 
}