簡體   English   中英

自定義布局,Xamarin形式。 最佳方法

[英]Custom layout, Xamarin forms. Best approach

我是Xamarin.Forms和Xaml的新手。 我做了一個自定義控件,希望在整個應用程序中用作自定義背景。 將自定義控件作為contentview制作,如下所示。

 <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的指定內部stackview內放置標簽。

我唯一能想到的就是想出某種方式來訪問我的自定義contentBackground的InnerStackView,然后訪問該Stacklayout的children屬性,然后訪問該堆棧布局的Children.add(View)。 盡管如此,這意味着我將不得不通過代碼來完成此操作,並且我想在XAML中實現此行為,因為這對我來說比較熟悉。

這是唯一的方法還是在XAML中實現我的目標的替代方法?

嘗試使用ContentProperty。簡單示例:

 [ContentProperty("AddContent")]
 public class YourView: ContentView
 {
    protected ContentView ContentContainer;

    public View AddContent
    {
        get => ContentContainer.Content;
        set => ContentContainer.Content = value;
    } 
    ......
 }

 //in xaml
 <YourView>
   <Label Text="Hello world"/>
 </YourView>

如果有人仍然嘗試這樣做,則可以使用ControlTemplate在多個頁面或整個應用范圍內查看,

<Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="SimpleTheme.App">
    <Application.Resources>
        <ResourceDictionary>
            <ControlTemplate x:Key="TealTemplate">
                <Grid>
                    ...
                    <BoxView ... />
                    <Label Text="Control Template Demo App"
                           TextColor="White"
                           VerticalOptions="Center" ... />
                    <ContentPresenter ... />
                    <BoxView Color="Teal" ... />
                    <Label Text="(c) Xamarin 2016"
                           TextColor="White"
                           VerticalOptions="Center" ... />
                </Grid>
            </ControlTemplate>
            <ControlTemplate x:Key="AquaTemplate">
                ...
            </ControlTemplate>
        </ResourceDictionary>
    </Application.Resources>
</Application>

這里的魔力是ContentPresenter ,您可以在其中放置任何東西,看起來都很好。 然后要使用所需的模板,像這樣為您的ContentView.ControlTemplate設置,

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="SimpleTheme.HomePage">
    <ContentView x:Name="contentView" Padding="0,20,0,0"
                 ControlTemplate="{StaticResource TealTemplate}">
        <StackLayout VerticalOptions="CenterAndExpand">
            <Label Text="Welcome to the app!" HorizontalOptions="Center" />
            <Button Text="Change Theme" Clicked="OnButtonClicked" />
        </StackLayout>
    </ContentView>
</ContentPage>

此處簽出官方文檔。

如果您想創建一個可插入任何頁面的自定義控件/布局,則可以使用ContentPresenter創建自己的ContentView ,以容納您的子視圖,

<!-- Content -->
<ContentPresenter Content="{Binding SomeContent}"/>

其中SomeContent可以是View類型的BindableProperty

您還可以在此處查看自定義布局的示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM