【发布时间】:2017-06-24 21:41:53
【问题描述】:
在Xamarin Forms 中(使用Prism,虽然不一定需要),我想从ListView 中的项目动态更改MenuItem ContextActions:
MainPage.xaml
<ContentPage
x:Class="XamarinProject.Views.MainPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
Title="MainPage"
prism:ViewModelLocator.AutowireViewModel="True">
<StackLayout HorizontalOptions="Center" VerticalOptions="Center">
<ListView
x:Name="mList"
ItemsSource="{Binding Items}"
SelectedItem="{Binding SelectedItem}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.ContextActions>
<MenuItem Command="{Binding CurrentCommand}" Text="{Binding Cycle}" />
</ViewCell.ContextActions>
<Label Text="{Binding State}" TextColor="Navy" />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
MainPageViewModel.cs
public class MainPageViewModel : BindableBase, INavigationAware
{
public ObservableCollection<MyItemViewModel> Items { get; set; } = new ObservableCollection<MyItemViewModel>();
public void OnNavigatedTo(NavigationParameters parameters)
{
Items.Add(new MyItemViewModel(_pageDialogService));
}
}
MyItemViewModel
public class MyItemViewModel : BindableBase
{
private string _cycle;
public string Cycle
{
get { return _cycle; }
set { SetProperty(ref _cycle, value); }
}
private DelegateCommand _currentCommand;
private readonly DelegateCommand[] _commands;
public DelegateCommand CurrentCommand
{
get { return _currentCommand; }
set { SetProperty(ref _currentCommand, value); }
}
public MyItemViewModel(IPageDialogService dialogService)
{
_commands = new DelegateCommand[]
{
new DelegateCommand(show1),
new DelegateCommand(show2),
new DelegateCommand(show3)
};
show1();
}
private void show1()
{
show("1", 1, "I show 1");
}
private void show2()
{
show("2", 2, "I show 2");
}
private void show3()
{
show("3", 0, "I show 3");
}
private void show(string cycle, int newIndex, string message)
{
Cycle = cycle;
CurrentCommand = _commands[newIndex];
// dialogService available through Unity, it's available
_dialogService.DisplayAlertAsync("Alert", message, "Ok");
}
}
但是,在 iOS 上,当我滑动行时,我看到 ContextAction(s),然后勾选它,它会执行 Command,但 ContextActionMenu/row 不会自动向后滑动,因为它应该是.如果我将它绑定到单个DelegateCommand,而不动态地将其引用更改为另一个DelegateCommand,它就可以正常工作(就像在XAML 中这样:Command={Binding MyNonChangingCommand})。如何解决这个问题,以便该行自动“向后滑动”(就像它应该的那样)?或者它只是 Prism 或 Xamarin Forms 中的一个错误?
【问题讨论】:
标签: ios xaml xamarin xamarin.forms prism