我想做的是将工具栏添加到内容页面。
在 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)
我在这里做错了什么?那是什么错误,如何解决?! 我相信你们。
最佳答案
这段代码
<ContentPage.ToolbarItems>
<ToolbarItem Icon="plus.png"/>
</ContentPage.ToolbarItems>
根据上下文可以表示两件事:
- 让我们设置此 ContentPage 的 ToolbarItems 属性(或 ToolbarItemsProperty BindableProperty)(如果父节点是 ContentPage,或者
- 让我们将附加的 BindableProperty ContentPage.ToolbarItemsProperty 设置为当前的 BindableObject。
当您在 StackLayout
中使用它时,它被解释为第二个,但这样的属性不存在!所以它失败了。
将它直接放在 ContentPage
中,它会非常有效
<ContentPage ...>
<ContentPage.ToolbarItems>
<ToolbarItem Icon="plus.png"/>
</ContentPage.ToolbarItems>
<StackLayout .../>
</ContentPage>
关于 ToolbarItems 不显示,根据您运行的平台,可能需要将 ContentPage 打包到 NavigationPage 中。
关于c# - Xamarin 未找到 'ToolbarItems' 的属性、可绑定(bind)属性或事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42575683/