想要在Windows Phone 7中单击按钮时显示图像幻灯片

时间:2010-12-07 06:31:01

标签: c# silverlight visual-studio-2010 windows-phone-7 slideshow

我在网格中有一堆图像,我想为它实现幻灯片放映。我正在使用 Microsoft VS 2010 Express Edition for Windows phone 来实现此功能。可以帮助人吗?代码是:

 using System;
 using System.Collections.Generic;

 using System.Windows.Threading;

  namespace swipe
 {
 public partial class MainPage : PhoneApplicationPage
{
 // private DispatcherTimer tmr = new DispatcherTimer();

private List<string> images = new List<string>();

private int imageIndex = 0;

public MainPage()
{
    InitializeComponent();

    Loaded += new RoutedEventHandler(MainPage_Loaded);
}

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
   // tmr.Interval = TimeSpan.FromSeconds(5);
   // tmr.Tick += new EventHandler(tmr_Tick);

    LoadImages();

    ShowNextImage();
}

private void LoadImages()
{
    images.Add("/images/Hydrangeas.jpg");
    images.Add("/images/Jellyfish.jpg");
    images.Add("/images/Koala.jpg");
    images.Add("/images/Tulips.jpg");
}

private void ShowNextImage()
{
   // String bi = new BitmapImage(new Uri(images[imageIndex], UriKind.Relative));

    myImg.Source = new BitmapImage(new Uri(images[imageIndex], UriKind.Relative));

    imageIndex = (imageIndex + 1) % images.Count;
   }

   //void tmr_Tick(object sender, EventArgs e)
   //{
   //    ShowNextImage();
   //}

 protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
   {
    //if (!tmr.IsEnabled)
    //{
    //    tmr.Start();
    //}

    base.OnNavigatedTo(e);
 }
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
    //tmr.Stop();

    base.OnNavigatedFrom(e);
}

private void Play_Click(object sender, RoutedEventArgs e)
{
    ShowNextImage();
}

 }
 }

2 个答案:

答案 0 :(得分:3)

以下是一种实现此目的的简单示例。它不使用图像网格,但我相信你可以根据需要进行调整。

修改重读标题。如果您希望按此按钮,请单击取消计时器并在单击事件中调用ShowNextImage()

该页面包含以下XAML:

<Image x:Name="myImg" />

背后的代码如下:

private DispatcherTimer tmr = new DispatcherTimer();

private List<string> images = new List<string>();

private int imageIndex = 0;

public MainPage()
{
    InitializeComponent();

    Loaded += new RoutedEventHandler(MainPage_Loaded);
}

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    tmr.Interval = TimeSpan.FromSeconds(5);
    tmr.Tick += new EventHandler(tmr_Tick);

    LoadImages();

    ShowNextImage();
}

private void LoadImages()
{
    // list the files (includede in the XAP file) here
    images.Add("/images/filename1.jpg");
    images.Add("/images/filename2.jpg");
    images.Add("/images/filename3.jpg");
    images.Add("/images/filename4.jpg");
}

private void ShowNextImage()
{
    var bi = new BitmapImage(new Uri(images[imageIndex], UriKind.Relative));

    myImg.Source = bi;

    imageIndex = (imageIndex + 1) % images.Count;
}

void tmr_Tick(object sender, EventArgs e)
{
    ShowNextImage();
}

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    if (!tmr.IsEnabled)
    {
        tmr.Start();
    }

    base.OnNavigatedTo(e);
}

protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
    tmr.Stop();

    base.OnNavigatedFrom(e);
}

可以从http://cid-cc22250598bf7f04.office.live.com/self.aspx/Public/SlideShowDemo.zip

下载使用按钮预览代码的示例代码

答案 1 :(得分:1)

简单的方法是创建一组图像,并在转到枢轴页面的下一页时转到集合中的下一个图像。

另一种解决方案是创建用户控件。 Here是指导如何在Silverlight中执行此操作(但不是在WP7中,但它非常相似)。