2017-02-09 32 views
0

我使用mvvm和viewmodel locator.Im使用按钮命令或listview itemtap行为没有问题。但在我的页面之一,我需要使用外部itemtemplate(资源)。在此模板中,我可以绑定标签没有问题。但我不能绑定按钮的命令,我得到这个错误“无法解析元素上的名称”。xamarin.forms listview的外部项目模板中的命令绑定?

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> 

,并在视图模型

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()); 
    } 
+0

如果你提供样本项目会更容易迅速繁殖并尝试修复 –

+0

抱歉迟了回应,我重现此干净简单的项目,你可以快速地从这里看HTTPS://www.dropbox。 com/s/0yesmmncbppwd3d/testapp.rar?dl = 0 – slayer35

回答

1

你的问题是在哪里定义您的处理程序。您在PageTestViewModel中定义了它,但在xaml中,此命令未针对列表或页面模型进行定义。它是为ITEM定义的。所以,你有3个选项。您不必同时定义OnButtonClicked和TestCommand,我只是将它显示为一个选项。如果您同时定义,你会得到所谓的在两个地方(见下文)

<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个处理程序中的一个,并订阅将由这些处理程序启动的事件。

+0

感谢Yuri,我想从viewmodel调用,尝试使用路径,但无法成功it.Because当我给路径(源= {x:参考名称= mylistView})但它会给出错误,因为listview在页面中,并且viewcell在资源中。我使用第二种方法表示感谢。感谢您的帮助。 – slayer35

+0

你能否描述第三种解决方案?我有同样的问题,我想绑定命令按钮,这是在数据模板中的另一个文件的ListView和命令在ViewModel中定义。 – kaktusas2598

+0

@ kaktusas2598你必须使用绑定路径 –

相关问题