简体   繁体   English

Xamarin.Forms:点击后错误的按钮文本对齐(Android)

[英]Xamarin.Forms: wrong button text alignment after click (Android)

I have a problem with Xamarin.Forms (version 1.2.2) on Android (Nexus 5). 我在Android(Nexus 5)上遇到了Xamarin.Forms(版本1.2.2)的问题。 The alignment of Button.Text is often not centered after performing a click. 执行单击后, Button.Text的对齐通常不会居中。

In a short project, I figured out, that updating the UI causes the problem. 在一个简短的项目中,我发现,更新UI会导致问题。

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. 单击“buttonBad”(更新label.Text )会导致此按钮的文本对齐不再居中。 A click on "buttonGood" does not cause the problem. 点击“buttonGood”不会导致问题。

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 这种解决方法似乎太复杂了: http//forums.xamarin.com/discussion/20608/fix-for-button-layout-bug-on-android

edit: A programatically edit of the UI also cases the bug. 编辑:以编程方式编辑UI也会出现错误。 Changing the label.Text in an async method after a short waiting leads the "buttonGood" to align its text wrong after a click. 在短暂等待后更改异步方法中的label.Text会导致“buttonGood”在单击后将其文本对齐错误。

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. edit2:我在GitHub上创建了一个示例/测试项目: https//github.com/perpetual-mobile/ButtonTextAlignmentBug.git当StackLayout被AbsolutLayout替换时,对齐是正确的,但我需要StackLayout才能正常工作。

Ok, after hours of dealing with this silly bug, I resolved it by implementing a custom renderer and overriding ChildDrawableStateChanged : 好的,经过几个小时的处理这个愚蠢的bug,我通过实现自定义渲染器并重写ChildDrawableStateChanged解决它:

public override void ChildDrawableStateChanged(Android.Views.View child) 
{
    base.ChildDrawableStateChanged(child); 
    Control.Text = Control.Text; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM