win8/Metro开发系列一 Xaml布局

首先要懂得一些基础的布局知识,这样在开发的过程中才能游刃有余.

布局主要写到xam里面,在xaml基本的主要涉及到三个布局控件Canvas,StackPanel,Grid

概括起来很简单

1.Canvas就是根据坐标、大小进行绝对定位布局。

示例代码

1 <Canvas>
2             <Button Canvas.Left="28" Canvas.Top="26" Content="Button" Height="45" Width="92" />
3 </Canvas>

 

Canvas.Top 属性指定控件左上角的纵坐标;  Canvas.Left 属性指定控件左上角的横坐标。

由于Canvas是绝对定位我没怎么用过只简单的说下 ,用的最多的还是他的z-index的附加属性;

补充:代码设置 Top Left 等附加属性的方法: Canvas.SetTop
 
2.StackPanel是把子控件横向或者纵向排列。用Orientation属性设定排列方向:Horizontal(水平)、Vertical(纵向,默认)
示例代码:
View Code

3.Grid他类似于html的<Table></Table>标签,由于我是从web开发转过来的所以特别喜欢它,基本上和table类似由行和列构成的

<RowDefinition ><ColumnDefinition >可以用这俩属性类定义他的行和列,并用Grid.Row,Grid.Column附加属性,来设置grid容器里的空间位于哪行哪列,Grid.RowSpan等可以用来跨行列,总之很方面

注:在用来定义行列大小高度的时候还可以用百分比,稍微变动不是加%而是用*,默认width=“1*”是平分高了分得多少了分的少可以自己去感受下同时*是表示剩下的全部,Auto:自动

此外这些基本布局控件,都是容器,就像你的web开发中的div就Ok了~还会有一个一开始我经常犯得错误!就是button的content属性他是Object类型想对里装俩控件会报错说存在重复的属性,其实想通了很简单你给content赋值给他同时赋俩值它没那么智能不知道你到底想给他赋什么所以就报错了,这时你只需要给他一个容器包裹着再赋给它就Ok了!

下面闲话少扯泳用这些基本的控件来布局一个

这是做的项目的第一页截图接下来就看其简单的实现,这是一开始尝试做的没用样式和资源比较繁琐和乱套,下节用样式和资源来重构一下,不过对练布局还好吧

代码示例:

复制代码
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition ></RowDefinition>
            <RowDefinition Height="3.5*"></RowDefinition>
            <RowDefinition ></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition ></ColumnDefinition>
            <ColumnDefinition Width="8*"></ColumnDefinition>
            <ColumnDefinition ></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <TextBlock Text="妙笔生花" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Margin="50,60,0,0" FontSize="48"></TextBlock>
        <Grid Grid.Column="1" Grid.Row="1" Name="auto">
            <Grid.ColumnDefinitions>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Button  Grid.Row="0" Grid.Column="0" Click="OnClick">
                <Button.Content>
                    <ContentControl>
                        <Grid >
                            <Grid.RowDefinitions>
                                <RowDefinition Height="4*" ></RowDefinition>
                                <RowDefinition></RowDefinition>
                            </Grid.RowDefinitions>
                            <Image Source="Images/Grop_Item00.jpg" Grid.RowSpan="2" ></Image>
                            <Image Grid.Row="1" Source="Images/11.png"></Image>
                            <TextBlock Text="电脑对联生成器"   Grid.Row="1" FontSize="30" HorizontalAlignment="Center">
                                <TextBlock.Foreground>
                                    <ImageBrush ImageSource="Images/background.png">
                                    </ImageBrush>
                                </TextBlock.Foreground>
                            </TextBlock>
                        </Grid>
                    </ContentControl>
                </Button.Content>
            </Button>
            <Button  Grid.Row="0" Grid.Column="1" Click="OndicClick">
                <Button.Content>
                    <ContentControl>
                        <Grid >
                            <Grid.RowDefinitions>
                                <RowDefinition Height="4*" ></RowDefinition>
                                <RowDefinition></RowDefinition>
                            </Grid.RowDefinitions>
                            <Image Source="Images/Grop_Item10.jpg" Grid.RowSpan="2" ></Image>
                            <Image Grid.Row="1" Source="Images/11.png"></Image>
                            <TextBlock Text="中华大字典"   Grid.Row="1" FontSize="30" HorizontalAlignment="Center"  >
                                <TextBlock.Foreground>
                                    <ImageBrush ImageSource="Images/backGround.png">
                                    </ImageBrush>
                                </TextBlock.Foreground>
                            </TextBlock>
                        </Grid>
                    </ContentControl>
                </Button.Content>
            </Button>
            <Button  Grid.Row="0" Grid.Column="2">
                <Button.Content>
                    <ContentControl>
                        <Grid >
                            <Grid.RowDefinitions>
                                <RowDefinition Height="4*" ></RowDefinition>
                                <RowDefinition></RowDefinition>
                            </Grid.RowDefinitions>
                            <Image Source="Images/Grop_Item02.jpg" Grid.RowSpan="2" ></Image>
                            <Image Grid.Row="1" Source="Images/11.png"></Image>
                            <TextBlock Text="诗歌生成器"   Grid.Row="1" FontSize="30" HorizontalAlignment="Center"  >
                                <TextBlock.Foreground>
                                    <ImageBrush ImageSource="Images/backGround.png">
                                    </ImageBrush>
                                </TextBlock.Foreground>
                            </TextBlock>
                        </Grid>
                    </ContentControl>
                </Button.Content>
            </Button>
            <Button  Grid.Row="1" Grid.Column="0">
                <Button.Content>
                    <ContentControl>
                        <Grid >
                            <Grid.RowDefinitions>
                                <RowDefinition Height="4*" ></RowDefinition>
                                <RowDefinition></RowDefinition>
                            </Grid.RowDefinitions>
                            <Image Source="Images/Grop_Item01.jpg" Grid.RowSpan="2" ></Image>
                            <Image Grid.Row="1" Source="Images/11.png"></Image>
                            <TextBlock Text="趣味经典对联"   Grid.Row="1" FontSize="30" HorizontalAlignment="Center"  >
                                <TextBlock.Foreground>
                                    <ImageBrush ImageSource="Images/backGround.png">
                                    </ImageBrush>
                                </TextBlock.Foreground>
                            </TextBlock>
                        </Grid>
                    </ContentControl>
                </Button.Content>
            </Button>
            <Button  Grid.Row="1" Grid.Column="1" Click="OnPoetryClick">
                <Button.Content>
                    <ContentControl>
                        <Grid >
                            <Grid.RowDefinitions>
                                <RowDefinition Height="4*" ></RowDefinition>
                                <RowDefinition></RowDefinition>
                            </Grid.RowDefinitions>
                            <Image Source="Images/Grop_Item11.jpg" Grid.RowSpan="2" ></Image>
                            <Image Grid.Row="1" Source="Images/11.png"></Image>
                            <TextBlock Text="诗歌大全"   Grid.Row="1" FontSize="30" HorizontalAlignment="Center"  >
                                <TextBlock.Foreground>
                                    <ImageBrush ImageSource="Images/backGround.png">
                                    </ImageBrush>
                                </TextBlock.Foreground>
                            </TextBlock>
                        </Grid>
                    </ContentControl>
                </Button.Content>
            </Button>
            <Button  Grid.Row="1" Grid.Column="2" Click="OnallegoricalClick">
                <Button.Content>
                    <ContentControl>
                        <Grid >
                            <Grid.RowDefinitions>
                                <RowDefinition Height="4*" ></RowDefinition>
                                <RowDefinition></RowDefinition>
                            </Grid.RowDefinitions>
                            <Image Source="Images/Grop_Item12.jpg" Grid.RowSpan="2" ></Image>
                            <Image Grid.Row="1" Source="Images/11.png"></Image>
                            <TextBlock Text="歇后语大全"   Grid.Row="1" FontSize="30" HorizontalAlignment="Center"  >
                                <TextBlock.Foreground>
                                    <ImageBrush ImageSource="Images/backGround.png">
                                    </ImageBrush>
                                </TextBlock.Foreground>
                            </TextBlock>
                        </Grid>
                    </ContentControl>
                </Button.Content>
            </Button>
        </Grid>
    </Grid>
复制代码

仅个人意见,初学还有很多不懂欢迎大家批评指导

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值