简体   繁体   中英

How to use UIPageControl in Xamarin C# for iOS

I have worked on titanium in past and it was very simple to use the paging control there. Just begun Xamarin iOS development and got stuck with this paging control. Not sure how to use it. I found an example here . It worked fine. My question is ,is the UIPageControl Useless ? I read it here that it is so. Does Xamarin have any control which could work like Titanium's Scrollable View ? Please suggest any better method than the code below .

Here's the code

 public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();


        // Perform any additional setup after loading the view, typically from a nib.
        createAndAddViewsToPagerControl ();
    }

    void createAndAddViewsToPagerControl ()
    {
        try {
            int pageCount = PagerControlMain.Pages;
            Console.WriteLine ("Pages Count in Page Control" + pageCount);


            List<UIColor> _colList = new List<UIColor> {
                UIColor.Red,
                UIColor.Green,
                UIColor.Blue,
                UIColor.Yellow
            };

            int i = 0;
            for (i = 0; i < pageCount; i++) {
                UILabel label = new UILabel ();
                RectangleF rectangle = new RectangleF ();
                rectangle.X = (this.PagerScrollView.Frame.Width * i) + 50;
                rectangle.Y = 0;
                rectangle.Size = this.PagerScrollView.Frame.Size;
                label.Frame = rectangle;
                label.Text = i.ToString ();
                label.TextColor = _colList [i];

                this.PagerScrollView.AddSubview (label);
            }

            this.PagerControlMain.Pages = pageCount;
            PagerScrollView.BackgroundColor = UIColor.Gray;
            PagerScrollView.ContentSize = new SizeF (PagerScrollView.Frame.Width * i, PagerScrollView.Frame.Height);
            this.PagerScrollView.Scrolled += ScrolledEvent;


        } catch (Exception ex) {
            Console.WriteLine ("Exception in PagerControl : " + ex);
        }
    }

    private void ScrolledEvent (object sender, EventArgs e)
    {
        this.PagerControlMain.CurrentPage = 
            (int)System.Math.Floor (this.PagerScrollView.ContentOffset.X
        / this.PagerScrollView.Frame.Size.Width);

        Console.WriteLine ("Scroll Event Fired");
    }

    partial void PagerControlMain_ValueChanged (UIPageControl sender)
    {
        Console.WriteLine ("Current Page " + sender.CurrentPage);
        //throw new NotImplementedException ();
    }

Is this what you want?

Xamarin: UIPageControl and UIScrollView for iOS homescreen look

you should add this in your createAndAddViewsToPagerControl () method:

PagerScrollView.ScrollEnabled = true;
PagerScrollView.PagingEnabled = true;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM