Xamarin.forms如何在运行时更改View属性

时间:2016-03-30 20:29:03

标签: xamarin.forms

我有这段代码:

StackLayout stackLayout = new StackLayout {

    Spacing = 0,
    Children = 
    {
        new Label
        {
            Text = "StackLayout",
            HorizontalOptions = LayoutOptions.Start,
            FontSize = 20
        }
    }

}

什么是在运行时更改空间属性和FontSize的最佳方法?

1 个答案:

答案 0 :(得分:0)

您有对StackLayout的引用,因此要更改它的属性:

stackLayout.Spacing = 10;

要更改标签,您需要保留对它的引用:

Label label = new Label
        {
            Text = "StackLayout",
            HorizontalOptions = LayoutOptions.Start,
            FontSize = 20
        };

StackLayout stackLayout = new StackLayout {

    Spacing = 0,
    Children = 
    {
      label
    }
}

然后你可以在运行时用

修改它
label.FontSize = 30;