简体   繁体   English

Xamarin搜索栏未显示

[英]Xamarin Search bar not showing

Is there any reason why a Xamarin.Forms search bar wouldn't show on Android (currently running Android 7.0). 是否有任何原因导致Xamarin.Forms搜索栏无法在Android(当前运行Android 7.0)上显示。 I read that it might be a good idea to do a HeightRequest but even after trying that, the search bar still doesn't show up. 我读到,执行HeightRequest可能是一个好主意,但是即使尝试了一下,搜索栏仍然没有显示。 Here's what I have in my xaml to initialize the search bar: 这是我在xaml中用来初始化搜索栏的内容:

<SearchBar x:Name="searchBarList" Placeholder="Search" HeightRequest="42" Opacity="1"/>

Any idea how to move forward? 知道如何前进吗?

UPDATE: The whole layout looks like this: 更新:整个布局看起来像这样:

<?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"/>

But it's only set up like this currently in order to see the search bar but it's still not visible 但这只是目前这样设置,以便查看搜索栏,但仍不可见

Your problem is that you have more than one View in the root of your ContentPage . 您的问题是在ContentPage的根目录中有多个View

Group your View s under a parent control, an example: 将您的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>

In case that you want a more general way to resolve this and not adding manually a HeightRequest you can implement a SearchBarRenderer 如果您想以更通用的方式解决此问题,而不是手动添加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;
            }
        }
    }
}

Source: https://forums.xamarin.com/discussion/comment/296772/#Comment_296772 资料来源: https : //forums.xamarin.com/discussion/comment/296772/#Comment_296772

Android 7.0 and 7.1 Search bar appears first time after installation once I logout or changes the orientation to landscape mode its gone, Have implemented this code as well. 一旦我注销或将方向更改为横向模式,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;
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM