Xamarin Custom ViewCell

时间:2017-03-14 12:55:38

标签: xaml xamarin data-binding xamarin.forms

在制作自定义嵌套视图时我遇到了问题。 我正在尝试从各种页面使用我的自定义视图(ViewCell)。 这就是我使用它的方式。

如果我使用View for Xaml和codebehind,它不会引发任何错误, 但它不会显示任何东西。

如果我尝试使用ViewCell for xaml及其代码隐藏,我会收到一些错误。 这就是我使用它的方式。

在mainPage xaml

 <local:UploaderCell Uploader="{Binding post.uploader}"/>

这是导致错误的行,

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage 
    xmlns:local="clr-namespace:EodiRoad;assembly=EodiRoad"
    xmlns:ic="clr-namespace:ImageCircle.Forms.Plugin.Abstractions;assembly=ImageCircle.Forms.Plugin.Abstractions"
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    x:Class="EodiRoad.SocialPage"
    Title="Story">

    <ContentPage.ToolbarItems>
        <ToolbarItem Icon="plus.png" Order="Primary" Priority="0" Activated="OnCreatePostClicked"/>
    </ContentPage.ToolbarItems>

    <StackLayout>

        <ListView 
            x:Name="socialPostsListView" 
            HasUnevenRows="true" 
            IsPullToRefreshEnabled="true" 
            Refreshing="Handle_Refreshing" 
            SeparatorVisibility="None"
            ItemSelected="OnSocialPostSelected"
            >

            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Vertical" Padding="5">
                            <local:UploaderCell Uploader="{Binding post.uploader}"/>


                            <Image
                                x:Name="postImage"
                                Source="{Binding post.image}"
                                Aspect="AspectFill"
                                MinimumHeightRequest="200"
                            />


                            <StackLayout Orientation="Horizontal">
                                <Switch IsToggled="{Binding post.isLiked}"/>
                                <Button Text="comment"/>
                                <Button Text="share"/>
                            </StackLayout>

                            <StackLayout Orientation="Vertical" HorizontalOptions="StartAndExpand">
                                <Label Text="{Binding post.content}"/>
                                <Label Text="{Binding post.uploadedTime}" TextColor="Gray"/>
                            </StackLayout>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

    </StackLayout>


</ContentPage>

这是我的完整主页xaml代码。

在UploderCell.xaml

<?xml version="1.0" encoding="UTF-8"?>
<ViewCell
    xmlns:ic="clr-namespace:ImageCircle.Forms.Plugin.Abstractions;assembly=ImageCircle.Forms.Plugin.Abstractions"
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    x:Class="EodiRoad.UploaderCell">


    <StackLayout Orientation="Horizontal" Padding="5">
        <Button Clicked="Handle_Clicked" Image="{Binding Uploader.userProfile.userThumbnail}" BorderRadius="5" HeightRequest="50" WidthRequest="50"/>

        <ic:CircleImage 
            x:Name="userProfileImage"
            HeightRequest="50"
            WidthRequest="50"
            Aspect="AspectFill"
            Source="{Binding Uploader.userProfile.userThumbnail}"
        />

        <Label Text="{Binding Uploader.username}"/>
    </StackLayout>
</ViewCell>

和codeBehind

public partial class UploaderCell : ViewCell
    {
        public static readonly BindableProperty UploaderProperty = 
            BindableProperty.Create(
                propertyName: "Uploader", 
                returnType: typeof(UserModel), 
                declaringType: typeof(UploaderCell)
            ); 

        public UserModel Uploader
        {
            get
            { return GetValue(UploaderProperty) as UserModel; }
            set 
            { SetValue(UploaderProperty, value); }
        }


        public UploaderCell()
        {
            InitializeComponent();
            BindingContext = this;
        }
  }

我从mainPage

收到此错误
object of type Project.UploaderCell cannot be converted to type 'xamarin.forms.view'

我在这里做错了什么? 我怎么可能这个..

0 个答案:

没有答案