在我的大师登陆中,我有一个简单的页面:
<ContentPage xmlns="..." Title="Detail">
<ContentPage.ToolbarItems>
<ToolbarItem Text="Hi" Icon="icon"><Entry></Entry></ToolbarItem>
</ContentPage.ToolbarItems>
我正在尝试在工具栏中添加一个定制视图,或者更好。我如何访问工具栏以添加这样的控件?
(从基本上我需要在每页上的工具栏上进行搜索文本框(
不可能使用ToolbarItems
集合中的构建方式以跨平台方式进行此操作,但是您可以使用自定义渲染器手动实现它。[此Blogpost]很好地描述了Android,以及链接到iOS解决方案。与UWP一起实现同样容易。
我使用此解决方案来存档页面顶部的搜索收件箱。它不是工具栏项目。我只是将工具栏隐藏在我想要的每个页面中。它可能是更灵活的解决方案,因为它应该在每个平台上按预期工作。减去,如果您有一个,则需要在页面中实现工具栏项目。我将所有内容绑定到ViewModel。
它在Xamarin 4.3上起作用,但是对于较小的修改也应该在早期版本上使用。
public SomeListPage()
{
Shell.SetTabBarIsVisible(this, false);
InitializeComponent();
}
这是我的完整页面。顶部包含文本输入和搜索按钮。然后列表。这里的奖励最后是圆底浮动按钮,用于用作新项目插入。对于cicle按钮,我使用fontawesome和plus符号。
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="Puvx.Pages.SomeListPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackLayout>
<StackLayout Orientation="Horizontal">
<Entry Text="Search box" />
<Button Text="Seach" />
</StackLayout>
<AbsoluteLayout>
<CollectionView ItemsSource="{Binding Reviews}">
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout Padding="10" Orientation="Vertical">
<Label FontAttributes="Bold" Text="{Binding Nr}" />
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<Button
AbsoluteLayout.LayoutBounds=".95,.95,80,80"
AbsoluteLayout.LayoutFlags="PositionProportional"
BackgroundColor="DarkGray"
CornerRadius="50"
FontFamily="{StaticResource FontAwesomeSolid}"
FontSize="Large"
Text=""
TextColor="White" />
</AbsoluteLayout>
</StackLayout>
</ContentPage>