2017-08-11 86 views
1

我在Xaml中使用包含絕對佈局的Xaml創建表單,其中我隱藏了stacklayout。但在隱藏的堆棧佈局之下還有一個堆棧佈局,但隱藏的堆棧佈局佔用了空間。隱藏Stacklayout以xamarin形式佔用空間

我想做什麼。當我確實隱藏了一個stacklayout時,應該使用另一個stacklayout來代替隱藏的staklayout。

+0

請分享您的代碼。你想要的行爲是應該在xamarin表單上默認的行爲。環境或代碼有問題...(對不起,英語不好) –

回答

0

感謝您的幫助和支持。

我已經解決了這個問題:

我跟着這個鏈接 https://forums.xamarin.com/discussion/83632/hiding-and-showing-stacklayout 在這個環節上,他們說,使用網格線和讓行高的汽車,它會自動 調整佈局的額外空間。

<Grid VerticalOptions="Fill">  
<Grid.RowDefinitions> 
      <RowDefinition Height="100"/> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
     </Grid.RowDefinitions> <StackLayout Grid.Row="1" Spacing="20"><StackLayout Margin="10,0"> 
     <Label Text="lable 1" VerticalOptions="Center" FontSize="Small" /> 
     <Label Text="lable 2" VerticalOptions="Center" FontSize="Small" /> 
</StackLayout> 
<StackLayout IsVisible="{Binding IsStudent}" Margin="10,0"> 
    <Label Text="lable3" VerticalOptions="Center" FontSize="Small" /> 
     <Label Text="lable 4" VerticalOptions="Center" FontSize="Small" /> 
    <Label Text="lable 5" VerticalOptions="Center" FontSize="Small" /> 
     <Label Text="lable 6" VerticalOptions="Center" FontSize="Small" /> 
</StackLayout> </StackLayout> <StackLayout Grid.Row="2" Spacing="20" > 
<local:Button x:Name="btnSave" Text="Submit" VerticalOptions="End" HorizontalOptions="FillAndExpand" IsVisible="{Binding IsBusy, Converter={x:Static local:InverseBoolConverter.Instance}}" AutomationId="saveButton" /></StackLayout></Grid> 
0

我的推薦是在內容的底部放置一個StackLayout,以保持其他元素的原始高度位於頂部。

<?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="CentroDono.Views.YOURVIEWPAGE" 
       Title="YOURVIEWTITLE" 
      x:Name="YOURPAGECONTENTNAME"> 
    <ContentPage.Content> 
     <StackLayout> 
     <!--VISIBLE CONTENT--> 
      <Label Text="HELLO"/> 
     </StackLayout> 
     <StackLayout> 
      <Label Text="FOOTER"/> 
      <!--INVISIBLE CONTENT-->  
      <Label x:Name="_searchText" Text="Result1" IsVisible="False"/> 
      <Label x:Name="_searchText2" Text="Result2" IsVisible="False"/> 
      <Label x:Name="_searchText3" Text="Result3" IsVisible="False"/> 
     </StackLayout> 
    </ContentPage.Content> 
</ContentPage> 
1

我也看着爲什麼這些隱藏的控制正在空間和良好......他們正在採取的空間,這就是它。 你可以做一個簡單的添加和刪除標籤。類似這樣的:

public class MyStack : StackLayout 
{ 
    Label 
     _label1 = new Label(), 
     _label2 = new Label(), 
     _label3 = new Label(); 

    public void ShowLabels() 
    { 
     Children.Add(_label1); 
     Children.Add(_label2); 
     Children.Add(_label3); 
    } 

    public void HideLabels() 
    { 
     Children.Remove(_label1); 
     Children.Remove(_label2); 
     Children.Remove(_label3); 
    } 
} 
0

不需要網格。 設置IsVisible AND HeightRequest屬性。

MyStackLayout.IsVisible = false; 
MyStackLayout.HeightRequest = 0; // trigger recalc of space layout. 

heightrequest的變化觸發了所需的重新計算。