为Xamarin创建一个不使用XAML的ControlTemplate



不使用XAML,有人有一个例子,他们创建一个元素的控制模板?

的例子:

<ControlTemplate>
<Grid>
<//Inner grid elements>
</Grid>
</ControlTemplate>

to C# code

var grid = new Grid();
//This isnt how you do it Im stuck here 
var ControlTemplate = new ControlTemplate(grid)

您可以参考下面的代码。

public class Page1 : ContentPage
{
public Page1()
{
ControlTemplate controlTemplate = new ControlTemplate(typeof(Grid1));
ControlTemplate = controlTemplate;
}
}
public class Grid1 : ContentView
{
public Grid1()
{
Grid grid = new Grid()
{
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand,
RowDefinitions =
{
new RowDefinition{Height =GridLength.Auto},
new RowDefinition{Height =GridLength.Auto},
},
ColumnDefinitions =
{
new ColumnDefinition{ Width= GridLength.Auto},
new ColumnDefinition{ Width= GridLength.Auto},
},
};
grid.Children.Add(new Label
{
Text = "column0,row0",
}, 0, 0);
grid.Children.Add(new Label
{
Text = "column1,row1"
}, 1, 1);
Content = grid;
}
}

最新更新