我在此行的HomeViewCell.xamlg.cs中收到以下异常(类型为'Xamarin.Forms.ViewCell'的对象无法转换为类型'Xamarin.Forms.View')代码: global :: Xamarin.Forms.Xaml.Extensions.LoadFromXaml(this,typeof(HomeViewCell));
基本上,我有
ViewCell
ContentView
ContentView.Content
Grid
GridRows
GridColumns
ViewCell // Feature ViewCell
ViewCell.View
Label (for testing only)
ViewCell.View
ViewCell
... (8 More ViewCells, same as above) // FeedItems ViewCell
ViewCell. // Other ViewCell
ViewCell.View
Label (for testing only)
ViewCell.View
ViewCell
Grid
ContentView.Content
ContentView
ViewCell
使用Xamarin Forms,我试图创建一个ListView ItemTemplate,其中ListView绑定到包含GroupedFeedItem模型列表的ObservableCollection(GroupedFeedItemList)。 ListView中的每一行都显示GroupedFeedItem
GroupedFeedItem
{
FeatureItem {get; set; }
FeedList1-8 { get; set; } // 6 items
OtherFeedList1-2 { get; set; } // 2 items
}
ObservableCollection<GroupedFeedItem> GroupedFeedItemList
ListView Item看起来像这样...
-------------------------------------------------------|
|
|
| FEATURE
|
|
|
|------------------------------------------------------|
| FEED1
|------------------------------------------------------|
| FEED2
|------------------------------------------------------|
| ...
|------------------------------------------------------|
| FEED4
|------------------------------------------------------|
| FEED8
|------------------------------------------------------|
|
| OTHER FEED
-------------------------------------------------------|
HomeViewCell.xaml
<?xml version="1.0" encoding="UTF-8"?>
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApp.CustomCells.HomeViewCell"
xmlns:local="clr-namespace:MyApp;assembly=MyApp"
xmlns:commonConverters="clr-namespace:MyApp.Common.Converters;assembly=MyApp.Common">
<ContentView>
<ContentView.Content>
<Grid x:Name="contentGrid" HorizontalOptions="FillAndExpand" Padding="0" VerticalOptions="FillAndExpand" >
<Grid.Resources>
<ResourceDictionary>
<commonConverters:ImageIsVisibleConverter x:Key="ImageIsVisibleConverter"/>
<commonConverters:TimeAgoConverter x:Key="TimeAgoConverter"/>
<commonConverters:TextColorConverter x:Key="TextColorConverter"/>
</ResourceDictionary>
</Grid.Resources>
<Grid.GestureRecognizers>
<TapGestureRecognizer Tapped="HomeViewCell_OnTap" NumberOfTapsRequired="1" />
</Grid.GestureRecognizers>
<Grid.RowDefinitions>
<RowDefinition x:Name="featureGridRow"/>
<RowDefinition x:Name="feedItemOneGridRow"/>
<RowDefinition x:Name="feedItemTwoGridRow"/>
<RowDefinition x:Name="feedItemThreeGridRow"/>
<RowDefinition x:Name="feedItemFourGridRow"/>
<RowDefinition x:Name="feedItemFiveGridRow"/>
<RowDefinition x:Name="feedItemSixGridRow"/>
<RowDefinition x:Name="feedItemSevenGridRow"/>
<RowDefinition x:Name="feedItemEightGridRow"/>
<RowDefinition x:Name="otherGridRow"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="columnOne" Width="*" />
<ColumnDefinition x:Name="columnTwo" Width="*" />
<ColumnDefinition x:Name="columnThree" Width="*" />
<ColumnDefinition x:Name="columnFour" Width="*" />
<ColumnDefinition x:Name="columnFive" Width="*" />
<ColumnDefinition x:Name="columnSix" Width="*" />
</Grid.ColumnDefinitions>
<ViewCell x:Name="featureViewCell">
<ViewCell.View>
<Label Text="FEATURE CELL HERE" />
</ViewCell.View>
</ViewCell>
<ViewCell x:Name="feedItemOneViewCell">
<ViewCell.View>
<Label Text="FEED CELL ONE HERE" />
</ViewCell.View>
</ViewCell>
<ViewCell x:Name="feedItemTwoViewCell">
<ViewCell.View>
<Label Text="FEED CELL TWO HERE" />
</ViewCell.View>
</ViewCell>
<ViewCell x:Name="feedItemThreeViewCell">
<ViewCell.View>
<Label Text="FEED CELL THREE HERE" />
</ViewCell.View>
</ViewCell>
<ViewCell x:Name="feedItemFourViewCell">
<ViewCell.View>
<Label Text="FEED CELL FOUR HERE" />
</ViewCell.View>
</ViewCell>
<ViewCell x:Name="feedItemFiveViewCell">
<ViewCell.View>
<Label Text="FEED CELL FIVE HERE" />
</ViewCell.View>
</ViewCell>
<ViewCell x:Name="feedItemSixViewCell">
<ViewCell.View>
<Label Text="FEED CELL SIX HERE" />
</ViewCell.View>
</ViewCell>
<ViewCell x:Name="feedItemSevenViewCell">
<ViewCell.View>
<Label Text="FEED CELL SEVEN HERE" />
</ViewCell.View>
</ViewCell>
<ViewCell x:Name="feedItemEightViewCell">
<ViewCell.View>
<Label Text="FEED CELL EIGHT HERE" />
</ViewCell.View>
</ViewCell>
<ViewCell x:Name="otherViewCell">
<ViewCell.View>
<Label Text="OTHER FEED CELL HERE" />
</ViewCell.View>
</ViewCell>
</Grid>
</ContentView.Content>
</ContentView>
</ViewCell>
HomeViewCell.xaml.cs
public partial class HomeViewCell : ViewCell
{
private XXX _xxx = null;
public HomeViewCell(XXX xxx)
{
InitializeComponent();
_xxx = xxx;
SetGridRowHeight();
SetViewCellLocationsInGrid();
}
bool isLandscape()
{
if (this.View.Height > this.View.Width) // Portrait OR 0
{
return false;
}
else // Landscape
{
return true;
}
}
void SetGridRowHeight()
{
GridLength featureGridRowHeight = (Device.Idiom == TargetIdiom.Phone)
? new GridLength(300)
: new GridLength(500);
GridLength feedItemGridRowHeight = (Device.Idiom == TargetIdiom.Phone)
? new GridLength(150)
: new GridLength(300);
featureGridRow.Height = featureGridRowHeight;
feedItemOneGridRow.Height = feedItemGridRowHeight;
feedItemTwoGridRow.Height = feedItemGridRowHeight;
feedItemThreeGridRow.Height = feedItemGridRowHeight;
feedItemFourGridRow.Height = feedItemGridRowHeight;
feedItemFiveGridRow.Height = feedItemGridRowHeight;
feedItemSixGridRow.Height = feedItemGridRowHeight;
feedItemSevenGridRow.Height = feedItemGridRowHeight;
feedItemEightGridRow.Height = feedItemGridRowHeight;
twitterGridRow.Height = (Device.Idiom == TargetIdiom.Phone)
? new GridLength(100)
: new GridLength(200);
}
void SetViewCellLocationsInGrid()
{
int featureColumnSpan = (Device.Idiom == TargetIdiom.Phone)
? 6
: isLandscape() // Tablet
? 4
: 6;
int feedItemColumnSpan = (Device.Idiom == TargetIdiom.Phone)
? 6
: 3;
// featureViewCell
Grid.SetRow(featureViewCell, 0);
Grid.SetRowSpan(featureViewCell, 0);
Grid.SetColumn(featureViewCell, 0);
Grid.SetColumnSpan(featureViewCell, featureColumnSpan);
// feedItemOneViewCell
Grid.SetRow(feedItemOneViewCell, 1);
Grid.SetRowSpan(feedItemOneViewCell, 1);
Grid.SetColumn(feedItemOneViewCell, 0);
Grid.SetColumnSpan(feedItemOneViewCell, feedItemColumnSpan);
// feedItemTwoViewCell
Grid.SetRow(feedItemTwoViewCell, 2);
Grid.SetRowSpan(feedItemTwoViewCell, 1);
Grid.SetColumn(feedItemTwoViewCell, 0);
Grid.SetColumnSpan(feedItemTwoViewCell, feedItemColumnSpan);
// feedItemThreeViewCell
Grid.SetRow(feedItemThreeViewCell, 3);
Grid.SetRowSpan(feedItemThreeViewCell, 1);
Grid.SetColumn(feedItemThreeViewCell, 0);
Grid.SetColumnSpan(feedItemThreeViewCell, feedItemColumnSpan);
// feedItemFourViewCell
Grid.SetRow(feedItemFourViewCell, 4);
Grid.SetRowSpan(feedItemFourViewCell, 1);
Grid.SetColumn(feedItemFourViewCell, 0);
Grid.SetColumnSpan(feedItemFourViewCell, feedItemColumnSpan);
// feedItemFiveViewCell
Grid.SetRow(feedItemFiveViewCell, 5);
Grid.SetRowSpan(feedItemFiveViewCell, 1);
Grid.SetColumn(feedItemFiveViewCell, 0);
Grid.SetColumnSpan(feedItemFiveViewCell, feedItemColumnSpan);
// feedItemSixViewCell
Grid.SetRow(feedItemSixViewCell, 6);
Grid.SetRowSpan(feedItemSixViewCell, 1);
Grid.SetColumn(feedItemSixViewCell, 0);
Grid.SetColumnSpan(feedItemSixViewCell, feedItemColumnSpan);
// feedItemSevenViewCell
Grid.SetRow(feedItemSevenViewCell, 7);
Grid.SetRowSpan(feedItemSevenViewCell, 1);
Grid.SetColumn(feedItemSevenViewCell, 0);
Grid.SetColumnSpan(feedItemSevenViewCell, feedItemColumnSpan);
// feedItemEightViewCell
Grid.SetRow(feedItemEightViewCell, 8);
Grid.SetRowSpan(feedItemEightViewCell, 1);
Grid.SetColumn(feedItemEightViewCell, 0);
Grid.SetColumnSpan(feedItemEightViewCell, feedItemColumnSpan);
// otherViewCell
Grid.SetRow(otherViewCell, 9);
Grid.SetRowSpan(otherViewCell, 1);
Grid.SetColumn(otherViewCell, 0);
Grid.SetColumnSpan(otherViewCell, 6);
}
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
}
public void HomeViewCell_OnTap(object sender, System.EventArgs e)
{
}
}
HomeListItemTemplateSelector.cs
public class HomeListItemTemplateSelector : DataTemplateSelector
{
private readonly XXX _xxx;
private readonly DataTemplate _featureTemplate;
private readonly DataTemplate _feed1Template;
private readonly DataTemplate _feed2Template;
private readonly DataTemplate _feed3Template;
private readonly DataTemplate _feed4Template;
private readonly DataTemplate _feed5Template;
private readonly DataTemplate _homeViewTemplate;
private readonly DataTemplate _feed6Template;
private readonly DataTemplate _feed7Template;
private readonly DataTemplate _otherTemplate;
public HomeListItemTemplateSelector(XXX xxx)
{
_xxx = xxx;
this._homeViewTemplate = new DataTemplate(() => new HomeViewCell(_xxx));
this._featureTemplate = new DataTemplate(() => new FeatureViewCell(_xxx));
this._feed1Template = new DataTemplate(() => new Feed1ViewCell(_xxx));
this._feed2Template = new DataTemplate(() => new Feed2ViewCell(_xxx));
this._feed3Template = new DataTemplate(() => new Feed3ViewCell(_xxx));
this._feed4Template = new DataTemplate(() => new Feed4ViewCell(_xxx));
this._feed5Template = new DataTemplate(() => new Feed5ViewCell(_xxx));
this._feed6Template = new DataTemplate(() => new Feed6ViewCell(_xxx));
this._feed7Template = new DataTemplate(() => new Feed7ViewCell(_xxx));
this._otherTemplate = new DataTemplate(() => new OtherViewCell(_xxx));
}
DataTemplate SelectViewType(ContentTypeEnum contentType)
{
switch (contentType)
{
case ContentTypeEnum.Home:
return this._homeViewTemplate;
case ContentTypeEnum.Feature:
return this._featureTemplate;
case ContentTypeEnum.Feed1:
return this._feed1Template;
case ContentTypeEnum.Feed2:
return this._feed2Template;
case ContentTypeEnum.Feed3:
return this._feed3Template;
case ContentTypeEnum.Feed4:
return this._feed4Template;
case ContentTypeEnum.Feed5:
return this._feed5Template;
case ContentTypeEnum.Feed6:
return this._feed6Template;
case ContentTypeEnum.Feed7:
return this._feed7Template;
case ContentTypeEnum.OtherFeed:
return this._otherTemplate;
}
throw new NotImplementedException("Content type not implemented");
}
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
...
}
}