Скрыть Xamarin Forms StackLayout, если в массиве Binding нет элементов - PullRequest
0 голосов
/ 04 марта 2020

В моем приложении Xamarin Forms у меня есть CarouselView для пролистывания сообщений. Некоторые сообщения имеют вложения. Я хотел бы скрыть StackLayout attachmentsLayout, когда в списке вложений нет элементов.

Я пытался поиграть с DataTriggers, но это не сработало.

<?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:MyApp.Helpers" x:Class="MyApp.MailboxConversationPage" Title="Messages">
    <ContentPage.Content>
        <AbsoluteLayout x:Name="absLayout" VerticalOptions="FillAndExpand">
            <StackLayout HorizontalOptions="Fill" HeightRequest="{Binding Path=Height, Source={x:Reference absLayout}}" WidthRequest="{Binding Path=Width, Source={x:Reference absLayout}}">
                <StackLayout VerticalOptions="Start" Padding="10,10,10,5">
                  <Label x:Name="Subject" Text="Subject" FontSize="Large" FontAttributes="Bold" />
                </StackLayout>
                <StackLayout x:Name="carouselViewLayout" VerticalOptions="FillAndExpand">
                    <ContentView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" HeightRequest="{Binding Path=Height, Source={x:Reference carouselViewLayout}}" WidthRequest="{Binding Path=Width, Source={x:Reference carouselViewLayout}}">
                      <CarouselView x:Name="CarouselMessages" PositionChanged="OnPositionChanged">
                        <CarouselView.ItemTemplate>
                          <DataTemplate>
                            <StackLayout HorizontalOptions="Fill" BackgroundColor="#f5f5f5">
                              <StackLayout VerticalOptions="Start" Padding="10,10,10,5">
                                <Label Text="{Binding Sender}" FontAttributes="Bold" />
                              </StackLayout>
                              <StackLayout VerticalOptions="FillAndExpand" Padding="10,10,10,10" BackgroundColor="White">
                                <ScrollView>
                                    <Label Text="{Binding Body}" />
                                </ScrollView>
                              </StackLayout>
                              <StackLayout x:Name="attachmentsLayout" BindableLayout.ItemsSource="{Binding Attachments}" VerticalOptions="End" Padding="10,10,10,10" BackgroundColor="White">
                                <BindableLayout.ItemTemplate>
                                    <DataTemplate>
                                        <Label>
                                            <Label.FormattedText>
                                                <FormattedString>
                                                    <local:HyperlinkSpan Text="{Binding Filename}" Url="{Binding Url}" />
                                                </FormattedString>
                                            </Label.FormattedText>
                                        </Label>
                                    </DataTemplate>
                                 </BindableLayout.ItemTemplate>
                              </StackLayout>
                              <StackLayout VerticalOptions="End" Padding="10,5,10,10">
                                <Label Text="{Binding Created_At}" BackgroundColor="#f5f5f5" />
                              </StackLayout>
                            </StackLayout>
                          </DataTemplate>
                        </CarouselView.ItemTemplate>
                      </CarouselView>
                    </ContentView>
                </StackLayout>
                <StackLayout x:Name="editorLayout" VerticalOptions="FillAndExpand" IsVisible="false" Padding="10,0,10,0">
                    <Frame x:Name="replyFrame" HeightRequest="{Binding Path=Height, Source={x:Reference editorLayout}}" HasShadow="false" OutlineColor="Gray" Padding="2,0,2,0">
                    <Editor x:Name="replyEditor">
                        <Editor.BackgroundColor>
                            <OnPlatform x:TypeArguments="Color"
                                iOS="White"
                                Android="Transparent"
                                WinPhone="#2c3e50" />
                        </Editor.BackgroundColor>
                    </Editor>
                    </Frame>
                </StackLayout>
                <StackLayout VerticalOptions="End" Padding="10,3,10,10">
                    <Button x:Name="replyButton" Text="Reply" Clicked="OnReplyActivated" BorderRadius="5" BackgroundColor="#337ab7" TextColor="White" FontAttributes="Bold" />
                    <Button x:Name="sendButton" Text="Send Message" Clicked="OnSendMessageActivated" BorderRadius="5" BackgroundColor="#337ab7" TextColor="White" FontAttributes="Bold" IsVisible="false"/>
                </StackLayout>
            </StackLayout>
            <ContentView x:Name="overlay" AbsoluteLayout.LayoutBounds="0, 0, 1, 1"  AbsoluteLayout.LayoutFlags="All" IsVisible="True" BackgroundColor="#C0808080" Padding="10, 0">
                <ActivityIndicator  WidthRequest="110" HeightRequest="70" IsRunning="True" IsVisible="True" Color="Black" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
            </ContentView>
        </AbsoluteLayout>
    </ContentPage.Content>
</ContentPage>

1 Ответ

0 голосов
/ 05 марта 2020

Почему бы вам просто не добавить другое свойство в вашу ViewModel, которое указывает, есть ли какие-либо элементы в вашей коллекции вложений. Затем привяжите IsVisible макета к этому свойству

View

<StackLayout x:Name="attachmentsLayout" 
             IsVisible="{Binding HasItems}"
             BindableLayout.ItemsSource="{Binding Attachments}"
             VerticalOptions="End" Padding="10,10,10,10" BackgroundColor="White">
<BindableLayout.ItemTemplate>
    <DataTemplate>
        <Label>
            <Label.FormattedText>
                <FormattedString>
                    <local:HyperlinkSpan Text="{Binding Filename}" Url="{Binding Url}" />
                </FormattedString>
            </Label.FormattedText>
        </Label>
    </DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>

ViewModel

publi c bool HasItems => Attachments.Any ();

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...