Xamarin.Forms:单击后按钮文本对齐错误(Android) [英] Xamarin.Forms: wrong button text alignment after click (Android)
问题描述
我在 Android (Nexus 5) 上使用 Xamarin.Forms(版本 1.2.2)有问题.Button.Text
的对齐方式在执行点击后通常不会居中.
在一个简短的项目中,我发现更新 UI 会导致问题.
公共类应用{公共静态页面 GetMainPage(){var 标签 = 新标签 {文字 = "标签",};var buttonBad = 新按钮 {Text = "buttonBad",Command = new Command(() => label.Text += "1"),};var buttonGood = 新按钮 {Text = "buttonGood",};返回新的 ContentPage {内容 = 新的 StackLayout {儿童 = {按钮坏,按钮好,标签,}}};}}
单击buttonBad"(更新label.Text
)会导致此按钮的文本对齐不再居中.单击buttonGood"不会导致问题.
有没有好的解决方法来解决这个问题?
这个解决方法似乎太复杂了:当 StackLayout 被 AbsolutLayout 替换时,对齐是正确的,但我需要 StackLayout 才能正常工作.
好的,在处理这个愚蠢的错误数小时后,我通过实现自定义渲染器并覆盖 ChildDrawableStateChanged
来解决它:
public override void ChildDrawableStateChanged(Android.Views.View child){base.ChildDrawableStateChanged(child);Control.Text = Control.Text;}
I have a problem with Xamarin.Forms (version 1.2.2) on Android (Nexus 5).
The alignment of Button.Text
is often not centered after performing a click.
In a short project, I figured out, that updating the UI causes the problem.
public class App
{
public static Page GetMainPage()
{
var label = new Label {
Text = "label",
};
var buttonBad = new Button {
Text = "buttonBad",
Command = new Command(() => label.Text += "1"),
};
var buttonGood = new Button {
Text = "buttonGood",
};
return new ContentPage {
Content = new StackLayout {
Children = {
buttonBad,
buttonGood,
label,
}
}
};
}
}
A click on "buttonBad" (updating the label.Text
) causes the text-alignment of this button to not be centered anymore. A click on "buttonGood" does not cause the problem.
Is there a good workaround to solve this problem?
This workaround seems to be too complicated: http://forums.xamarin.com/discussion/20608/fix-for-button-layout-bug-on-android
edit:
A programatically edit of the UI also cases the bug. Changing the label.Text
in an async method after a short waiting leads the "buttonGood" to align its text wrong after a click.
edit2: I created an example / test project on GitHub: https://github.com/perpetual-mobile/ButtonTextAlignmentBug.git The alignment is correct, when the StackLayout is replaced by an AbsolutLayout, but i need the StackLayout to work well.
Ok, after hours of dealing with this silly bug, I resolved it by implementing a custom renderer and overriding ChildDrawableStateChanged
:
public override void ChildDrawableStateChanged(Android.Views.View child)
{
base.ChildDrawableStateChanged(child);
Control.Text = Control.Text;
}
这篇关于Xamarin.Forms:单击后按钮文本对齐错误(Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!