如何在Xamarin.Forms中使用CSS来样式化按钮?

huangapple go评论77阅读模式
英文:

How to style a Button in Xamarin.Forms using CSS?

问题

我已经从XAML文件中添加了一个按钮,如下所示:

<ContentView.Content>
   <Button Image="{Binding Icon}" Text="{Binding Title}" Clicked="ClickEvent" HeightRequest="10"  WidthRequest="10" />
</ContentView.Content>

如何使按钮样式看起来像这样?

如何在Xamarin.Forms中使用CSS来样式化按钮?

我在我的Xamarin项目中有一个CSS文件,但似乎无法成功地用它来更改按钮图像,因为这样的代码没有结果:

.button img {
    height: 50px;
    width: 50px;
}

我的当前按钮结果(太大的图标):

如何在Xamarin.Forms中使用CSS来样式化按钮?

英文:

I have added a button from xaml file like that:

<ContentView.Content>
   <Button Image="{Binding Icon}" Text="{Binding Title}" Clicked="ClickEvent" HeightRequest="10"  WidthRequest="10" />
</ContentView.Content>

How can I style the button to look like this?

如何在Xamarin.Forms中使用CSS来样式化按钮?

I have css file in my xamarin project but I dont seem to reach Button image with it sucessfully because such code gives no result:

.button img {
    height: 50px;
    width: 50px;
}

My current result of a button (terrible), picture is way to big:
如何在Xamarin.Forms中使用CSS来样式化按钮?

答案1

得分: 2

为了能够完全自定义一个按钮,最好的方法可能是实现自己的按钮控件,而不是使用CSS:

XAML

<?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="YourApp.Views.SomeRoundImageButton">

  <Frame
    CornerRadius="8">
    <Grid
      RowDefinitions="2*,*">
      <Image
        Grid.Row="0"
        Source="{Binding Icon}"/>
      <Label
        Grid.Row="1"
        TextColor="Blue"
        Text="{Binding Title}"/>      
    </Grid>
    <Frame.GestureRecognizers>
      <TapGestureRecognizer
        Tapped="OnButtonTapped"/>
    </Frame.GestureRecognizers>
  </Frame>

</ContentView>

Code-behind

using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace YourApp.Views {
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class SomeRoundImageButton : ContentView {

        public string Title {
            get => (string)GetValue(TitleProperty);
            set => SetValue(TitleProperty, value);
        }

        public ImageSource Icon {
            get => (ImageSource)GetValue(IconProperty);
            set => SetValue(IconProperty, value);
        }

        public event EventHandler<EventArgs> ButtonTapped;

        public static readonly BindableProperty TitleProperty = BindableProperty.Create(nameof(Title), typeof(string), typeof(SomeRoundImageButton));
        public static readonly BindableProperty IconProperty = BindableProperty.Create(nameof(Icon), typeof(ImageSource), typeof(SomeRoundImageButton));

        public SomeRoundImageButton() {
            InitializeComponent();
            BindingContext = this;
        }

        private void OnButtonTapped(object sender, EventArgs e) {
            ButtonTapped?.Invoke(this, e);
        }
    }
}

Usage

<ContentView.Content>
    <StackLayout>
        <views:SomeRoundImageButton
          Icon="{Binding Icon}"
          Title="{Binding Title}"
          WidthRequest="80"
          HeightRequest="80"
          ButtonTapped="Clicked"/>
    </StackLayout>
</ContentView.Content>

这样可以为你的设计提供更多的自由度,你可以调整图像的大小,向标签添加更多文本行,定义CornerRadius的大小,设置各种其他属性...

显然,你需要根据需要进行调整以实现预期的设计,但我希望这对你有所帮助。请注意,这只是一种实现方式。在Xamarin.Forms(和.NET MAUI)中,有很多方法可以实现相同的功能。

英文:

To be able to fully customize a button like that, it's probably best to implement your own Button control instead of using CSS:

XAML

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;ContentView xmlns=&quot;http://xamarin.com/schemas/2014/forms&quot; 
             xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
             x:Class=&quot;YourApp.Views.SomeRoundImageButton&quot;&gt;

  &lt;Frame
    CornerRadius=&quot;8&quot;&gt;
    &lt;Grid
      RowDefinitions=&quot;2*,*&quot;&gt;
      &lt;Image
        Grid.Row=&quot;0&quot;
        Source=&quot;{Binding Icon}&quot;/&gt;
      &lt;Label
        Grid.Row=&quot;1&quot;
        TextColor=&quot;Blue&quot;
        Text=&quot;{Binding Title}&quot;/&gt;      
    &lt;/Grid&gt;
    &lt;Frame.GestureRecognizers&gt;
      &lt;TapGestureRecognizer
        Tapped=&quot;OnButtonTapped&quot;/&gt;
    &lt;/Frame.GestureRecognizers&gt;
  &lt;/Frame&gt;

&lt;/ContentView&gt;

Code-behind

using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace YourApp.Views {
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class SomeRoundImageButton : ContentView {

        public string Title {
            get =&gt; (string)GetValue(TitleProperty);
            set =&gt; SetValue(TitleProperty, value);
        }

        public ImageSource Icon {
            get =&gt; (ImageSource)GetValue(IconProperty);
            set =&gt; SetValue(IconProperty, value);
        }

        public event EventHandler&lt;EventArgs&gt; ButtonTapped;

        public static readonly BindableProperty TitleProperty = BindableProperty.Create(nameof(Title), typeof(string), typeof(SomeRoundImageButton));
        public static readonly BindableProperty IconProperty = BindableProperty.Create(nameof(Icon), typeof(ImageSource), typeof(SomeRoundImageButton));

        public SomeRoundImageButton() {
            InitializeComponent();
            BindingContext = this;
        }

        private void OnButtonTapped(object sender, EventArgs e) {
            ButtonTapped?.Invoke(this, e);
        }
    }
}

Usage

&lt;ContentView.Content&gt;
    &lt;StackLayout&gt;
        &lt;views:SomeRoundImageButton
          Icon=&quot;{Binding Icon}&quot;
          Title=&quot;{Binding Title}&quot;
          WidthRequest=&quot;80&quot;
          HeightRequest=&quot;80&quot;
          ButtonTapped=&quot;Clicked&quot;/&gt;
    &lt;/StackLayout&gt;
&lt;/ContentView.Content&gt;

This gives you a lot more freedom for your design, you can make the Image bigger or smaller, add more lines of text to the Label, define the size of the CornerRadius, set all kinds of other properties...

Obviously, you need to play around with it to figure out the intended design, but I hope this will be of help. Note, this is just one way of doing this. In Xamarin.Forms (and .NET MAUI), there are many ways of achieving the same things.

huangapple
  • 本文由 发表于 2023年3月9日 19:34:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75684041.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定