2017-09-23 50 views

回答

0

是的!您可以在Xamarin.Forms中使用Bindable屬性自定義控件,然後將其打包到nuget包中。

例如:你可以創建一個名爲MyCustomControl這樣

<?xml version="1.0" encoding="UTF-8"?> 
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      x:Class="BindablePropertyDemo.Custom.MyCustomControl"> 
    <Grid x:Name="grid" 
      Padding="10,40,10,10" 
      HeightRequest="160" 
      VerticalOptions="Start"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="100"/> 
      <RowDefinition Height="Auto"/> 
     </Grid.RowDefinitions> 
     <Image x:Name="image" 
       HeightRequest="100" 
       HorizontalOptions="Center"/> 

     <Label x:Name="title" 
       Text="BASKETBALL" 
       Grid.Row="1" 
       FontSize="20" 
       VerticalOptions="Center" 
       TextColor="White" 
       HorizontalOptions="Center" 
       FontAttributes="Bold"/> 
    </Grid> 
</ContentView> 

另一個文件夾ContentView然後你就可以從任何網頁這種控制是這樣的:

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      xmlns:local="clr-namespace:BindablePropertyDemo" 
      xmlns:custom="clr-namespace:BindablePropertyDemo.Custom" 
      x:Class="BindablePropertyDemo.MainPage" 
      BackgroundColor="#33334c"> 
    <ScrollView> 
     <Grid RowSpacing="0"> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="60"/> 
       <RowDefinition Height="Auto"/> 
      </Grid.RowDefinitions> 
      <Grid BackgroundColor="White"> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="60"/> 
        <ColumnDefinition Width="*"/> 
       </Grid.ColumnDefinitions> 
       <Grid BackgroundColor="#ff4284" Padding="10"> 
        <Image Source="hamburger.png"/> 
       </Grid> 
       <Label Grid.Column="1" 
        Text="Home" 
        TextColor="#ff4284" 
        FontSize="20" 
        Margin="5,0,0,0" 
        HorizontalOptions="Start" 
        VerticalOptions="Center"/> 
      </Grid> 
      <StackLayout Spacing="0" Grid.Row="1">< 
       <!-- SEE HERE!! --> 
       <custom:MyCustomControl BackgroundColor="#76dab2" 
            TitleText="BASKETBALL" 
            Image="basketball.png"/> 

       <custom:MyCustomControl BackgroundColor="#7c57e4" 
            TitleText="FOOTBALL" 
            Image="football.png"/> 

       <custom:MyCustomControl BackgroundColor="#f1b136" 
            TitleText="GRIDIRON" 
            Image="gridiron.png"/> 
      </StackLayout> 
     </Grid> 
    </ScrollView> 
</ContentPage> 

你將會產生如下結果:

enter image description here

我已經拿這個tutorial這個例子,你可以繼續有更多的信息。

你可以找到自己的源代碼在這裏:https://github.com/mindofai/BindablePropertyDemo

相關問題