Xamarin学习笔记——4种不同的页面

Xamarin官网介绍了五种不同的页面,分别为:

  • ContentPage
  • MasterDetailPage
  • NavigationPage
  • CarouselPage
  • TabbedPage

下面我们对这几个页面进行一一介绍。

1、Contentpage

A Page that displays a single view.

ContentPage是一个显示一个单一的View的页面,是最简单的一种page。我们用一个简单的例子来说明:
新建一个工程test1,在test1.cs中增加如下代码:

namespace test4
{
class PageA : ContentPage
{

    View CreateColorView(Color color , string name)
    {
        return new Frame
        {
            OutlineColor = Color.Accent,
            Padding = new Thickness(5),
            Content = new StackLayout
            {
                Orientation = StackOrientation.Horizontal,
                Spacing = 15,
                Children = 
                {
                    new BoxView
                    {
                        Color = color
                    },
                    new Label
                    {
                        Text = name,
                        FontSize = Device.GetNamedSize(NamedSize.Large,typeof(Label)),
                        FontAttributes = FontAttributes.Bold,
                        VerticalOptions = LayoutOptions.Center,
                        HorizontalOptions = LayoutOptions.StartAndExpand
                    },

                    new StackLayout
                    {
                        Children = 
                        { 
                        new Label
                        {
                            Text = String.Format("{0:X2}-{1:X2}-{2:X2}",
                                (int)(255*color.R),
                                (int)(255*color.G),
                                (int)(255*color.B)),
                            VerticalOptions = LayoutOptions.CenterAndExpand,
                            IsVisible = color!=Color.Default
                        },

                        new Label
                        {
                            Text = String.Format("{0:F2},{1:F2},{2:F2}",
                                color.Hue,
                                color.Saturation,
                                color.Luminosity),
                            VerticalOptions = LayoutOptions.CenterAndExpand,
                            IsVisible = color != Color.Default
                        }

                    },
                    HorizontalOptions = LayoutOptions.End
                    }
                }
            }
        };
    }
    public PageA ()
    {

        Content = new ScrollView 
        {
            Orientation = ScrollOrientation.Vertical,
            Content = new StackLayout
            {
                Children = 
                {
                    CreateColorView(Color.Aqua,"Aqua"),
                    CreateColorView(Color.Black,"Black"),
                    CreateColorView(Color.Blue,"Blue"),
                    CreateColorView(Color.Fuchsia,"Fuchsia"),
                    CreateColorView(Color.Gray,"Gray"),
                    CreateColorView(Color.Green,"Green"),
                    CreateColorView(Color.Lime,"Lime"),
                    CreateColorView(Color.Maroon,"Maroon"),
                    CreateColorView(Color.Navy,"Navy"),
                    CreateColorView(Color.Olive,"Olive"),
                    CreateColorView(Color.Pink,"Pink"),
                    CreateColorView(Color.Purple,"Purple"),
                    CreateColorView(Color.Red,"Red"),
                    CreateColorView(Color.Silver,"Silver"),
                    CreateColorView(Color.Teal,"Teal"),
                    CreateColorView(Color.White,"White"),
                    CreateColorView(Color.Yellow,"Yellow"),
                    CreateColorView(Color.Aqua,"Aqua"),
                    CreateColorView(Color.Black,"Black"),
                    CreateColorView(Color.Blue,"Blue"),
                    CreateColorView(Color.Fuchsia,"Fuchsia"),
                    CreateColorView(Color.Gray,"Gray"),
                    CreateColorView(Color.Green,"Green"),
                    CreateColorView(Color.Lime,"Lime"),
                    CreateColorView(Color.Maroon,"Maroon"),
                    CreateColorView(Color.Navy,"Navy"),
                    CreateColorView(Color.Olive,"Olive"),
                    CreateColorView(Color.Pink,"Pink"),
                    CreateColorView(Color.Purple,"Purple"),
                    CreateColorView(Color.Red,"Red"),
                    CreateColorView(Color.Silver,"Silver"),
                    CreateColorView(Color.Teal,"Teal"),
                    CreateColorView(Color.White,"White"),
                    CreateColorView(Color.Yellow,"Yellow"),

                }
            }
        };
    }
}
public class App : Application
{
    public App ()
    {
        MainPage = new PageA();
    }

    protected override void OnStart ()
    {
        // Handle when your app starts
    }

    protected override void OnSleep ()
    {
        // Handle when your app sleeps
    }

    protected override void OnResume ()
    {
        // Handle when your app resumes
    }
}
}

通过上面的例子我们可以看出:
我们新建了一个继承于ContentPage 的 PageA, PageA包含了一个用于滑动的ScrollView,ScrollView 内包含一个StackLayout,里面的孩子通过CreateColorView()这一方法来创建。每一个Child里有一个BoxView用来表示颜色,一个Label用于显示颜色名称,以及另外两个小的Label用于显示颜色的RGB值,色相,饱和度等。

2、MasterDetailPage

A Page that manages two panes of information: A master page that presents data at a high level, and a detail page that displays low-level details about information in the master.

页面同事管理着两个不同的信息窗格,一个页面master显示主要的更高级别的数据,一个detail页面显示低级别的,关于master页面的具体信息。

我们还是给个例子。
在刚才的test.cs文件中,我们增加一个PageB:
完整代码如下:

using System;
using System.Reflection;
using System.Collections.Generic;

using Xamarin.Forms;

namespace test4
{
class PageA : ContentPage
{

    View CreateColorView(Color color , string name)
    {
        return new Frame
        {
            OutlineColor = Color.Accent,
            Padding = new Thickness(5),
            Content = new StackLayout
            {
                Orientation = StackOrientation.Horizontal,
                Spacing = 15,
                Children = 
                {
                    new BoxView
                    {
                        Color = color
                    },
                    new Label
                    {
                        Text = name,
                        FontSize = Device.GetNamedSize(NamedSize.Large,typeof(Label)),
                        FontAttributes = FontAttributes.Bold,
                        VerticalOptions = LayoutOptions.Center,
                        HorizontalOptions = LayoutOptions.StartAndExpand
                    },

                    new StackLayout
                    {
                        Children = 
                        { 
                        new Label
                        {
                            Text = String.Format("{0:X2}-{1:X2}-{2:X2}",
                                (int)(255*color.R),
                                (int)(255*color.G),
                                (int)(255*color.B)),
                            VerticalOptions = LayoutOptions.CenterAndExpand,
                            IsVisible = color!=Color.Default
                        },

                        new Label
                        {
                            Text = String.Format("{0:F2},{1:F2},{2:F2}",
                                color.Hue,
                                color.Saturation,
                                color.Luminosity),
                            VerticalOptions = LayoutOptions.CenterAndExpand,
                            IsVisible = color != Color.Default
                        }

                    },
                    HorizontalOptions = LayoutOptions.End
                    }
                }
            }
        };
    }
    public PageA ()
    {

        Content = new ScrollView 
        {
            Orientation = ScrollOrientation.Vertical,
            Content = new StackLayout
            {
                Children = 
                {
                    CreateColorView(Color.Aqua,"Aqua"),
                    CreateColorView(Color.Black,"Black"),
                    CreateColorView(Color.Blue,"Blue"),
                    CreateColorView(Color.Fuchsia,"Fuchsia"),
                    CreateColorView(Color.Gray,"Gray"),
                    CreateColorView(Color.Green,"Green"),
                    CreateColorView(Color.Lime,"Lime"),
                    CreateColorView(Color.Maroon,"Maroon"),
                    CreateColorView(Color.Navy,"Navy"),
                    CreateColorView(Color.Olive,"Olive"),
                    CreateColorView(Color.Pink,"Pink"),
                    CreateColorView(Color.Purple,"Purple"),
                    CreateColorView(Color.Red,"Red"),
                    CreateColorView(Color.Silver,"Silver"),
                    CreateColorView(Color.Teal,"Teal"),
                    CreateColorView(Color.White,"White"),
                    CreateColorView(Color.Yellow,"Yellow"),
                    CreateColorView(Color.Aqua,"Aqua"),
                    CreateColorView(Color.Black,"Black"),
                    CreateColorView(Color.Blue,"Blue"),
                    CreateColorView(Color.Fuchsia,"Fuchsia"),
                    CreateColorView(Color.Gray,"Gray"),
                    CreateColorView(Color.Green,"Green"),
                    CreateColorView(Color.Lime,"Lime"),
                    CreateColorView(Color.Maroon,"Maroon"),
                    CreateColorView(Color.Navy,"Navy"),
                    CreateColorView(Color.Olive,"Olive"),
                    CreateColorView(Color.Pink,"Pink"),
                    CreateColorView(Color.Purple,"Purple"),
                    CreateColorView(Color.Red,"Red"),
                    CreateColorView(Color.Silver,"Silver"),
                    CreateColorView(Color.Teal,"Teal"),
                    CreateColorView(Color.White,"White"),
                    CreateColorView(Color.Yellow,"Yellow"),

                }
            }
        };
    }
}

class PageB : MasterDetailPage
{
    class Person
    {


        public Person(String name , DateTime birthday , Color favoriteColor)
        {
            this.Name = name;
            this.Birthday = birthday;
            this.FavoriteColor = favoriteColor;
        }

        public String Name{ private set; get;}
        public DateTime Birthday{ private set; get;}
        public Color FavoriteColor{ private set; get;}

    }

    public PageB()
    {
        Label header = new Label {
            Text = "MasterDetailPage",
            Font = Font.SystemFontOfSize (30,FontAttributes.Bold),
            HorizontalOptions = LayoutOptions.Center
        };
//          int[] a = new int[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 11, 12, 13, 14 };
        List<Person> people = new List<Person>
        {
            new Person("A",new DateTime(1975 , 1, 1),Color.Aqua),
            new Person("B",new DateTime(1989 , 1, 1),Color.Black),
            new Person("C",new DateTime(1940 , 1, 1),Color.Blue),
            new Person("D",new DateTime(1962 , 1, 1),Color.Fuchsia),
            new Person("E",new DateTime(1991 , 1, 1),Color.Gray),
            new Person("F",new DateTime(1999 , 1, 1),Color.Green),
        };

        ListView listView = new ListView {
            ItemsSource = people,
            ItemTemplate = new DataTemplate(() =>
                {
                    Label nameLable = new Label();
                    nameLable.SetBinding(Label.TextProperty , "Name");
                    Label birthdayLable = new Label();
                    birthdayLable.SetBinding(Label.TextProperty, new Binding("Birthday",BindingMode.OneWay,null,null,"Born{0:d}"));

                    BoxView boxView = new BoxView();
                    boxView.SetBinding(BoxView.ColorProperty,"FavoriteColor");

                    return new ViewCell
                    {
                        View = new StackLayout
                        {
                            Padding = new Thickness(0,5),
                            Orientation = StackOrientation.Horizontal,
                            Children = 
                            {
                                boxView,
                                new StackLayout
                                {
                                    VerticalOptions = LayoutOptions.Center,
                                    Spacing = 0 ,
                                    Children = 
                                    {
                                        nameLable,
                                        birthdayLable
                                    }
                                }
                            }
                        }
                    };
                })
        };
        Master = new ContentPage {
            Title = header.Text,
            Content = new StackLayout {
                Children = {
                    header,
                    listView
                }
            }
        };
        Detail = new NavigationPage (new PageA ());



        listView.ItemSelected += (sender, args) => 
        {
            this.Detail.BindingContext = args.SelectedItem;
            this.IsPresented = false;
        }; 
    }
}
public class App : Application
{
    public App ()
    {
        MainPage = new PageB();
    }

    protected override void OnStart ()
    {
        // Handle when your app starts
    }

    protected override void OnSleep ()
    {
        // Handle when your app sleeps
    }

    protected override void OnResume ()
    {
        // Handle when your app resumes
    }
}
}

这一次我们把主页面指向了PageB
在PageB中我们用一个Label做了一个header,一个listView用于masterPage,DetailPage我们用了之前的PageA加了一个标题栏。
效果如下:
效果如下:
MasterPage
DetailPage

3、NavigationPage

A Page that manages the navigation and user-experience of a stack of other pages.

简单的理解是加了一个标题栏或导航栏的页面。在上一个页面我们已经使用过了。这里就不在举例子了。

4、CarouselPage

A Page that users can swipe from side to side to display pages of content, like a gallery.

这个页面可以像照片查看器那样从左边滑倒右边看上下文的内容
我们还是举一个简单的栗子。

public class App : Application
{
    public App ()
    {
        List<ContentPage> pages = new List<ContentPage>(0);
        Color[] colors = { Color.Red, Color.Green, Color.Blue };
        foreach (Color c in colors)
        {
            pages.Add(new ContentPage
                {
                    Content = new StackLayout
                    {
                        Children =
                        {
                            new Label
                            {
                                Text = c.ToString ()
                            },
                            new BoxView
                            {
                                Color = c,
                                VerticalOptions = LayoutOptions.CenterAndExpand
                            }
                        }
                        }
                });
        }

        MainPage = new CarouselPage
        {
            Children =
            {
                pages [0],
                pages [1],
                pages [2]
            }
            };
    }

5、TabbedPage

class PageC:TabbedPage
{
public PageC()
{
this.Title = “TabbedPage”;
this.Children.Add(new ContentPage
{
Title = “Blue”,
Content = new BoxView
{
Color = Color.Blue,
HeightRequest = 100f,
VerticalOptions = LayoutOptions.Center
},
}
);
this.Children.Add(new ContentPage
{
Title = “Blue and Red”,
Content = new StackLayout
{
Children = {
new BoxView { Color = Color.Blue },
new BoxView { Color = Color.Red}
}
}
});
}
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在移动应用开发中,自动轮播视图是一个非常常见的控件,它可以帮助用户展示多个图片或者内容,使得应用更加生动、丰富。但是在Xamarin中,自动轮播视图并没有原生的控件,需要通过自定义布局来实现。本文将介绍如何使用CarouselView实现支持无限滚动的自动轮播视图。 ## 1. CarouselView简介 CarouselView是一个基于Xamarin.Forms的自定义布局,它可以帮助我们实现类似于ViewPager的效果,支持水平和垂直滚动,并且可以无限滚动。它的主要特点包括: - 支持多布局方式,包括Stack、Wrap、Grid等。 - 支持水平和垂直滚动,以及无限滚动。 - 支持手势滑动和自动轮播。 - 提供了各事件,方便我们进行自定义操作。 ## 2. 安装CarouselView 要使用CarouselView,我们首先需要将其添加到我们的项目中。我们可以通过NuGet包管理器来安装CarouselView,具体操作如下: 1. 在Visual Studio中打开我们的移动应用项目。 2. 打开NuGet包管理器,可以通过菜单栏中的“工具”->“NuGet包管理器”->“管理解决方案的NuGet包”打开。 3. 在NuGet包管理器中搜索“CarouselView.FormsPlugin”,并安装它。 安装完成后,我们就可以在项目中使用CarouselView了。 ## 3. 使用CarouselView 在使用CarouselView之前,我们需要在XAML文件中添加CarouselView的命名空间: ```xml xmlns:cv="clr-namespace:CarouselView.FormsPlugin.Abstractions;assembly=CarouselView.FormsPlugin.Abstractions" ``` 然后在我们的布局中添加CarouselView控件: ```xml <cv:CarouselView x:Name="carouselView" ItemsSource="{Binding ImageUrls}" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Orientation="Horizontal" PositionSelected="OnPositionSelected"> <cv:CarouselView.ItemTemplate> <DataTemplate> <Image Source="{Binding .}" Aspect="AspectFill" /> </DataTemplate> </cv:CarouselView.ItemTemplate> </cv:CarouselView> ``` 上述代码中,我们使用了一个绑定ImageUrls的集合作为CarouselView的数据源,每一个Item都是一个Image控件。我们还可以通过设置HorizontalOptions和VerticalOptions来控制CarouselView的布局方式,设置Orientation来控制滚动的方向。 在代码中,我们可以通过PositionSelected事件来监听当前选中的位置,然后进行自定义操作: ```csharp private void OnPositionSelected(object sender, PositionSelectedEventArgs e) { // Do something } ``` ## 4. 实现无限滚动 上述代码中的CarouselView实现了基本的自动轮播效果,但是它并不能无限滚动。当滚动到最后一个Item时,就会停止滚动,用户需要手动操作才能再次滚动。 要实现无限滚动,我们需要通过自定义布局来实现。具体操作如下: 1. 创建一个新的CarouselView控件,继承自CarouselView控件。 ```csharp public class InfiniteCarouselView : CarouselViewControl { //... } ``` 2. 在控件中实现一个循环滚动的方法,用于在滚动到最后一个Item时将其移动到第一个Item。 ```csharp private void LoopScroll(int position) { if (position == ItemsSource.Count - 1) { Task.Delay(200).ContinueWith(t => { Device.BeginInvokeOnMainThread(() => { this.Position = 0; }); }); } } ``` 3. 在控件的构造函数中监听PositionSelected事件,当滚动到最后一个Item时调用LoopScroll方法。 ```csharp public InfiniteCarouselView() { PositionSelected += (sender, e) => { LoopScroll(e.NewValue); }; } ``` 4. 在页面中使用我们自定义的CarouselView控件。 ```xml <local:InfiniteCarouselView x:Name="carouselView" ItemsSource="{Binding ImageUrls}" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Orientation="Horizontal"> <cv:CarouselView.ItemTemplate> <DataTemplate> <Image Source="{Binding .}" Aspect="AspectFill" /> </DataTemplate> </cv:CarouselView.ItemTemplate> </local:InfiniteCarouselView> ``` 通过以上步骤,我们就可以实现一个支持无限滚动的自动轮播视图了。完整代码示例可以参考我的Github仓库:https://github.com/wangxizhe/CarouselViewDemo 。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值