我使用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());
}
如果你提供樣本項目會更容易迅速繁殖並嘗試修復 –
抱歉遲了迴應,我重現此乾淨簡單的項目,你可以快速地從這裏看HTTPS://www.dropbox。 com/s/0yesmmncbppwd3d/testapp.rar?dl = 0 – slayer35