我想實現一種可摺疊的StackLayout。每個用戶點擊按鈕,它展開或摺疊堆棧佈局以顯示/隱藏更多細節。Xamarin Forms Collapsable StackLayout
我是能夠實現更多/更少此與下面的代碼,但它看起來不正確,效果不是很大,因爲它會立即增長,我應用效果其他元素。
你有什麼建議可以做到這一點,我使用的是xamarin Forms嗎?
XAML
<?xml version="1.0" encoding="utf-8" ?>
<StackLayout xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Sample.MyStackLayout" >
<StackLayout x:Name="TopLayout">
<StackLayout Orientation="Horizontal">
<Label Text="some text" VerticalOptions="Center" HorizontalOptions="StartAndExpand" />
<Label Text="123" VerticalOptions="Center" HorizontalOptions="End" FontSize="Large" />
</StackLayout>
<BoxView Color="Black" HeightRequest="1" />
<StackLayout Orientation="Horizontal">
<Label Text="some text" VerticalOptions="Center" HorizontalOptions="StartAndExpand" />
<Label Text="123" VerticalOptions="Center" HorizontalOptions="End" FontSize="Large" />
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="some text" VerticalOptions="Center" HorizontalOptions="StartAndExpand" />
<Label Text="123" VerticalOptions="Center" HorizontalOptions="End" FontSize="Large" />
</StackLayout>
<Button x:Name="btn" Text="Button" Clicked="btnClicked" />
</StackLayout>
<StackLayout x:Name="MoreDetails" IsVisible="False">
<Label Text="some text 1"></Label>
<Label Text="some text 2"></Label>
<Label Text="some text 3"></Label>
<Label Text="some text 4"></Label>
<Label Text="some text 5"></Label>
<Label Text="some text 6"></Label>
<Label Text="some text 7"></Label>
<Label Text="some text 8"></Label>
</StackLayout>
</StackLayout>
代碼
public AccountInfo()
{
InitializeComponent();
}
bool isExpanded = false;
protected async void btnClicked(object sender, EventArgs e)
{
if (isExpanded)
{
await MoreDetails.FadeTo(0);
MoreDetails.IsVisible = !isExpanded;
}
else
{
MoreDetails.IsVisible = !isExpanded;
await MoreDetails.FadeTo(1);
}
isExpanded = !isExpanded;
}
對不起,我可能做錯了什麼,但我粘貼代碼,並沒有奏效如預期。我的想法是刪除(隱藏)較低的堆棧佈局。 Btw Easing.None在我的Xamarin版本中不存在,所以我使用的是Linear。我也在StackLayout中包裝了所有主要內容,所以我有[StackLayout> StackLayout TopLayout和StackLayout ModerDetails] – user1845593
我編輯了我的答案,試試這個 – Greensy
感謝您的幫助,但不編譯,TranslateTo採用此參數:double x,double y,uint length = 250,Easing easing = null。順便說一句,你在ShowMore和ShowLess之後缺少「()」 – user1845593