【问题标题】:xamarin forms command issuexamarin 表单命令问题
【发布时间】:2018-09-27 11:28:58
【问题描述】:

我有一个奇怪的问题 我在我的 xamarin 表单应用程序上使用 Prism。

有一个命令可以将用户导航到特定页面。 XAML 代码如下

<ContentPage.ToolbarItems>
    <ToolbarItem Icon="add_circle_white_48dp.png" Command="{Binding NavigateToPage}" CommandParameter="Asset" />
    <ToolbarItem Text="Detay" Command="{Binding NavigateToPage}" CommandParameter="AssetTabbed" />
</ContentPage.ToolbarItems>

此 NavigateToPage 命令在这里工作正常,但如果我想在列表视图视图单元 ContextActions 中添加此命令,如下所示

                            <ViewCell.ContextActions>
                                <MenuItem Text="Detay" IsDestructive="False"
                                          Command="{Binding NavigateToPage}" CommandParameter="AssetTabbed" />
                            </ViewCell.ContextActions>

命令无效

【问题讨论】:

    标签: listview xamarin.forms command


    【解决方案1】:

    ViewCell 具有不同的绑定上下文。您需要这样做:

     <ListView ItemsSource="{Binding Collection}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Button Command="{Binding BindingContext.YourCommand,Source={x:Reference page}}"
                                CommandParameter="{Binding .}" />
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    
    
    public class YourPageViewModel : BindableBase
    {
        public YourPageViewModel()
        {
            YourCommand = new DelegateCommand<MyModel>( ExecuteMyCommand );
        }
    
        public ObservableCollection<YourModel> Collection { get; set; }
    
        public DelegateCommand<YourModel> YourCommand { get; }
    
        private void ExecuteYourCommand( YourModel model )
        {
            // Your Logic
        }
    

    【讨论】:

    • 这不起作用。 Source={x:Reference page} 应该是 ListView 的名称而不是 page
    猜你喜欢
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    • 2018-07-11
    • 2020-02-07
    相关资源
    最近更新 更多