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