如何在 C# 中将 ViewCell、Grid 和 Label 添加为 class?
How can I implement the adding of a ViewCell, Grid and Label in C# as a class?
我有这个代码:
<?xml version="1.0" encoding="UTF-8"?>
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="CommentBlock">
<Grid HeightRequest="60" VerticalOptions="CenterAndExpand" Padding="20,10" BackgroundColor="#EAEAF1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label Style="{DynamicResource ListItemDetailTextStyleStyle}" TextColor="#59595F" Text="ABC" HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand" />
</Grid>
</ViewCell>
现在它是在XAML中实现的,但我需要在C#中实现并且可以将文本消息(当前为ABC)和高度(50)设置为参数。
有人可以就如何将其实现为 class 提出一些建议,然后我可以将其添加到我的应用程序中:
new CommentBlock(60, "ABC");
我相信你应该能够通过简单地在 CommentBlock
的代码隐藏中添加一个构造函数来实现这一点,并且仍然继续使用基于 XAML 的 CommentBlock
:
public CommentBlock(string text, double height)
{
InitializeComponent();
_commentLbl.Text = text;
_containerGrid.HeightRequest = height;
}
并在 CommentBlock
的 XAML 中添加对内部控件的命名引用:
<Grid x:Name="_containerGrid" ..>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label x:Name="_commentLbl" ../>
</Grid>
编辑:08/23
但是如果您特别寻找基于 C# 的实现,那么下面将是您的 XAML 的 C# 等价物(我为评论文本和网格高度添加了几个可绑定属性以允许 XAML 基于用法)。
public class CommentBlockCell : ViewCell
{
public static readonly BindableProperty CommentTextProperty = BindableProperty.Create(
"CommentText", typeof(string), typeof(CommentBlockCell),
defaultValue: null);
public string CommentText
{
get { return (string)GetValue(CommentTextProperty); }
set { SetValue(CommentTextProperty, value); }
}
public static readonly BindableProperty GridHeightProperty = BindableProperty.Create(
"GridHeight", typeof(double), typeof(CommentBlockCell),
defaultValue: 30.0);
public double GridHeight
{
get { return (double)GetValue(GridHeightProperty); }
set { SetValue(GridHeightProperty, value); }
}
public CommentBlockCell()
{
BuildControl();
}
public CommentBlockCell(double gridHeight, string comment)
{
CommentText = comment;
GridHeight = gridHeight;
BuildControl();
}
private void BuildControl()
{
var grid = new Grid
{
VerticalOptions = LayoutOptions.CenterAndExpand,
Padding = new Thickness(20, 10),
BackgroundColor = Color.FromHex("#EAEAF1"),
RowDefinitions = new RowDefinitionCollection {
new RowDefinition { Height = GridLength.Auto }
},
ColumnDefinitions = new ColumnDefinitionCollection {
new ColumnDefinition { Width = GridLength.Auto }
}
};
var label = new Label
{
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.CenterAndExpand
};
//label.SetDynamicResource(VisualElement.StyleProperty, "ListItemDetailTextStyleStyle");
label.SetBinding(Label.TextProperty, new Binding(nameof(CommentText), source: this));
grid.SetBinding(VisualElement.HeightRequestProperty, new Binding(nameof(GridHeight), source: this));
grid.Children.Add(label);
View = grid;
}
}
用法可以是:
new CommentBlockCell(60, "ABC");
或在XAML中:
<local:CommentBlockCell CommentText="ABC" GridHeight="60" />
我有这个代码:
<?xml version="1.0" encoding="UTF-8"?>
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="CommentBlock">
<Grid HeightRequest="60" VerticalOptions="CenterAndExpand" Padding="20,10" BackgroundColor="#EAEAF1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label Style="{DynamicResource ListItemDetailTextStyleStyle}" TextColor="#59595F" Text="ABC" HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand" />
</Grid>
</ViewCell>
现在它是在XAML中实现的,但我需要在C#中实现并且可以将文本消息(当前为ABC)和高度(50)设置为参数。
有人可以就如何将其实现为 class 提出一些建议,然后我可以将其添加到我的应用程序中:
new CommentBlock(60, "ABC");
我相信你应该能够通过简单地在 CommentBlock
的代码隐藏中添加一个构造函数来实现这一点,并且仍然继续使用基于 XAML 的 CommentBlock
:
public CommentBlock(string text, double height)
{
InitializeComponent();
_commentLbl.Text = text;
_containerGrid.HeightRequest = height;
}
并在 CommentBlock
的 XAML 中添加对内部控件的命名引用:
<Grid x:Name="_containerGrid" ..>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label x:Name="_commentLbl" ../>
</Grid>
编辑:08/23
但是如果您特别寻找基于 C# 的实现,那么下面将是您的 XAML 的 C# 等价物(我为评论文本和网格高度添加了几个可绑定属性以允许 XAML 基于用法)。
public class CommentBlockCell : ViewCell
{
public static readonly BindableProperty CommentTextProperty = BindableProperty.Create(
"CommentText", typeof(string), typeof(CommentBlockCell),
defaultValue: null);
public string CommentText
{
get { return (string)GetValue(CommentTextProperty); }
set { SetValue(CommentTextProperty, value); }
}
public static readonly BindableProperty GridHeightProperty = BindableProperty.Create(
"GridHeight", typeof(double), typeof(CommentBlockCell),
defaultValue: 30.0);
public double GridHeight
{
get { return (double)GetValue(GridHeightProperty); }
set { SetValue(GridHeightProperty, value); }
}
public CommentBlockCell()
{
BuildControl();
}
public CommentBlockCell(double gridHeight, string comment)
{
CommentText = comment;
GridHeight = gridHeight;
BuildControl();
}
private void BuildControl()
{
var grid = new Grid
{
VerticalOptions = LayoutOptions.CenterAndExpand,
Padding = new Thickness(20, 10),
BackgroundColor = Color.FromHex("#EAEAF1"),
RowDefinitions = new RowDefinitionCollection {
new RowDefinition { Height = GridLength.Auto }
},
ColumnDefinitions = new ColumnDefinitionCollection {
new ColumnDefinition { Width = GridLength.Auto }
}
};
var label = new Label
{
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.CenterAndExpand
};
//label.SetDynamicResource(VisualElement.StyleProperty, "ListItemDetailTextStyleStyle");
label.SetBinding(Label.TextProperty, new Binding(nameof(CommentText), source: this));
grid.SetBinding(VisualElement.HeightRequestProperty, new Binding(nameof(GridHeight), source: this));
grid.Children.Add(label);
View = grid;
}
}
用法可以是:
new CommentBlockCell(60, "ABC");
或在XAML中:
<local:CommentBlockCell CommentText="ABC" GridHeight="60" />