如何在Windows Phone应用程序开发中播放图像幻灯片

时间:2013-12-18 09:27:02

标签: c# windows-phone slideshow

我正在使用visual C#2010为Windows phone应用程序构建我的第一个应用程序。

  • 我希望在页面中播放图像幻灯片
  • 在单击图像的幻灯片中,我想导航到另一页。
我用Google搜索了,但没有得到太多帮助。我尝试的是:

void Page1_Loaded(object sender, RoutedEventArgs e)
{
    LoadImages();
}

private void LoadImages()
{
    Image.Add("Image/aaa.jpg");
    Image.Add("Image/bbb.jpg");
    Image.Add("Image/ccc.jpg");
    Image.Add("Image/ddd.jpg");
}

我在页面的设计页面中编写了这些代码,我需要幻灯片显示图片。

2 个答案:

答案 0 :(得分:0)

检查此样本。

MainPage.xaml

中添加图片
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Image x:Name="Img" Height="400" Tap="Img_OnTap" ></Image>
</Grid>

MainPage.xaml.cs

中执行以下操作
    private List<string> _images;
    /// <summary>
    /// List of images
    /// </summary>
    public List<string> Images
    {
        get { return _images; }
        set { _images = value; }
    }

    //selected image index
    public int SelectedImageIndex { get; set; }

    // Constructor
    public MainPage()
    {
      InitializeComponent();
      this.Loaded += MainPage_Loaded;
      this.DataContext = this;
    }

   void MainPage_Loaded(object sender, RoutedEventArgs e)
   {
        LoadImages();
        DispatcherTimer t = new DispatcherTimer();
        //setting a 5 second interval
        t.Interval = new TimeSpan(0, 0, 5); 
        t.Tick += t_Tick;
        t.Start();
    }

    void t_Tick(object sender, EventArgs e)
    {
        if (SelectedImageIndex == Images.Count-1)
            SelectedImageIndex = 0;
        else
            SelectedImageIndex++;

        SetImageSource(Images[SelectedImageIndex]);
    }

    //Populating image list
    private void LoadImages()
    {
        if (Images == null)
            Images = new List<string>();
        Images.Add("/Image/aaa.jpg");
        Images.Add("/Image/bbb.jpg");
        Images.Add("/Image/ccc.jpg");
        Images.Add("/Image/ddd.jpg");
        SelectedImageIndex = 0;
        SetImageSource(Images[SelectedImageIndex]);
    }

    //setting image source
    private void SetImageSource(string imagePath)
    {
        Img.Source = new BitmapImage(new Uri(imagePath, UriKind.RelativeOrAbsolute));
    }

    //Image tap event
    private void Img_OnTap(object sender, GestureEventArgs e)
    {
        string selectedImagePath = Images[SelectedImageIndex];
        //Put your navigation here
    }

答案 1 :(得分:0)

您可以使用计时器和列表框控件创建自己的幻灯片,试试这个

你的xaml代码应该是

<ListBox  Grid.Row="1" Name="listControlImage" SelectionChanged="listControlImage_SelectionChanged">
      <ListBox.ItemTemplate>
      <DataTemplate>
      <Image  Height="600" Width="460" Stretch="Fill" Source="{Binding}"/>
  </ListBox.ItemTemplate>
      </DataTemplate>

代码背后的代码应该是

//全局变量

List<BitmapImage> ListImages = new List<BitmapImage>();
 DispatcherTimer Timer = new DispatcherTimer();
int SlideCount=0;

void Page1_Loaded(object sender, RoutedEventArgs e)
{
  LoadImages();
  DisTimer.Tick += DisTimer_Tick;
  DisTimer.Interval = new TimeSpan(0, 0, 1);
  DisTimer.Start();
}

private void LoadImages()
{
  ListImages.Add( new BitmapImage(new Uri("/Your project name;component/Image/aaa.jpg", UriKind.Relative))); 
 ListImages.Add( new BitmapImage(new Uri("/Your project name;component/Image/bbb.jpg", UriKind.Relative))); 
 ListImages.Add( new BitmapImage(new Uri("/Your project name;component/Image/ccc.jpg", UriKind.Relative))); 
 ListImages.Add( new BitmapImage(new Uri("/Your project name;component/Image/ddd.jpg", UriKind.Relative))); 

ListImages.ItemsSource =ListImages;
}

 void DisTimer_Tick(object sender, EventArgs e)
        {
if(SlideCount<=3)
{
listControlImage.SelectedIndex = SlideCount;
SlideCount++;
}
else
   SlideCount=0;
        }






 private void listControlImage_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
            {
if(listControlImage.SelectedIndex ==-1)
return;
    //Your Navigation code
            }