Xamarin.Forms如何为View添加背景色

  由于Xamarin.Forms的面世的时间不到一年,虽然朝着wpf的方面前行,但是还有很多地方不完善,譬如最基本的,View的背景图片无法设置,BackgroundImage只是在Page中才有.

所以,只能找变通的方式了.

  个人能想到的方式是在View下面叠加一个Image,而forms中能叠加控件的layout有两种:Grid和RelativeLayout

1.RelativeLayout方式

  跟大多数的UI设计一样,RelativeLayout下的View都是叠加在左上角

  设置View的附加属性:

  RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,Property=Width,Factor=1}"

  RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent,Property=Height,Factor=1}"

  使View的长宽和Layout的长宽一致,先加一个Image,后面加需要设置背景图片的View

2.Grid方式

  原理都一样,设置View的附加属性在同一Column和Row就行了

  设置View的附加属性:

  Grid.Column=""  Grid.Row=""

如果谁有更好的方式,欢迎讨论.

原文地址:https://www.cnblogs.com/yz1311/p/4442348.html