Xamarin Forms Carousel视图具有绑定问题

时间:2019-05-21 14:35:01

标签: xaml xamarin.forms

你好,我在将字符串列表绑定到“轮播视图”时遇到问题 首先,我有一个从服务器获取的对象的列表

public class PostObject
    {
        public string PostOwner { get; set; }
        public string Id { get; set; }
        public string Post { get; set; }
        public string ProfileImage { get; set; }
        public List<string> PostImages { get; set; }
    }
  List<PostObject> posts = new List<PostObject>();

这按预期工作。

接下来,我将创建一个名片视图,并且在该名片视图中我想要一个“轮播视图”。

所以我已经像这样设置了我的Xaml(为了清楚起见,省略了一些有用的部分)

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:controls="clr-namespace:ImageCircle.Forms.Plugin.Abstractions;assembly=ImageCircle.Forms.Plugin"
              NavigationPage.HasNavigationBar="True"
             NavigationPage.HasBackButton="False"
             NavigationPage.BackButtonTitle="Back"
             Title="amici"
             x:Class="amici.Posts">

    <NavigationPage.TitleView>
        <StackLayout Orientation="Horizontal" VerticalOptions="Center" Spacing="10" >
            <Label x:Name="GroupTitle" TextColor="White" FontSize="Medium"/>
        </StackLayout>
    </NavigationPage.TitleView>

    <ContentPage.ToolbarItems>
        <ToolbarItem Name="iconexample" Icon="settings.png" Priority="0" Order="Primary" />
    </ContentPage.ToolbarItems>

    <ContentPage.Content>
        <StackLayout>
            <ListView x:Name="ItemsListView" 
             VerticalOptions="FillAndExpand"
             HasUnevenRows="true"
             IsPullToRefreshEnabled="true"
             IsRefreshing="{Binding IsBusy, Mode=OneWay}"
             CachingStrategy="RecycleElement">
                <!--ItemSelected="OnItemSelected"-->
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell >
                            <StackLayout Padding="10">
                                <Frame x:Name="myframe" HasShadow="True" >

                                    <Grid HorizontalOptions="FillAndExpand"  RowSpacing="0" ColumnSpacing="0"  >
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="20" />
                                            <RowDefinition Height="Auto" />
                                            <RowDefinition Height="Auto" />
                                            <RowDefinition Height="Auto" />
                                            <RowDefinition Height="20" />
                                            <RowDefinition Height="20" />
                                            <RowDefinition Height="Auto" />
                                        </Grid.RowDefinitions>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto" />
                                        </Grid.ColumnDefinitions>
                                        <Label Grid.Row="0"  Text="{Binding PostOwner}" LineBreakMode="WordWrap" Font="Bold,16"  />
                                        <controls:CircleImage Grid.Row="0" Margin="10" BorderColor="white" BorderThickness="1" VerticalOptions="Start" HorizontalOptions="Start" Source="{Binding ProfileImage}" Aspect="AspectFit">
                                            <controls:CircleImage.WidthRequest>
                                                <OnPlatform x:TypeArguments="x:Double">
                                                    <On Platform="Android, iOS">65</On>
                                                </OnPlatform>
                                            </controls:CircleImage.WidthRequest>
                                            <controls:CircleImage.HeightRequest>
                                                <OnPlatform x:TypeArguments="x:Double">
                                                    <On Platform="Android, iOS">65</On>
                                                </OnPlatform>
                                            </controls:CircleImage.HeightRequest>
                                        </controls:CircleImage>

                                        <Label Grid.Row="1"  Text="{Binding Post}" LineBreakMode="WordWrap" Font="Bold,16"  />

                                        <CarouselView x:Name="PostImages" Grid.Row="2" ItemsSource="{Binding PostImages}">
                                            <CarouselView.ItemTemplate>
                                                <DataTemplate>
                                                    <Image Source="{Binding .}" Aspect="AspectFill" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
                                                </DataTemplate>
                                            </CarouselView.ItemTemplate>
                                        </CarouselView>

                                        <!--<Image Grid.Row="2"  Source="{Binding ImageURL}" Aspect="AspectFill" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />-->
                                        <BoxView Grid.Row="3"  BackgroundColor="black" HeightRequest="1"  HorizontalOptions="FillAndExpand"/>
                                        <StackLayout Grid.Row="4" Orientation="Horizontal" >
                                            <Label  Text="Likes: " LineBreakMode="NoWrap" Font="Bold,14" />
                                            <Label  Text="0" LineBreakMode="NoWrap" FontSize="14" />
                                        </StackLayout>
                                        <StackLayout Grid.Row="5" Orientation="Horizontal" >
                                            <Label Text="Comments: " LineBreakMode="NoWrap"  Font="Bold,14"  HorizontalOptions="End" />
                                            <Label Text="0" HorizontalOptions="End"  LineBreakMode="NoWrap" FontSize="14" />
                                        </StackLayout>
                                        <!--<Label Grid.Row="2" Text="{Binding OwnerFullName}" LineBreakMode="NoWrap" FontSize="16" />-->
                                    </Grid>
                                </Frame>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
         </StackLayout>


    </ContentPage.Content>
</ContentPage>

在我后面的代码中有这个

public Posts (GroupInfo ginfo)
        {
            InitializeComponent ();
            GroupTitle.Text = ginfo.Title;
            CurrentGroupInfo = ginfo;
            GetDataPosts();


            ItemsListView.RefreshCommand = new Command(() => {
                GetDataPosts();
                ItemsListView.IsRefreshing = false;

            });
        }

public void GetDataPosts()
        {
            try
            {
                string apikey = Application.Current.Properties["api"].ToString();
                ItemsListView.ItemsSource = null;
                posts.Clear();

                if (RestController.GetMyPostData(ref posts, CurrentGroupInfo.Id.ToString(), apikey))
                {
                    ItemsListView.ItemsSource = posts;
                }
            }
            catch(Exception e)
            {
                Debug.WriteLine(e.Message);
            }

        }

到目前为止,一切正常,没有错误,但是当应用程序渲染页面时出现错误

System.TypeInitializationException:“ Xamarin.Forms.ItemsView”的类型初始值设定项引发了异常。

我可以追溯到“轮播视图”。当我注释掉“轮播视图”后,它就起作用了。所以我想我不能以我认为的方式绑定或使用“轮播视图”?

2 个答案:

答案 0 :(得分:1)

CarouselView曾经是一个插件,现在已成为Xamarin.Forms的一部分,从版本4开始生效。您可以使用早期的XF版本,但需要获得此plugin,并向XAML添加程序集引用并将初始化代码放入您的平台特定项目中。

或者,您可以升级到Xamarin.Forms 4

答案 1 :(得分:0)

即使您可以在Xamarin.Forms 3.6.0.344457上看到Carousel视图,也只能在其中定义一个接口。 “轮播视图”类中没有实现和属性。

您只能在Xamarin.Forms 4.0下使用它。由于它现在仍是预览版,因此存在一些限制。有关更多信息,请参见此处https://stackoverflow.com/a/56235795/8354952