Wha*_*Man 8 c# xamarin xamarin.forms
我有一个Xamarin表单应用程序,我使用相对布局来非常具体地定位一些元素.我希望堆栈布局位于相对布局的中间.我用这个代码:
_relativeLayout.Children.Add (
_stackLayout,
Constraint.RelativeToParent(p => (p.Width / 2) - (_stackLayout.Width / 2) ),
Constraint.RelativeToParent(p => (p.Height/ 2) - (_stackLayout.Height / 2) )
);
Run Code Online (Sandbox Code Playgroud)
当表单加载时,我得不到正确的结果.堆栈布局偏向一侧.但是,如果我以一种方式旋转屏幕然后再返回,它看起来很完美.因此,我认为在渲染布局时,堆栈布局的高度和宽度尚未完全计算,但在旋转时,这些值是已知的,因此它可以正确渲染.
如何使堆栈布局完美地以初始表单加载为中心?
SKa*_*all 10
您可以使用请求值.检查Width/Height是否为-1,在这种情况下它尚未初始化,您应该使用请求值.如果已初始化,则使用它.
this._relativeLayout.Children.Add(
this._stackLayout,
Constraint.RelativeToParent(p => (p.Width / 2) -
((this._stackLayout.Width == -1 ? this._stackLayout.WidthRequest : this._stackLayout.Width) / 2)),
Constraint.RelativeToParent(p => (p.Height / 2) -
((this._stackLayout.Height == -1 ? this._stackLayout.HeightRequest : this._stackLayout.Height) / 2))
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6049 次 |
| 最近记录: |