Xamarin.Forms:点击后错键文本对齐方式(安卓) [英] Xamarin.Forms: wrong button text alignment after click (Android)
问题描述
我对Android的一个问题,Xamarin.Forms(版本1.2.2)(Nexus的5)。
的排列 Button.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.
在很短的项目,我想通了,是更新UI导致问题。
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,
}
}
};
}
}
在buttonBad一个点击(更新 label.Text
)导致此按钮上的文字对齐到不能再居中。在buttonGood咔嗒不会造成问题。
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?
这个解决办法似乎是太复杂了: <一href="http://forums.xamarin.com/discussion/20608/fix-for-button-layout-bug-on-android">http://forums.xamarin.com/discussion/20608/fix-for-button-layout-bug-on-android
This workaround seems to be too complicated: http://forums.xamarin.com/discussion/20608/fix-for-button-layout-bug-on-android
编辑:
一个编程编辑UI也病例的bug。更改 label.Text
在异步方法经过短暂的等待领导的buttonGood来调整其文字后点击错误的。
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: 我创建了一个例子/测试项目在GitHub上: <一href="https://github.com/perpetual-mobile/ButtonTextAlignmentBug.git">https://github.com/perpetual-mobile/ButtonTextAlignmentBug.git 对齐是正确的,当StackLayout被替换为AbsolutLayout,但我需要的StackLayout很好地工作。
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.
推荐答案
好了,处理这个愚蠢的错误了几个小时之后,我通过实现自定义渲染和覆盖解决它 ChildDrawableStateChanged
:
Ok, after hours of dealing with this silly bug, I resolved it by implementing a custom rendering and overriding ChildDrawableStateChanged
:
public override void ChildDrawableStateChanged(Android.Views.View child)
{
base.ChildDrawableStateChanged(child);
Control.Text = Control.Text;
}
这篇关于Xamarin.Forms:点击后错键文本对齐方式(安卓)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!