2014-12-02 87 views
0

我对Xamarin Forms完全陌生,但我设法创建了一个具有多个页面的简单应用程序,并且我可以在页面之间导航。Xamarin Forms自动图像SlideShow /旋转木马

我已经成功添加了图片,按钮和其他基本控件,看起来不错。

我的问题是,我不知道如何创建一个页面上的多个图像的自动轮播。任何谷歌搜索都会返回CarouselPage,使用户可以轻扫屏幕来更改页面。

我正在考虑一个水平卷轴与3个图像,但它并没有真正具有相同的效果 - 用户将不得不通过图像移动自己!

有没有人找到一种方法来做到这一点?任何提示或提示都会很棒!

回答

2

可以交流#计时器,旋转木马页面,contentPage的组合与图片上我做了类似的事情,但使用按钮导航转盘页面的后台代码:

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

namespace SEEForgeX.Views 
{ 
class CarouselView : CarouselPage 
{ 
    ContentPage image1,image2,image3,image4; 
    public CarouselView() 
    { 
     NavigationPage.SetHasNavigationBar(this, false); 

     string btnPrevTitle = "< Prev"; 
     string btnNextTitle = "Next >"; 
     Color btnColor = Color.FromRgba(0, 0, 0, 0.5); 
     Color btnTextColor = Color.White; 
     LayoutOptions btnPosY = LayoutOptions.EndAndExpand; 
     LayoutOptions btnPrevPosX = LayoutOptions.StartAndExpand; 
     LayoutOptions btnNextPosX = LayoutOptions.EndAndExpand; 
     Font buttonFont = Font.SystemFontOfSize(16, FontAttributes.Bold); 
     int btnWidth = 100; 
     string exitBtnImg = "close.png"; 

     Button nextBtn1 = new Button 
     { 
      Text = btnNextTitle, 
      BackgroundColor = btnColor, 
      TextColor = btnTextColor, 
      VerticalOptions = btnPosY, 
      HorizontalOptions = btnNextPosX, 
      Font = buttonFont, 
      WidthRequest = btnWidth 
     }; 

     Button prevBtn2 = new Button 
      { 
       Text = btnPrevTitle, 
       BackgroundColor = btnColor, 
       TextColor = btnTextColor, 
       VerticalOptions = btnPosY, 
       HorizontalOptions = btnPrevPosX, 
       Font = buttonFont, 
       WidthRequest = btnWidth 
      }; 

     image1 = new ContentPage 
     { 
      BackgroundImage = "slide_01.jpg", 
      Content = new StackLayout 
      { 
       HorizontalOptions = LayoutOptions.FillAndExpand, 
       VerticalOptions = LayoutOptions.FillAndExpand, 
       Orientation = StackOrientation.Vertical, 
       Padding = 0, 

       Children = { 
        new StackLayout 
        { 
         Orientation = StackOrientation.Horizontal, 
         VerticalOptions = LayoutOptions.StartAndExpand, 
         Padding = new Thickness(0,10,10,0), 
         Children = { exitBtn1 } 
        }, 
        new StackLayout 
        { 
         Orientation = StackOrientation.Horizontal, 
         VerticalOptions = LayoutOptions.EndAndExpand, 
         Padding = 20, 
         Children = { nextBtn1 } 
        } 
       } 
      }, 
     }; 

     image2 = new ContentPage 
     { 
      BackgroundImage = "slide_02.jpg", 
      Content = new StackLayout 
      { 
       HorizontalOptions = LayoutOptions.FillAndExpand, 
       VerticalOptions = LayoutOptions.FillAndExpand, 
       Orientation = StackOrientation.Vertical, 
       Padding = 0, 
       Children = { 
        new StackLayout 
        { 
         Orientation = StackOrientation.Horizontal, 
         VerticalOptions = LayoutOptions.StartAndExpand, 
         Padding = new Thickness(0,10,10,0), 
         Children = { exitBtn2 } 
        }, 
        new StackLayout 
        { 
         Orientation = StackOrientation.Horizontal, 
         VerticalOptions = LayoutOptions.EndAndExpand, 
         Padding = 20, 
         Children = {prevBtn2, nextBtn2} 
        } 
       } 
      }, 

     }; 
//This is the children of the parent view is like adding stacklayout.children.add(foo) but since    my parent class is a CarouselPage I can access Children its children 
      Children.Add(image1); 
      Children.Add(image2); 

    void prevBtn2_Clicked(object sender, EventArgs e) 
    { 
     this.CurrentPage = image1; 
    } 

    void nextBtn1_Clicked(object sender, EventArgs e) 
    { 
     this.CurrentPage = image2; 
    } 

    private async void exitBtn_Clicked(object sender, EventArgs e) 
    { 
     //await Navigation.PopModalAsync(); 
     await Navigation.PopModalAsync(); 
    } 

我不执行计时器,但它应该不难,也许你甚至可以使用循环。

+0

嗨马里奥,谢谢你的回复。看起来这是我将采取的路线。我在想同样的思路,但我希望能够在旋转木马控制中建立某种内容......谢谢。 – Rick 2014-12-04 15:41:58

+0

@Rick:嗨Rick,您是否找到了自动幻灯片的解决方案?我想要一个在我的登录页面上运行的自动幻灯片。我无法在Xamarin.Form Portable中使用c#timer: – 2016-07-18 18:51:01

+0

@minamorsali也许你可以使用Device.StartTimer? https://developer.xamarin.com/api/member/Xamarin.Forms.Device.StartTimer/p/System .TimeSpan/System.Func%7BSystem.Boolean%7D/ – 2016-07-18 21:44:16