【问题标题】:How to autoplay the list of image in xamarin forms如何自动播放 xamarin 表单中的图像列表
【发布时间】:2019-08-07 12:06:20
【问题描述】:

我们正在 Visual Studio 2019 中使用 Xamarin 开发购物车应用程序,我的问题是,我想从图像列表中从左到右自动播放幻灯片,请给我解决此问题的解决方案

这是我的代码:

using System;
using System.Collections.Generic;
using Xamarin.Forms;

namespace Shopping.Pages
{
    public partial class GalleryPage : ContentPage
    {
        public GalleryPage()
        {
            InitializeComponent();

            List<string> students = new List<string>()
                {
                    "Manikandan.png",
                    "RajKumar.png",
                    "Suresh.png",
                    "Karthick.png",
                    "Prakash.png",
                    "Kavin.png",
                    "Aathif.png",
                    "Prabhu.png",
                    "Aravind.png",
                };

            listViewCarousal.ItemsSource = students;
        }

        void OnTapGestureRecognizerTapped1(object sender, EventArgs args)
        {

        }
    }
}

GalleryPage.xaml:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Shopping.Pages.GalleryPage">
    <ContentPage.Content>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="8*" />
                <RowDefinition Height="2*" />
            </Grid.RowDefinitions>
            <controls:CarouselViewControl Grid.Row="0"
                                          Orientation="Horizontal" 
                                          InterPageSpacing="10" 
                                          VerticalOptions="FillAndExpand" 
                                          HorizontalOptions="FillAndExpand"
                                          x:Name="listViewCarousal" 
                                          ShowIndicators="True">
                <controls:CarouselViewControl.ItemTemplate>
                    <DataTemplate>
                        <Image Source="{Binding .}"/>
                    </DataTemplate>
                </controls:CarouselViewControl.ItemTemplate>
            </controls:CarouselViewControl>
            <Image Source="Autoplay.png"  Grid.Row="1">
                <Image.GestureRecognizers>
                    <TapGestureRecognizer Tapped="OnTapGestureRecognizerTapped" />
                </Image.GestureRecognizers>
            </Image>
        </Grid>
    </ContentPage.Content>
</ContentPage>

注意:当我单击自动播放图像时,我想将图像列表显示为幻灯片。

【问题讨论】:

标签: xamarin.forms carousel


【解决方案1】:

根据你的描述,你想要CarouseViewControl自动滑动条,@Paul的点很好用,不过你也可以这么简单。

private void OnTapGestureRecognizerTapped1(object sender, EventArgs args)
    {
        Device.StartTimer(TimeSpan.FromSeconds(2), (Func<bool>)(() =>

        {

            int currentIndex = listViewCarousal.Position;
            int nextIndex = currentIndex < listViewCarousal.ItemsSource.OfType<object>().Count() - 1
                                 ? currentIndex + 1
                                 : 0;
            listViewCarousal.Position = nextIndex;

            return true;
        }));
    }

【讨论】:

  • 谢谢,这是我的解决方案,您节省了我的时间
  • @P.Thiyagu,如果我的回复解决了你的问题,请记得标记为答案,谢谢
  • 这似乎是正确的答案。我在我的项目中使用了类似的方法。
【解决方案2】:

假设您正在谈论this view,您可以简单地设置或绑定CarouselViewControl 的Position 属性。只要CarouselViewControl.AnimateTransition 是true(据我所知,这应该是默认值),CarouselViewControl 就会在您设置Position 时为项目之间的过渡设置动画。

您可以在后面的代码中实现以下方法:

private void SelectNextImage()
{
    int nextImageIndex = GetNextImageIndex();
    listViewCarousal.Position = nextImageIndex;
}

private int GetNextImageIndex()
{
    int currentIndex = listViewCarousal.Position;
    int nextIndex = currentIndex < listViewCarousal.ItemsSource.OfType<object>().Count() - 1 
                         ? currentIndex + 1 
                         : 0;
}

可以从定时器方法调用

using System.Threading;

// in constructor
this.slideshowTimer = new Timer(SlideshowTimerElapsed, null, Timeout.Infinite, Timeout.Infinity);

private void OnTapGestureRecognizerTapped1(object sender, EventArgs args)
{
    // this starts the timer, you'll hopefully be able to figure out 
    // how to stop it by yourself
    slideshowTimer.Change(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));
}

private void SlideshowTimerElapsed(object unusedState)
{
    // since we are on another thread
    Device.BeginInvokeOnMainThread(SelectNextImage);
}

【讨论】:

    【解决方案3】:
    Here my code 
    
    void Handle_Tapped(object sender, System.EventArgs e)
            {
                _isRunning = true;
                RunTimer();
            }
    
            void Handle_Tapped_1(object sender, System.EventArgs e)
            {
    
                _isRunning = false;
            }
    
            //this is for timer
    
            private bool _isRunning;
    
            public void RunTimer()
            {
    
                Device.StartTimer(new TimeSpan(0, 0, 2), () =>
                {
    
                    int currentIndex = listViewCarousal.Position;
                    int nextIndex = currentIndex < listViewCarousal.ItemsSource.OfType<object>().Count() - 1
                                         ? currentIndex + 1
                                         : 0;
                    listViewCarousal.Position = nextIndex;
    
                    return _isRunning;
                });
            }
    

    【讨论】:

    • 看来你的问题已经解决了,可以把你的回复或者我的回复标记为答案,就看你自己了,谢谢。
    • 我需要能够在图像上实现双指缩放手势,有什么想法吗?
    • ,如果想放大Image的话可以看一下:c-sharpcorner.com/article/zoom-in,不过我觉得CarouselViewControl不行,因为CarouselViewControl是Container,里面有很多image ,所以它不起作用。
    猜你喜欢
    • 2020-07-19
    • 1970-01-01
    • 2019-12-25
    • 2015-06-29
    • 2019-01-22
    • 1970-01-01
    • 1970-01-01
    • 2018-03-14
    • 1970-01-01
    相关资源
    最近更新 更多