C# Xamarin.Forms中的外部控件模板,页面上没有模板布局属性

C# Xamarin.Forms中的外部控件模板,页面上没有模板布局属性,c#,xamarin,mobile,xamarin.forms,xamarin.android,C#,Xamarin,Mobile,Xamarin.forms,Xamarin.android,我是Xamarin的新手。我需要避免在应用程序中重复控件和布局,所以我尝试使用它来实现这一点。但是,根据前面链接(源代码)中的示例,我始终需要在我要使用控件的每个页面上包含一个带有标记的ControlTemplate属性 这是必要的还是我可以在内容视图中单独创建控件,并在我希望添加到的页面主体中引用它?我已经这样做了,因为当从不同页面调用控件时,我需要添加参数。这是正确的方法吗?这将主要用于android和ios应用程序,如果这有什么不同的话 在每个需要的页面上添加控件模板布局似乎与我尝试使用的

我是Xamarin的新手。我需要避免在应用程序中重复控件和布局,所以我尝试使用它来实现这一点。但是,根据前面链接(源代码)中的示例,我始终需要在我要使用控件的每个页面上包含一个带有标记的
ControlTemplate
属性

这是必要的还是我可以在内容视图中单独创建控件,并在我希望添加到的页面主体中引用它?我已经这样做了,因为当从不同页面调用控件时,我需要添加参数。这是正确的方法吗?这将主要用于android和ios应用程序,如果这有什么不同的话

在每个需要的页面上添加控件模板布局似乎与我尝试使用的“一次写入,随处可见”的基本原理背道而驰

我总是需要在我想要使用控件的每个页面上包含一个带有标记的ControlTemplate属性

不,您可以像下面的代码一样,将您的
ControlTemplate
放入
App.xaml的
Application.Resources
选项卡中

<Application xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="ControlTemplateDemos.App">
    <Application.Resources>
        <ControlTemplate x:Key="CardViewControlTemplate">
            <!--
            In this example, the frame's BindingContext is set to the control instance that the template is applied to. Therefore,
            the binding expressions resolve against the properties of each CardView object.
            -->
            <Frame BindingContext="{Binding Source={RelativeSource TemplatedParent}}"
                   BackgroundColor="{Binding CardColor}"
                   BorderColor="{Binding BorderColor}"
                   CornerRadius="5"
                   HasShadow="True"
                   Padding="8"
                   HorizontalOptions="Center"
                   VerticalOptions="Center">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="75" />
                        <RowDefinition Height="4" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="75" />
                        <ColumnDefinition Width="200" />
                    </Grid.ColumnDefinitions>
                    <Frame IsClippedToBounds="True"
                           BorderColor="{Binding BorderColor}"
                           BackgroundColor="{Binding IconBackgroundColor}"
                           CornerRadius="38"
                           HeightRequest="60"
                           WidthRequest="60"
                           HorizontalOptions="Center"
                           VerticalOptions="Center">
                        <Image Source="{Binding IconImageSource}"
                               Margin="-20"
                               WidthRequest="100"
                               HeightRequest="100"
                               Aspect="AspectFill" />
                    </Frame>
                    <Label Grid.Column="1"
                           Text="{Binding CardTitle}"
                           FontAttributes="Bold"
                           FontSize="Large"
                           VerticalTextAlignment="Center"
                           HorizontalTextAlignment="Start" />
                    <BoxView Grid.Row="1"
                             Grid.ColumnSpan="2"
                             BackgroundColor="{Binding BorderColor}"
                             HeightRequest="2"
                             HorizontalOptions="Fill" />
                    <Label Grid.Row="2"
                           Grid.ColumnSpan="2"
                           Text="{Binding CardDescription}"
                           VerticalTextAlignment="Start"
                           VerticalOptions="Fill"
                           HorizontalOptions="Fill" />
                </Grid>
            </Frame>
        </ControlTemplate>
    </Application.Resources>
</Application>
这里是运行GIF

我总是需要在我想要使用控件的每个页面上包含一个带有标记的ControlTemplate属性

不,您可以像下面的代码一样,将您的
ControlTemplate
放入
App.xaml的
Application.Resources
选项卡中

<Application xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="ControlTemplateDemos.App">
    <Application.Resources>
        <ControlTemplate x:Key="CardViewControlTemplate">
            <!--
            In this example, the frame's BindingContext is set to the control instance that the template is applied to. Therefore,
            the binding expressions resolve against the properties of each CardView object.
            -->
            <Frame BindingContext="{Binding Source={RelativeSource TemplatedParent}}"
                   BackgroundColor="{Binding CardColor}"
                   BorderColor="{Binding BorderColor}"
                   CornerRadius="5"
                   HasShadow="True"
                   Padding="8"
                   HorizontalOptions="Center"
                   VerticalOptions="Center">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="75" />
                        <RowDefinition Height="4" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="75" />
                        <ColumnDefinition Width="200" />
                    </Grid.ColumnDefinitions>
                    <Frame IsClippedToBounds="True"
                           BorderColor="{Binding BorderColor}"
                           BackgroundColor="{Binding IconBackgroundColor}"
                           CornerRadius="38"
                           HeightRequest="60"
                           WidthRequest="60"
                           HorizontalOptions="Center"
                           VerticalOptions="Center">
                        <Image Source="{Binding IconImageSource}"
                               Margin="-20"
                               WidthRequest="100"
                               HeightRequest="100"
                               Aspect="AspectFill" />
                    </Frame>
                    <Label Grid.Column="1"
                           Text="{Binding CardTitle}"
                           FontAttributes="Bold"
                           FontSize="Large"
                           VerticalTextAlignment="Center"
                           HorizontalTextAlignment="Start" />
                    <BoxView Grid.Row="1"
                             Grid.ColumnSpan="2"
                             BackgroundColor="{Binding BorderColor}"
                             HeightRequest="2"
                             HorizontalOptions="Fill" />
                    <Label Grid.Row="2"
                           Grid.ColumnSpan="2"
                           Text="{Binding CardDescription}"
                           VerticalTextAlignment="Start"
                           VerticalOptions="Fill"
                           HorizontalOptions="Fill" />
                </Grid>
            </Frame>
        </ControlTemplate>
    </Application.Resources>
</Application>
这里是运行GIF

谢谢Leon Lu

我不想在App.xaml中添加模板,因为添加的模板越多,这会变得很麻烦。我最终用我想要的控件布局创建了一个单独的内容视图,并在每个需要它的页面中直接使用它,如下所示:

 <ContentPage.Resources>
            <ControlTemplate x:Key="StellaControlTemplate">
                <controls:MyTemplate></controls:MyTemplate>
            </ControlTemplate>
 </ContentPage.Resources>

     <StackLayout Spacing="10" x:Name="layout">            
               <Label Text="My template below"></Label>
               <controls:MyTemplate PicForTemplate="harold.png" 
                 ControlTemplate="{StaticResource StellaControlTemplate}"> 
               </controls:MyTemplate>
     </StackLayout>

谢谢Leon Lu

我不想在App.xaml中添加模板,因为添加的模板越多,这会变得很麻烦。我最终用我想要的控件布局创建了一个单独的内容视图,并在每个需要它的页面中直接使用它,如下所示:

 <ContentPage.Resources>
            <ControlTemplate x:Key="StellaControlTemplate">
                <controls:MyTemplate></controls:MyTemplate>
            </ControlTemplate>
 </ContentPage.Resources>

     <StackLayout Spacing="10" x:Name="layout">            
               <Label Text="My template below"></Label>
               <controls:MyTemplate PicForTemplate="harold.png" 
                 ControlTemplate="{StaticResource StellaControlTemplate}"> 
               </controls:MyTemplate>
     </StackLayout>


你可以在视图中使用ControlTemplate,比如说基本视图,在任何地方都可以使用它,如果它是一个页面,你也可以对它进行子分类。你可以在视图中使用ControlTemplate,比如说基本视图,在任何地方都可以使用它,如果它是一个页面,你也可以对它进行子分类。