未显示 Xamarin 搜索栏



有什么理由为什么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"/>

但是目前只是这样设置才能看到搜索栏,但仍然不可见

您的问题是您的ContentPage根目录中有多个View

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>

Android 7.0 和 7.1 安装后搜索栏首次出现,一旦我注销或将方向更改为横向模式它就消失了,也实现了此代码。

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 = 40;
            }
如果您

想要一种更通用的方法来解决此问题,而不是手动添加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

最新更新