简体   繁体   English

如何使用C#代码和XAML在xamarin.forms中使用成语

[英]how to use Idioms in xamarin.forms using C# code and XAML

How to use idiom to set Grid (height, width) padding, margin and to set label font size in xamarin forms using C# and XAML code. 如何使用成语使用C#和XAML代码以xamarin形式设置Grid(高度,宽度)填充,边距以及设置标签字体大小。

I have an example of use Rectangle with StackLayout. 我有一个在StackLayout中使用Rectangle的示例。 But i am not aware of using it with other controls. 但我不知道将其与其他控件一起使用。

<StackLayout Spacing="10" AbsoluteLayout.LayoutFlags="All" BackgroundColor="#0E517B" Padding="0,30,0,0">
<StackLayout.AbsoluteLayout.LayoutBounds>
    <OnIdiom x:TypeArguments="Rectangle" Phone="0.5,1,1,0.80" Tablet="1,0,0.5,1" />
</StackLayout.AbsoluteLayout.LayoutBounds></StackLayout>

You can use it with virtually any property of any type of object you use in XAML. 您几乎可以将其与XAML中使用的任何类型的对象的任何属性一起使用。

Just use the right property and get the correct type of the argument it needs. 只需使用正确的属性并获取所需的正确参数类型即可。

For instance, if you want to set the Spacing in a Grid , just do this: 例如,如果要在Grid设置Spacing ,只需执行以下操作:

<Grid VerticalOptions="FillAndExpand">
  <Grid.ColumnSpacing>
    <OnIdiom x:TypeArguments="x:Double"
             Phone="20"
             Tablet="40"/>
 </Grid.ColumnSpacing>
  <Grid.RowSpacing>
    <OnIdiom x:TypeArguments="x:Double"
             Phone="10"
             Tablet="20"/>
  </Grid.RowSpacing>
  <Grid.Padding>
    <OnIdiom x:TypeArguments="Thickness"
             Phone="10, 10, 10, 0"
             Tablet="20, 20, 20, 0"/>
  </Grid.Padding>
  <!-- Grid Content -->
</Grid>

The things to note here is that we set the ColumnSpacing by adding a child node to the Grid and as a child of that we use the OnIdiom . 这里要注意的事情是,我们设定的ColumnSpacing通过添加一个子节点到Grid ,并作为一个孩子,我们使用OnIdiom If you would want to do something different for a platform there is also the OnPlatform . 如果您想为平台做一些不同的事情,那么还有OnPlatform

The only thing you need to figure out is what the x:TypeArguments has to be. 您唯一需要弄清楚的是x:TypeArguments是什么。 This is the type of object that you are trying to assign as a value. 这是您尝试分配为值的对象的类型。 In the case above, you would have to check what the type of Grid.ColumnSpacing is, which is a Double . 在上述情况下,您必须检查Grid.ColumnSpacing的类型是Double

For more on this check this blog post by James Montemagno. 有关此内容的更多信息,请查看James Montemagno的博客文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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