簡體   English   中英

如何使用 xamarin forms 在輪播視圖的最后一頁上添加按鈕?

[英]How to Add Button on Last Page in Carousel view using xamarin forms?

我想在 CarouselView 頁面的最后一頁添加一個按鈕,任何人都可以幫助我嗎?

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:local="clr-namespace:sing"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="sing.Welcome"
    BackgroundColor="#ff4949">
    <ContentPage.BindingContext>
        <local:Items/>
    </ContentPage.BindingContext>
    <ContentPage.Content>
        <StackLayout>

這是我的視圖模型中帶有綁定項的輪播視圖

            <CarouselView ItemsSource="{Binding Monkeys}"
                      IndicatorView="indicatorView">
            <CarouselView.ItemTemplate>
                <DataTemplate>
                    <StackLayout>
                        <Frame HasShadow="False"
                               CornerRadius="5"
                               Margin="0,50,0,0"
                               HeightRequest="300"
                               HorizontalOptions="Center"
                               VerticalOptions="CenterAndExpand"
                               BackgroundColor="#ff4949">
                            <StackLayout>
                                <Image Source="{Binding ImageUrl}" 
                                       Aspect="AspectFill"
                                       HeightRequest="300"
                                       WidthRequest="300"
                                       HorizontalOptions="Center" />
                            </StackLayout>
                        </Frame>
                    </StackLayout>
                </DataTemplate>
            </CarouselView.ItemTemplate>
        </CarouselView>

繼續::::::::

        <IndicatorView x:Name="indicatorView"
                       IndicatorsShape="Circle"
                       IndicatorColor="LightGray"
                       SelectedIndicatorColor="Yellow"
                       HorizontalOptions="Center"
                       Margin="0,0,0,40" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

是否可以在最后一頁添加按鈕?

您可以提前在DataTemplate中定義 Button。 並將屬性IsVisible設置為最后一項的 true。

在 xaml

<DataTemplate>

    <StackLayout>

          <Frame HasShadow="False"
                 CornerRadius="5"
                 Margin="0,50,0,0"
                 HeightRequest="300"
                 HorizontalOptions="Center"
                 VerticalOptions="CenterAndExpand"
                 BackgroundColor="#ff4949">
             <StackLayout>
                 <Image Source="{Binding ImageUrl}" 
                                       Aspect="AspectFill"
                                       HeightRequest="300"
                                       WidthRequest="300"
                                       HorizontalOptions="Center" />
             </StackLayout>
         </Frame>

         <Button Text="xxx" IsVisible="{Binding IsVisible}"/>

    </StackLayout>
</DataTemplate>

在你的 model

public class xxxModel : INotifyPropertyChanged
{
   

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    bool isVisible=false;   // the button is invisible in default

    public bool IsVisible
    {
        get
        {
            return isVisible;
        }

        set
        {
            if (isVisible != value)
            {
                isVisible = value;
                NotifyPropertyChanged("IsVisible");
            }
        }
    }

    //other properties
}

在視圖模型中

初始化ItemSource后調用以下行

var model = Monkeys[Monkeys.Count - 1] as xxxModel;

model.IsVisible = true;

暫無
暫無

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

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