首页 > c# > xamarin.form(可移植的xaml.form跨平台)-on android,SearchBar和StackLayout无法正常工作

xamarin.form(可移植的xaml.form跨平台)-on android,SearchBar和StackLayout无法正常工作 (xamarin.form (portable xaml.form cross-platform) -on android, SearchBar and StackLayout not working)

问题

我根本不知道为什么SearchBar不会显示在StackLayout上我写了下面的xaml标签:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:App3"
         x:Class="App3.MainPage">

  <StackLayout>
    <SearchBar IsVisible="True" IsEnabled="True" Placeholder="Search" 
     BackgroundColor="Red" TextColor="White"/>
    <Label Text="asdasd "/>

  </StackLayout>
</ContentPage>

但我唯一看到的是标签,而不是搜索栏,因为你看到我添加了IsVisible和IsEnabled属性,即使它是多余的只是为了确保问题,请注意,如果我单独放置搜索栏它工作正常:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:App3"
         x:Class="App3.MainPage">

    <SearchBar  Placeholder="shsdfdsf"/>

</ContentPage>

它也能在绝对布局上正常工作,感谢所有帮手!

解决方法

好吧,我已经弄清楚了,显然不像标签/按钮和许多其他它不适合搜索栏的requestHeight和requestwidth属性(或至少在这里 - 虽然它很奇怪,因为它应该在任何地方行为相同)但写:

    <SearchBar Placeholder="asdasdasf23" HorizontalOptions="Fill" HeightRequest="50" />

有固定的,但是如果有其他人知道为什么它不起作用,如果我能听到如何解决它会很好。

问题

I simply don't know why SearchBar won't display on StackLayout I've written the following xaml tags:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:App3"
         x:Class="App3.MainPage">

  <StackLayout>
    <SearchBar IsVisible="True" IsEnabled="True" Placeholder="Search" 
     BackgroundColor="Red" TextColor="White"/>
    <Label Text="asdasd "/>

  </StackLayout>
</ContentPage>

but the only thing i see is the label and not the searchbar, as you see i've added the IsVisible and IsEnabled properties even though it's redundant just to make sure that's the problem, notice that if I place the searchbar alone it works fine:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:App3"
         x:Class="App3.MainPage">

    <SearchBar  Placeholder="shsdfdsf"/>

</ContentPage>

it's also working fine on absolutelayout, thanks for all the helpers !

解决方法

ok i've figured it out, apparently unlike label/button and many other it doesn't "fit" the requestHeight and the requestwidth properties of searchbar(or at least here - though it's odd since it should act the same everywhere) but writing:

    <SearchBar Placeholder="asdasdasf23" HorizontalOptions="Fill" HeightRequest="50" />

there fixed it, but if there's someone else who knows why it didn't work w/o that it would be great if I can hear how to fix it.

相似信息