如何使用 xamarin 表单在轮播视图的最后一页上添加按钮?
How to Add Button on Last Page in Carousel view using xamarin forms?
我想在 CarouselView 页面的最后一页添加一个 Button 谁能帮助我?
<?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>
继续:::::::
<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>
在你的模型中
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;
我想在 CarouselView 页面的最后一页添加一个 Button 谁能帮助我?
<?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>
继续:::::::
<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>
在你的模型中
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;