【问题标题】:Xamarin Forms button with loading indicator带有加载指示器的 Xamarin Forms 按钮
【发布时间】:2021-04-20 01:47:50
【问题描述】:

我从大约一周开始就开始使用 Xamarin,并且对在按钮内制作加载图标的可能性有疑问。

我想实现与此类似的目标: https://jsfiddle.net/mr8fwtcv/

我试图用 FontAwesome 来实现这个效果,但动画它有问题,所以在解决方案中使用 Activity Indicator 也不错。

我的按钮放在 Stack Layout 里面,代码如下:

var loginButton = new Button
{
    BackgroundColor = Color.FromHex("689F38"),
    TextColor = Color.White,
    HeightRequest = 46,
    Text = "Log in",
    FontAttributes = FontAttributes.Bold,
    FontSize = 18,
    Margin = margin
};

我已经在寻找一些插件/解决方案,但找不到。 提前致谢。

【问题讨论】:

  • 您是否尝试在按钮顶部添加ActivityIndicator 并将ButtonText 属性设置为空字符串,一旦IsBusy(或您绑定的任何内容)是true?
  • 我会按照 Demitrian 的建议去做,或者创建一个 ButtonRemderer 并将其添加到本机。在 OpropretyChanging 覆盖中,您可以处理功能。

标签: xamarin xamarin.forms


【解决方案1】:

您必须使用布局才能在其中包含多个控件。如果您不是在寻找CustomRenderers,那么您可以做的是拥有一个RelativeLayout,并在其中包含Button 和Activity Indicator。您还必须为按钮文本编写一个转换器,当 IsBusy 为真时,它将文本设置为 string.Empty。

例如:

<StackLayout>
    <ActivityIndicator IsVisible="{Binding IsBusy}" IsRunning="{Binding IsBusy}" />
    <Button Text="{Binding ButtonText, Converter={StaticResource ButtonTextConverter}, ConverterParameter={Binding IsBusy}}" />
</StackLayout>

此外,如果您打算在多个地方使用它,您可以将其创建为自定义控件。

您可以查看自定义控制代码here

完整项目可在here获取。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-04
    • 2019-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多