是否有任何理由说明Xamarin.Forms搜索栏无法在Android上显示(目前正在运行Android 7.0)。我读过,做一个HeightRequest可能是一个好主意但是在尝试之后,搜索栏仍然没有出现。这是我在xaml中初始化搜索栏的内容:
<SearchBar x:Name="searchBarList" Placeholder="Search" HeightRequest="42" Opacity="1"/>
知道如何前进吗?
更新: 整个布局如下:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="SalApp.views.MainList" BackgroundColor="LightGray" NavigationPage.HasNavigationBar="False">
<ContentPage.ToolbarItems>
<ToolbarItem Icon="shopping_cart_icon.png"
Priority="0" Order="Primary" />
</ContentPage.ToolbarItems>
<SearchBar x:Name="searchBarList"
Placeholder="Search"
HeightRequest="42"
Opacity="1"/>
<ListView x:Name="listView" Opacity="0" SeparatorColor="AntiqueWhite" RowHeight="80" ItemSelected="listView_ItemSelected" IsPullToRefreshEnabled="True"/>
但它目前只是为了查看搜索栏而设置如此,但它仍然不可见
答案 0 :(得分:2)
您的问题是View
的根目录中有多个ContentPage
。
将您的View
分组在父控件下,例如:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="SalApp.views.MainList" BackgroundColor="LightGray" NavigationPage.HasNavigationBar="False">
<ContentPage.ToolbarItems>
<ToolbarItem Icon="shopping_cart_icon.png"
Priority="0" Order="Primary" />
</ContentPage.ToolbarItems>
<StackLayout>
<SearchBar x:Name="searchBarList"
Placeholder="Search"
HeightRequest="42"
Opacity="1"/>
<ListView x:Name="listView" Opacity="0" SeparatorColor="AntiqueWhite" RowHeight="80" ItemSelected="listView_ItemSelected" IsPullToRefreshEnabled="True"/>
</StackLayout>
</ContentPage>
答案 1 :(得分:0)
如果您想要更通用的方法来解决此问题,而不是手动添加HeightRequest
,则可以实施SearchBarRenderer
using Android.OS;
using AppTTM_AVI.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(SearchBar), typeof(CustomSearchBarRenderer))]
namespace App.Droid
{
/// <summary>
/// Workaround for searchBar not appearing on Android >= 7
/// </summary>
public class CustomSearchBarRenderer : SearchBarRenderer
{
public CustomSearchBarRenderer()
{
}
protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
{
base.OnElementChanged(e);
if (e.OldElement != null || Element == null)
{
return;
}
if (Build.VERSION.SdkInt >= BuildVersionCodes.N)
{
Element.HeightRequest = 42;
}
}
}
}
来源:https://forums.xamarin.com/discussion/comment/296772/#Comment_296772
答案 2 :(得分:0)
Android 7.0和7.1搜索栏在安装后第一次出现,一旦我退出或将方向更改为横向模式,它已经实现了。
permute