在 ContentView 中嵌套内容
Nest Content inside ContentView
不知道标题够不够清楚,所以让我解释一下我正在尝试做的事情:
我有一个 LoggingField
class 由以下组成:
XAML:
<ContentView.Content>
<StackLayout>
<Label Style="{DynamicResource LabelTitle}" x:Name="LabelTitle"/>
<View x:Name="FieldContent"/>
</StackLayout>
</ContentView.Content>
C#:
[ContentProperty(nameof(Field))]
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class LoggingField : ContentView
{
/// <summary>
/// Creates a new Logging field.
/// </summary>
public LoggingField()
{
InitializeComponent();
}
//
// PUBLIC
//
// PROPERTIES
/// <summary>
/// The field's content.
/// </summary>
public View Field
{
set => FieldContent = value;
get => FieldContent;
}
/// <summary>
/// The field's title.
/// </summary>
public string Text
{
set => LabelTitle.Text = value;
get => LabelTitle.Text;
}
}
第XAML页:
<logcontent:LoggingField Text="Test Title:">
<Entry Placeholder="PlaceholderText"/>
</logcontent:LoggingField>
从中可以看出,我希望 <Entry Placeholder="PlaceholderText"/>
覆盖 <View x:Name="FieldContent"/>
object,但我无法实现。它总是会覆盖整个 ContentView,只显示占位符 Entry
.
我所做的唯一进步是在 ContentView 的 C# class 中添加了 [ContentProperty(nameof(Field))]
,这使其原始内容得以显示,但随后 Entry
object 获胜不再出现了。
那么如何让 Entry
object 正确覆盖 ContentView 中的 View
object?提前致谢。
PS:我尝试用 <ContentPresenter x:Name="FieldContent"/>
替换 <View x:Name="FieldContent"/>
并改用 ContentPresenter.Content
,但无济于事。
你可以在 code behind(viewmodel) 中创建 ControlTemplate
,然后在 xaml 中绑定它。
代码隐藏
ControlTemplate MyTemplate = new ControlTemplate(()=> {
return new Entry { Placeholder= "PlaceholderText" };
});
第Xaml页
<local:LoggingField ControlTemplate="{Binding MyTemplate}"/>
However, this is total not recommended , the best way is to place ControlTemplate
in xaml .
不知道标题够不够清楚,所以让我解释一下我正在尝试做的事情:
我有一个 LoggingField
class 由以下组成:
XAML:
<ContentView.Content>
<StackLayout>
<Label Style="{DynamicResource LabelTitle}" x:Name="LabelTitle"/>
<View x:Name="FieldContent"/>
</StackLayout>
</ContentView.Content>
C#:
[ContentProperty(nameof(Field))]
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class LoggingField : ContentView
{
/// <summary>
/// Creates a new Logging field.
/// </summary>
public LoggingField()
{
InitializeComponent();
}
//
// PUBLIC
//
// PROPERTIES
/// <summary>
/// The field's content.
/// </summary>
public View Field
{
set => FieldContent = value;
get => FieldContent;
}
/// <summary>
/// The field's title.
/// </summary>
public string Text
{
set => LabelTitle.Text = value;
get => LabelTitle.Text;
}
}
第XAML页:
<logcontent:LoggingField Text="Test Title:">
<Entry Placeholder="PlaceholderText"/>
</logcontent:LoggingField>
从中可以看出,我希望 <Entry Placeholder="PlaceholderText"/>
覆盖 <View x:Name="FieldContent"/>
object,但我无法实现。它总是会覆盖整个 ContentView,只显示占位符 Entry
.
我所做的唯一进步是在 ContentView 的 C# class 中添加了 [ContentProperty(nameof(Field))]
,这使其原始内容得以显示,但随后 Entry
object 获胜不再出现了。
那么如何让 Entry
object 正确覆盖 ContentView 中的 View
object?提前致谢。
PS:我尝试用 <ContentPresenter x:Name="FieldContent"/>
替换 <View x:Name="FieldContent"/>
并改用 ContentPresenter.Content
,但无济于事。
你可以在 code behind(viewmodel) 中创建 ControlTemplate
,然后在 xaml 中绑定它。
代码隐藏
ControlTemplate MyTemplate = new ControlTemplate(()=> {
return new Entry { Placeholder= "PlaceholderText" };
});
第Xaml页
<local:LoggingField ControlTemplate="{Binding MyTemplate}"/>
However, this is total not recommended , the best way is to place
ControlTemplate
in xaml .