2017-02-09 106 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你必須使用綁定路徑 –

相關問題