【问题标题】:Xamarin No property, bindable property, or event found for 'ToolbarItems'Xamarin 找不到“ToolbarItems”的属性、可绑定属性或事件
【发布时间】:2017-03-03 09:54:28
【问题描述】:

我试图做的是将工具栏添加到内容页面。

StackLayout下添加这段代码后

<ContentPage.ToolbarItems>
   <ToolbarItem Icon="plus.png"/>
</ContentPage.ToolbarItems>

了解更多信息。如果我将上面的代码放在与顶部相同的位置 StackLayout 工具栏就不会出现。

我遇到了一个错误,在其他任何地方都找不到相同的错误。

这是我的 xaml 文件内容。

    <?xml version="1.0" encoding="UTF-8"?>
<ContentPage 
    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">



    <StackLayout>

        <ContentPage.ToolbarItems>
            <ToolbarItem Icon="plus.png"/>
        </ContentPage.ToolbarItems>

        <ListView x:Name="socialPostsListView" HasUnevenRows="true" IsPullToRefreshEnabled="true">

            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Vertical" Padding="5">
                            <StackLayout Orientation="Horizontal" Padding="5">
                                <ic:CircleImage 
                                    x:Name="userProfileImage"
                                    HeightRequest="50"
                                    WidthRequest="50"
                                    Aspect="AspectFill"
                                    Source="{Binding post.uploader.userProfile.userThumbnail}"
                                />
                                <Label Text="{Binding post.uploader.username}"/>
                            </StackLayout>

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

                            <StackLayout Orientation="Horizontal">
                                <Button Text="like"/>
                                <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>

这就是错误。

No property, bindable property, or event found for 'ToolbarItems'

以及完整的错误。

/Users/UZU/Google Drive/Apps/EodiRoad/EodiRoadXamarin/EodiRoad/EodiRoad/EodiRoad.Views.SocialPage.xaml(5,5): Error: Position 13:5. No property, bindable property, or event found for 'ToolbarItems' (EodiRoad)

我在这里做错了什么?那个错误是关于什么的,以及如何解决它?! 我相信你们。

【问题讨论】:

    标签: c# xaml xamarin xamarin.forms


    【解决方案1】:

    这个sn-p

    <ContentPage.ToolbarItems>
      <ToolbarItem Icon="plus.png"/>
    </ContentPage.ToolbarItems>
    

    可能意味着两件事,具体取决于上下文:

    1. 让我们设置这个 ContentPage 的 ToolbarItems 属性(或 ToolbarItemsProperty BindableProperty)(如果父节点是 ContentPage,或者
    2. 让我们将 Attached BindableProperty ContentPage.ToolbarItemsProperty 设置为当前的 BindableObject。

    当您在StackLayout 中使用它时,这被解释为第二个,但这样的属性不存在!所以它失败了。

    将它直接放在ContentPage 中,它会像一个魅力一样工作

    <ContentPage ...>
      <ContentPage.ToolbarItems>
        <ToolbarItem Icon="plus.png"/>
      </ContentPage.ToolbarItems>
      <StackLayout .../>
    </ContentPage>
    

    关于未出现的 ToolbarItems,根据您运行的平台,可能需要将 ContentPage 打包在 NavigationPage 中。

    【讨论】:

    • 哦!感谢您的精彩解释。我正在使用选项卡式页面内的内容页面。那么在这种情况下我不能显示工具栏吗?
    【解决方案2】:

    您需要在StackLayout 之外添加ToolbarItems 试试这个:

    <ContentPage 
        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">
        <ContentPage.ToolbarItems>
            <ToolbarItem Icon="plus.png"/>
        </ContentPage.ToolbarItems>
        <StackLayout>
            <!-- Your Content -->
        </StackLayout>
    </ContentPage>
    

    【讨论】:

      猜你喜欢
      • 2019-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-18
      • 2021-03-11
      • 1970-01-01
      相关资源
      最近更新 更多