我可以使用 Xamarin 中的控件模板之类的东西来包围新对象吗?

2024-02-18

我的应用程序具有以下包围的对象:

<StackLayout HeightRequest="49" BackgroundColor="Red" Orientation="Vertical" Margin="0" >
    <Grid VerticalOptions="CenterAndExpand">
        <!-- XAML here -->
    </Grid>
</StackLayout>

我想这样做,这样我就不需要每次在第一行、第二行、最后一行和倒数第二行输入 XAML。

我可以使用控件模板之类的东西来避免这样做吗?如果可以,我将如何使用它?


您将需要使用以下组合ContentPresenter and ControlTemplate(正如注释中提到的这一页 https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/templates/control-templates/creating)

On a ContentPage (or ContentView), the Content属性可以被分配并且ControlTemplate属性也可以设置。当这种情况发生时,如果ControlTemplate包含一个ContentPresenter实例,分配给的内容Content财产将由ContentPresenterControlTemplate.

<ControlTemplate x:Key="DefaultTemplate">
    <StackLayout HeightRequest="49" BackgroundColor="Red"
                 Orientation="Vertical" Margin="0">
        <Grid VerticalOptions="CenterAndExpand">
            <ContentPresenter /> <!-- This line is replaced by actual content -->
        </Grid>
    </StackLayout>
</ControlTemplate>

Usage:

<ContentView ControlTemplate="{StaticResource DefaultTemplate}">
    <!-- XAML here (basically the view that is to be surrounded by above layout) -->
    <Label TextColor="Yellow" Text="I represent the content" />
</ContentView>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我可以使用 Xamarin 中的控件模板之类的东西来包围新对象吗? 的相关文章

随机推荐