image slideshow with dispatcher timer in windows store apps

谁说我不能喝 提交于 2019-12-11 11:16:06

问题


I want to make slideshow image with dispatcher timer in my windows store apps. But, i have a problem: if the image has reached the final image, the slideshow do not want to repeat the slideshow from the first image, but the second image directly. For example: I have 5 images and when it reaches the fifth picture, slideshows do not want a repeat of the first image, but the second image directly.

Here is my xaml:

<Image x:Name="sceneriesBtn" IsDoubleTapEnabled="False" VerticalAlignment="Bottom" Tapped="sceneriesBtn_Tapped" Height="242" Stretch="UniformToFill"/>

And here my xaml.cs code:

public Home()
        {
            this.InitializeComponent();          
        }

        DispatcherTimer playlistTimer1 = null;
        List<string> Images1 = new List<string>();

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            ImageSource1();
        }
        private void ImageSource1()
        {
            Images1.Add("17-Ijen-Crater.jpg");
            Images1.Add("19-Ranu-kumbolo-Semeru.jpg");
            Images1.Add("30-Kelud-blitar.jpg");
            Images1.Add("31-sarangan_lake.jpg");
            Images1.Add("390-ranu_agung.jpg");
            playlistTimer1 = new DispatcherTimer();
            playlistTimer1.Interval = new TimeSpan(0, 0, 5);
            playlistTimer1.Tick += playlistTimer_Tick1;
            sceneriesBtn.Source = new BitmapImage(new Uri("ms-appx:///Sceneries/" + Images1[count1]));
            playlistTimer1.Start();
        }
        int count1 = 0;
        void playlistTimer_Tick1(object sender, object e)
        {
            if (Images1 != null)
            {
                if (count1 == Images1.Count - 1)
                    count1 = 0;
                if (count1 < Images1.Count)
                {
                    count1++;
                    ImageRotation1();
                }
            }
        }
        private void ImageRotation1()
        {
            sceneriesBtn.Source = new BitmapImage(new Uri("ms-appx:///Sceneries/" + Images1[count1].ToString()));
        }
  }
}

How to fix it?


回答1:


Logical flaw. In your code, the counter is changed twice, first from Images1.Count - 1 to 0, then incremented in the second if statement from 0 to 1.

My fix

if (count1 < Images1.Count)
   count1++;

if (count1 >= Images1.Count)
      count1 = 0;

ImageRotation1();


来源:https://stackoverflow.com/questions/29003107/image-slideshow-with-dispatcher-timer-in-windows-store-apps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!