reusability user controls of Xaml in xamarin.forms

后端 未结 1 964
闹比i
闹比i 2020-12-17 07:34

I am trying a new approach i.e. XAML to make application in xamarin.forms. At this time i am facing an issue to reuse my stack layout which is having a image and label. How

相关标签:
1条回答
  • 2020-12-17 07:42

    You can actually define your custom component in a separate XAML file and then just link the component wherever you need it.

    For example a label with image can be grouped together in a dedicated XAML file:

    <?xml version="1.0" encoding="utf-8" ?>
    <StackLayout xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="UserControls.ImageWithTitle"
                 VerticalOptions="Center" HorizontalOptions="Center" >
        <Label HorizontalOptions="Center"
               x:Name="TitleLabel" />
        <Image Source="noimage.png" />
    </StackLayout>
    

    On the .cs file I've defined a binding for the TitleLabel

    public string TitleName
    {
         get { return TitleLabel.Text; }
         set { TitleLabel.Text = value; }
    }
    

    So when you include the component on another layout, you can assign the label value directly (or via a binding):

     <usercontrols:ImageWithTitle TitleName="Home"/>
    
    0 讨论(0)
提交回复
热议问题