【发布时间】:2018-07-06 14:47:23
【问题描述】:
我是 Xamarin.Forms 和 Xaml 的新手。我制作了一个自定义控件,我想在整个应用程序中用作自定义背景。自定义控件被制作成一个内容视图,看起来像这样。
<ContentView.Content>
<ScrollView>
<StackLayout>
<RelativeLayout Padding="0" BackgroundColor="Teal" VerticalOptions="Start">
<Image Source="TopBG" BackgroundColor="Purple" Aspect="Fill" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.6}" />
</RelativeLayout>
<Frame Padding="0" Margin="0,-25,0,0" CornerRadius="25" BackgroundColor="{StaticResource Key=Charcoal}">
<StackLayout Margin="0,0,0,0" VerticalOptions="StartAndExpand" BackgroundColor="Transparent" x:Name="InnerStack">
<!--I can insert custom controls here, but htat would determine this custom contentView for one purpose only-->
</StackLayout>
</Frame>
</StackLayout>
</ScrollView>
</ContentView.Content>
然后在我的 ContentPage 上实现自定义控件,如下所示:
<ContentPage.Content>
<CustomLayouts:ContentBackground>
<!-- I would instead like to be able to add content here, and have it go into the stacklayout of the custom view.
This way i could utilize the same background but have different content go into it depending on my wishes -->
</CustomLayouts:ContentBackground>
</ContentPage.Content>
如果我在后面的示例中添加一个标签,它只会覆盖所有内容并放置一个标签,但不是按预期在我的 ContentBackground 的指定内部堆栈视图中。
我唯一能想到的就是想办法访问我的自定义 contentBackground 的 InnerStackView,然后访问该 Stacklayout 的 children 属性,然后访问该堆栈布局的 Children.add(View)。虽然这意味着我必须从代码中完成,我想在 XAML 中实现这种行为,因为这对我来说更熟悉。
这是实现我在 XAML 中目标的唯一方法还是有替代方法?
【问题讨论】:
标签: c# xaml xamarin.forms app.xaml