如何使用xamarin表单在“轮播”视图的“最后一页”上添加按钮?

时间:2020-07-03 11:11:45

标签: xamarin xamarin.forms view carousel

我想在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>

这是我的旋转木马视图,其中包含ViewModels中的绑定项

            <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>

Continuation :::::::

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

是否可以在最后一页添加按钮?

1 个答案:

答案 0 :(得分:2)

您可以预先在 DataTemplate 中定义按钮。并将属性 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>

在您的模型中

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
}

在ViewModel中

初始化 ItemSource

后,调用以下行
var model = Monkeys[Monkeys.Count - 1] as xxxModel;

model.IsVisible = true;