Home > OS >  static design and only change the text and the binding in Xamarin forms?
static design and only change the text and the binding in Xamarin forms?

Time:04-06

I'm new to xamarin and c# and i need some help.

this code in xaml and i want to make the design static and in function to change the text and the command to use the design for this component in another page and only change the text and command or binding

<Button BackgroundColor="#2196f3" 
                    WidthRequest="300"
                    CornerRadius="20"
                    FontSize="Medium"
                    TextColor="White"
                    Text="Login"
                    Command="{Binding LoginCommand}" ></Button>

how can i do it:)?

CodePudding user response:

You can use a resource dictionary and so you apply the same style to your button and only change the text and the command

<ContentPage.Resources>
    <ResourceDictionary>
        <Style x:Key="MyStyleButton" TargetType="Button">
            <Setter Property="BackgroundColor" Value="#2196f3" />
            <Setter Property="WidthRequest" Value="300" />
            <Setter Property="CornerRadius" Value="20" />
            <Setter Property="FontSize" Value="Medium" />
            <Setter Property="TextColor" Value="White" />
        </Style>
    </ResourceDictionary>
</ContentPage.Resources>


<ContentPage.Content>
    <StackLayout>
        <Button
            Command="{Binding Command1}"
            Style="{StaticResource MyStyleButton}"
            Text="Button1" />
        <Button
            Command="{Binding Command2}"
            Style="{StaticResource MyStyleButton}"
            Text="Button2" />
        <Button
            Command="{Binding Command3}"
            Style="{StaticResource MyStyleButton}"
            Text="Button3" />
    </StackLayout>
</ContentPage.Content>
  • Related