我有这段代码:
<?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)设置为参数。
有人可以提供一些建议,告诉我如何将其作为一个类实现,然后我可以将其添加到我的应用程序中:
new CommentBlock(60, "ABC");
答案 0 :(得分:2)
我相信你应该能够通过在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#等价物(我为comment-text添加了几个可绑定属性,并且为了允许基于XAML的用法添加了grid-height)。
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" />