10. 布局

About页面使用Grid和StackLayout控件来安排其子元素。Grid控件允许定义行和列——就像使用UWP中的Grid控件一样——使用Auto、*、和固定大小设置:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="BooksAppX.Views.AboutPage">
    <ContentPage.IconImageSource>
        <!--<OnPlatform x:TypeArguments="FileImageSource">
            <On Platform="iOS" Value="tab_about.png"/>
        </OnPlatform>-->
        <FileImageSource File="tab_about.png"/>
    </ContentPage.IconImageSource>
    <ContentPage.Content>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="Fill"
                         BackgroundColor="Black">
                <StackLayout Orientation="Horizontal" HorizontalOptions="Center"
                             VerticalOptions="Center">
                    <ContentView Padding="0,40" VerticalOptions="FillAndExpand">
                        <Image VerticalOptions="Center"
                               HeightRequest="64"
                               Source="xamarin_logo.png">
                            <!--<Image.Source>
                                <OnPlatform x:TypeArguments="FileImageSource">
                                    <On Platform="UWP" Value="Assets/xamarin_logo.png"/>
                                    <On Platform="iOS" Value="tab_feed.png"/>
                                </OnPlatform>
                            </Image.Source>-->
                        </Image>
                    </ContentView>
                </StackLayout>
            </StackLayout>
            <ScrollView Grid.Row="1">
                <StackLayout Orientation="Vertical" Padding="16,40" Spacing="10">
                    <Label FontSize="22">
                        <Label.FormattedText>
                            <FormattedString>
                                <FormattedString.Spans>
                                    <Span Text="AppName" FontAttributes="Bold" FontSize="22"/>
                                    <Span Text=" "/>
                                    <Span Text="1.0" TextColor="Blue"/>
                                </FormattedString.Spans>
                            </FormattedString>
                        </Label.FormattedText>
                    </Label>
                    <Label>
                        <Label.FormattedText>
                            <FormattedString>
                                <FormattedString.Spans>
                                    <Span Text="This is a sample app for "/>
                                    <Span Text="Professional C# and .NET Core 2.0"
                                          FontAttributes="Bold"/>
                                    <Span Text="."/>
                                </FormattedString.Spans>
                            </FormattedString>
                        </Label.FormattedText>
                    </Label>
                    <Label>
                        <Label.FormattedText>
                            <FormattedString>
                                <FormattedString.Spans>
                                    <Span Text="It shares code with"/>
                                    <Span Text=" "/>
                                    <Span Text="iOS, Android, and Windows" FontAttributes="Bold"/>
                                    <Span Text="."/>
                                </FormattedString.Spans>
                            </FormattedString>
                        </Label.FormattedText>
                    </Label>
                    <Button Margin="0,10,0,0" Text="Learn more"
                            BackgroundColor="Pink" TextColor="Black">

                    </Button>
                </StackLayout>
            </ScrollView>
        </Grid>
    </ContentPage.Content>
</ContentPage>

下表列出了Xamarin.Forms的布局控件,以及它们与Windows面板控件的比较。

About页面还有其他一些有趣的方面。使用OnPlatform元素,可以根据平台定义不同的XAML元素和属性。使用Image元素,将Source属性设置为一个PNG文件。该文件对Android和iOS是有效的,但是对于UWP,该文件存储在Assets文件夹中。只有UWP中的Source属性的设置是不同的,它由OnPlatform元素定义:

                            <Image.Source>
                                <OnPlatform x:TypeArguments="FileImageSource">
                                    <On Platform="UWP" Value="Assets/xamarin_logo.png"/>
                                </OnPlatform>
                            </Image.Source>

另一个有趣的部分是Label元素。与UWP中的TextBlock一样,Label不仅可以包含简单的文本,还可以包含用不用字体和颜色格式化的文本。使用带有Label的不同文本格式,需要将FormattedText属性设置为FormattedString:

                    <Label>
                        <Label.FormattedText>
                            <FormattedString>
                                <FormattedString.Spans>
                                    <Span Text="This is a sample app for "/>
                                    <Span Text="Professional C# and .NET Core 2.0"
                                          FontAttributes="Bold"/>
                                    <Span Text="."/>
                                </FormattedString.Spans>
                            </FormattedString>
                        </Label.FormattedText>
                    </Label>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值