簡體   English   中英

如何在Xamarin表單中使用XAML隱藏TableSection?

[英]How do I hide a TableSection with XAML in Xamarin Forms?

我最近一直在Xamarin Forms中從事項目工作,並且一直在使用TableView來顯示從Web api檢索到的記錄的詳細信息。 有時,某些詳細信息不存在,因此我想隱藏顯示信息的部分。

但是,我找不到隱藏TableSection的方法。

這是一些XAML:

<TableView>
    <TableRoot>

        ...

        <!--Contact info-->
        <TableSection IsVisible="{Binding HasContact}" Title="Contact">

          <!--Contact name-->
          <TextCell Text="{Binding ContactName}" Detail="Primary contact" />

          <!--Phone-->
          <TextCell Text="Phone"
                    Detail="{Binding FormattedContactPhoneNumber}"
                    Command="{Binding BindingContext.DialPhoneCommand, Source={x:Reference MainGrid}}"
                    CommandParameter="{Binding ContactPhoneNumber}"/>


          <!--Email-->
          <TextCell Text="Email"
                    Detail="{Binding ContactEmail}"
                    Command="{Binding BindingContext.SendEmailCommand, Source={x:Reference MainGrid}}"
                    CommandParameter="{Binding ContactEmail}"/>

        </TableSection>
    </TableRoot>
</TableView>

顯然,IsVisible屬性不起作用,並因為不存在而引發異常(它存在於其他元素(如Labels)上)。 我還嘗試使用VisualElement.IsVisible拋出無效的VisualElement.IsVisible異常。 那么有什么方法可以隱藏此部分?

如果沒有辦法,也許我需要走一條骯臟的路,並使用單獨的TableViews(可以在其中使用VisualElement.IsVisible):(

好吧,您遇到了使用TableView的一個缺點,即無法通過可綁定屬性動態隱藏節。

在我的項目中,我這樣解決了這個問題:

在頁面后面的代碼中,我偵聽用作BindingContext的ViewModel的OnPropertyChanges。 當所需的布爾值更改時,我刪除了TableSection中不再需要的單元格。 當再次需要該單元格時,我再次插入它。

因此,命名所有節和單元格,並在頁面開始時保留需要更改的那些單元格,以供以后刪除和添加它們以供參考。

小示例代碼

private void OnViewmodelPropertyChanged (object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
    if (e.PropertyName.Equals("IsBioSecurityAvailable", StringComparison.OrdinalIgnoreCase))
    {
        AdjustBioSecurityHeight();
    }
}

private void AdjustBioSecurityHeight()
{
    if (!_viewmodel.IsBioSecurityAvailable && GeneralSection.Contains(BioSecurityViewCell))
        GeneralSection.Remove(BioSecurityViewCell);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM