C# Xamarin搜索栏未显示
是否有任何原因导致Xamarin.Forms搜索栏无法在Android(目前运行的是Android 7.0)上显示。我读到这可能是一个好主意,做一个高度请求,但即使尝试了,搜索栏仍然没有显示。以下是我在xaml中用于初始化搜索栏的内容:C# Xamarin搜索栏未显示,c#,xamarin,xamarin.forms,xamarin.android,C#,Xamarin,Xamarin.forms,Xamarin.android,是否有任何原因导致Xamarin.Forms搜索栏无法在Android(目前运行的是Android 7.0)上显示。我读到这可能是一个好主意,做一个高度请求,但即使尝试了,搜索栏仍然没有显示。以下是我在xaml中用于初始化搜索栏的内容: <SearchBar x:Name="searchBarList" Placeholder="Search" HeightRequest="42" Opacity="1"/> 你知道如何前进吗 更新: 整个布局如下所示: <?xml ve
<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"/>
但是,目前只有这样设置才能查看搜索栏,但它仍然不可见您的问题是在
内容页的根目录中有多个视图
将视图分组到父控件下,例如:
<?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>
您的问题是内容页面的根目录中有多个视图
将视图分组到父控件下,例如:
<?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>
如果您想要一种更通用的方法来解决此问题,而不是手动添加高度请求
,则可以实现搜索排序器
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;
}
}
}
}
使用Android.OS;
使用AppTTM_AVI.Droid;
使用Xamarin.Forms;
使用Xamarin.Forms.Platform.Android;
[程序集:ExportRenderer(typeof(SearchBar)、typeof(CustomSearchDerrer))]
名称空间App.Droid
{
///
///Android>=7上未出现搜索栏的解决方法
///
公共类CustomSearchBallenderer:SearchBallenderer
{
公共定制器()
{
}
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(e.OldElement!=null | | Element==null)
{
返回;
}
如果(Build.VERSION.SdkInt>=BuildVersionCodes.N)
{
Element.HeightRequest=42;
}
}
}
}
来源:如果您需要更通用的方法来解决此问题,并且不需要手动添加高度请求
,您可以实现搜索排序器
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;
}
}
}
}
使用Android.OS;
使用AppTTM_AVI.Droid;
使用Xamarin.Forms;
使用Xamarin.Forms.Platform.Android;
[程序集:ExportRenderer(typeof(SearchBar)、typeof(CustomSearchDerrer))]
名称空间App.Droid
{
///
///Android>=7上未出现搜索栏的解决方法
///
公共类CustomSearchBallenderer:SearchBallenderer
{
公共定制器()
{
}
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(e.OldElement!=null | | Element==null)
{
返回;
}
如果(Build.VERSION.SdkInt>=BuildVersionCodes.N)
{
Element.HeightRequest=42;
}
}
}
}
来源: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;
}
protected override void OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(e.OldElement!=null | | Element==null)
返回;
如果(Build.VERSION.SdkInt>=BuildVersionCodes.N)
Element.HeightRequest=40;
}
安卓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;
}
protected override void OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(e.OldElement!=null | | Element==null)
返回;
如果(Build.VERSION.SdkInt>=BuildVersionCodes.N)
Element.HeightRequest=40;
}
您的其他布局是什么样子的?将整个布局添加到原始问题中。如果我没记错的话,我在Android中的搜索栏也有一些问题。我只是使用了一个旁边有搜索图标的条目。你的其他布局是什么样子的?将整个布局添加到原始问题中。如果我没记错的话,我在Android中的搜索栏也有一些问题。我只是使用了一个旁边有搜索图标的条目。啊,StackLayout;)杰拉尔德弗斯路易斯,你抓得好!嗯,StackLayout;)杰拉尔德弗斯路易斯,你抓得好!