我想在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>
是否可以在最后一页添加按钮?
答案 0 :(得分:2)
您可以预先在 DataTemplate 中定义按钮。并将属性 IsVisible 设置为最后一项的true。
<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
}
初始化 ItemSource
后,调用以下行var model = Monkeys[Monkeys.Count - 1] as xxxModel;
model.IsVisible = true;