命令绑定在xamarin.forms listview的外部itemtemplate中?

时间:2017-02-09 12:52:47

标签: c# listview mvvm xamarin.forms

我正在使用mvvm和viewmodel locator.Im使用按钮命令或listview itemtap行为没有问题。但在我的一个页面中我需要使用外部itemtemplate(资源)。在这个模板中我可以绑定标签没有问题。但是我无法绑定命令按钮,我收到此错误"无法解析Element"

here is the external custom cell


<ViewCell xmlns="http://xamarin.com/schemas/2014/forms"
              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
              xmlns:core="clr-namespace:paux;assembly=paux"
              xmlns:controls="clr-namespace:paux.Controls;assembly=paux"
               xmlns:xlabs="clr-namespace:XLabs.Forms.Controls;assembly=XLabs.Forms"
              xmlns:base="clr-namespace:paux.Base;assembly=paux"   
              x:Class="paux.Controls.Cells.CustomDonemCell">   <ViewCell.View>
          <Grid  
            BackgroundColor="{StaticResource WhiteColor}" Margin="0,0,0,0">
          <Grid  Grid.Column="1"  Grid.Row="0">
            <Grid.RowDefinitions>
              <RowDefinition Height="Auto" />
              <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Grid    Grid.Row="0">
              <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
              </Grid.ColumnDefinitions>
              <StackLayout
                  Grid.Column="1"
                  Margin="0,16,0,0"
                  Orientation="Vertical"
                  Spacing="0"
                  VerticalOptions="Start">    -           
                    <Button Command="{Binding Path=BindingContext.mybuttonClicked, Source={x:Reference Name=mylistView}}"   CommandParameter="{Binding id}"    Text="My Button"/>
                <controls:MultiLineLabel Text="{Binding BolumAdi}"    Lines="2"             VerticalOptions="Center" HorizontalOptions="Center"             LineBreakMode="TailTruncation"
                 Margin="0,0,0,3"/>        
                </StackLayout>

            </Grid>
          </Grid>
        </Grid>   </ViewCell.View> </ViewCell>

这是带有templateselector的页面(工作正常)

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:behavior="clr-namespace:paux.Behavior;assembly=paux"
                  xmlns:animations="clr-namespace:paux.Animations;assembly=paux"
                  xmlns:triggers="clr-namespace:paux.Triggers;assembly=paux"
                 xmlns:effects="clr-namespace:paux.Effects;assembly=paux"
                 xmlns:templateSelectors="clr-namespace:paux.TemplateSelectors;assembly=paux"    
                 xmlns:converters="clr-namespace:paux.Converters;assembly=paux"
                 x:Class="paux.Pages.PageOgrenciDonem" >
  <ContentPage.Resources>
    <ResourceDictionary>
      <templateSelectors:DataTemplateSelector x:Key="ogrenciDonemTemplate" />  
    </ResourceDictionary>  
  </ContentPage.Resources>
  <Grid>
    <ListView
        x:Name="mylistView"
        CachingStrategy="RecycleElement"
        ItemsSource="{Binding OgrencilikList, Mode=OneWay}"
        HasUnevenRows="True"
        SeparatorVisibility="None"
        ItemTemplate="{StaticResource ogrenciDonemTemplate}" >
      <ListView.Margin>
        <OnPlatform x:TypeArguments="Thickness"
                    Android="8"
                    WinPhone="8"/>
      </ListView.Margin>
    </ListView>
  </Grid>
</ContentPage>

并在viewmodel中

 public static readonly BindableProperty TestCommandProperty =
            BindableProperty.Create("TestCommand", typeof(ICommand), typeof(CustomDonemCell), null);
    public ICommand TestCommand => new Command<detay>(testclickevent);
    private async void testclickevent(detay item)
    {  await NavigationService.NavigateToAsync<detayviewmodel(item.id.ToString());
    }

1 个答案:

答案 0 :(得分:1)

您遇到的问题是定义处理程序的位置。您在PageTestViewModel中定义了它,但是在xaml中没有为列表或页面模型定义此命令。它是为ITEM定义的。所以,你有3个选择。您不必定义OnButtonClicked和TestCommand,我只是将其显示为一个选项。如果你定义了两个,你将在2个地方被调用(见下文)

<Button Clicked="OnButtonClicked" Command="{Binding  TestCommand}"   CommandParameter="{Binding id}"    Text="My Button"/>
  1. 通过OnButtonClicked调用

    public partial class CustomCell : ViewCell
    {
        public CustomCell()
        {
            InitializeComponent();
        }
    
        void OnButtonClicked(object sender, EventArgs args)
        {
    
        }
    
    }
    
  2. 在项目

    中调用
    public class testdata
        {
            public string id { get; set; }
    
            public Command TestCommand
            {
                get
                {
    
                    return new Command((o) =>
                    {
                        System.Diagnostics.Debug.WriteLine("Item " + o.ToString());
                    });
                }
            }
        }
    
  3. 要在要调用的PageTestViewModel中调用,您需要指定模型的路径。这更复杂。如果前两种方法不适合您,请给我发消息。但是,当您将ViewCell xaml放在单独的文件中时,这将是棘手的,因此您无法访问页面名称或列表名称。我不确定在单元格中指定处理程序是否是好的设计,它留在顶级模型中。您可能希望使用我建议的2个处理程序之一,并订阅将从这些处理程序中触发的事件。

相关问题