Xamarin.Forms - StackLayout标签超出设备宽度

Cad*_*dab 5 c# xamarin.forms

可以设置元素的百分比宽度吗?

我遇到的问题是第二个标签按下屏幕上的按钮,你怎么能强制标签只占用可用的空间.我已经尝试设置元素的最小宽度.

 new StackLayout
 {
     Orientation = StackOrientation.Horizontal,                                    
     Spacing = 0,
     Children = {       
         new Label() { Text = "TITLE", HorizontalOptions = LayoutOptions.Start},
         new Label() { Text = "fsdf dsfsd fsdfsdfs ewtrey vjdgyu jhy jgh tyjht rhyrt rgtu gtr ujtrey gt yu tgrt uh tyui y5r rtuyfgtj yrjhrytjtyjy jty t ruy ujh i rt", HorizontalOptions = LayoutOptions.Center, LineBreakMode = LineBreakMode.WordWrap},                                        
         new Button() { Text = "wee", HorizontalOptions = LayoutOptions.EndAndExpand}                                      
     }
 },       
Run Code Online (Sandbox Code Playgroud)

Fem*_*jin 8

尝试使用OnSizeAllocated(双倍宽度,双倍高度),

代码在您的Xamarin.Forms页面

protected override void OnSizeAllocated(double width, double height)
    {
        base.OnSizeAllocated(width, height);

        Metrics.Instance.Width=width;
        Metrics.Instance.Height=height;
    }
Run Code Online (Sandbox Code Playgroud)

Singleton类保存宽度和高度并检查方向更改

public class Metrics
    {

        private static Metrics _instance;

        protected SessionData ()
        {
        }

        public double Width{ get; set; } //Width

        public double Height{ get; set; } //Height
        }
Run Code Online (Sandbox Code Playgroud)

创建Stacklayout

   var StackchildSize = Metrics.Width/3; 
   new StackLayout
   {
   Orientation = StackOrientation.Horizontal,                                    
   Spacing = 0,
   Children = {       
     new Label() { Text = "TITLE", HorizontalOptions = LayoutOptions.Start
     WidthRequest=stackChildSize},
     new Label() { Text = "<Your Text>", WidthRequest=stackChildSize,},                                       
     new Button() { Text = "wee", HorizontalOptions = LayoutOptions.EndAndExpand,
     WidthRequest=stackChildSize,}                                      
 }
Run Code Online (Sandbox Code Playgroud)

},