Xamarin表单按钮,带有加载指示器 [英] Xamarin Forms button with loading indicator
问题描述
大约一周以来,我进入Xamarin,并对在按钮内加载图标的可能性有疑问.
I'm into Xamarin since about week and have a question about the possibility of making loading icon inside a button.
我想实现一些类似的东西: https://jsfiddle.net/mr8fwtcv/
I would like to achieve something similiar to this: https://jsfiddle.net/mr8fwtcv/
我试图用FontAwesome达到这种效果,但是在动画化方面遇到了问题,因此在解决方案中使用活动指示器也不错.
I was trying to achieve this effect with FontAwesome but had a problem animating it, so it would be not bad to use Activity Indicator in the solution.
我的按钮位于堆栈布局中,这是代码:
My button is placed inside Stack Layout, and here is the code:
var loginButton = new Button
{
BackgroundColor = Color.FromHex("689F38"),
TextColor = Color.White,
HeightRequest = 46,
Text = "Log in",
FontAttributes = FontAttributes.Bold,
FontSize = 18,
Margin = margin
};
我已经在寻找一些插件/解决方案,但找不到任何插件/解决方案. 预先感谢.
I was already looking for some plugins / solutions but couldn't find any. Thanks in advance.
推荐答案
您将必须使用Layout才能在其中包含多个控件.如果您不寻找CustomRenderers,那么您可以做的是创建一个RelativeLayout,并在其中包含Button和Activity Indicator.您还必须为按钮文本编写一个Converter,以将IsBusy为true时将文本设置为string.Empty.
You will have to use a Layout to have more than one control in it. If you are not looking for CustomRenderers then what you could do is have a RelativeLayout and inside that both the Button and the Activity Indicator. You will also have to write a Converter for the button text that will set the text to string.Empty when the IsBusy is true.
例如:
<StackLayout>
<ActivityIndicator IsVisible="{Binding IsBusy}" IsRunning="{Binding IsBusy}" />
<Button Text="{Binding ButtonText, Converter={StaticResource ButtonTextConverter}, ConverterParameter={Binding IsBusy} />
</StackLayout>
此外,如果您打算在多个地方使用它,可以将其创建为自定义控件.
Also if you are planning to use this in multiple places you can create this as a custom control.
您可以检出自定义控件代码这里.
You can check out the custom control code here.
完整项目可在此处获得.
这篇关于Xamarin表单按钮,带有加载指示器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!