2016-10-10 5 views
1

WidthHeightXamarin.Forms.Viewにする方法が見つかりません。Xamarinフォーム - AbsoluteLayoutのサイズを取得

私はこのオブジェクトを持っている:

public class SquareLayout : View 
{ 
    public static readonly BindableProperty ScalingBaseProperty = 
     BindableProperty.Create(nameof(ScalingBase), typeof(ScalingBaseType), typeof(SquareLayout), ScalingBaseType.Average, 
      propertyChanged: OnScalingBasePropertyChanged); 

    public ScalingBaseType ScalingBase 
    { 
     get { return (ScalingBaseType)GetValue(ScalingBaseProperty); } 
     set { SetValue(ScalingBaseProperty, value); } 
    } 

    public enum ScalingBaseType 
    { 
     Average, 
     Height, 
     Width 
    } 

    public static void OnScalingBasePropertyChanged(BindableObject bindable, object oldValue, object newValue) 
    { 
     SquareLayout sq = ((SquareLayout)bindable); 

     Debug.WriteLine("|Width = {0}|Height = {1}|", sq.Bounds.Width, sq.Bounds.Height); 

     switch (sq.ScalingBase) 
     { 
      case ScalingBaseType.Average: 
       double size = (sq.Bounds.Width + sq.Bounds.Height)/2; 
       sq.WidthRequest = size; 
       sq.HeightRequest = size; 
       break; 
      case ScalingBaseType.Height: 
       sq.WidthRequest = sq.Bounds.Height; 
       break; 
      case ScalingBaseType.Width: 
       sq.HeightRequest = sq.Bounds.Width; 
       break; 
     } 

     Debug.WriteLine("|Width = {0}|Height = {1}|", sq.Bounds.Width, sq.Bounds.Height); 
    } 
} 

基本的に、あなたはどこかにこのSquareLayoutを宣言し、Enumに基づいて、レイアウトが広場としてサイズが変更されます。だから、

、私は、このXAMLの一部私の周りの2 SquareRectangleを与えない..しかし、何も考え

<ContentPage.Content> 
    <AbsoluteLayout BackgroundColor="White"> 
    ... 
    <AbsoluteLayout x:Name="ToolsLayout" BackgroundColor="Red" 
        AbsoluteLayout.LayoutBounds="0.5, 0.05, 0.9, 0.075" 
        AbsoluteLayout.LayoutFlags="All"> 
     <control:SquareLayout BackgroundColor="White" ScalingBase="Height" 
          AbsoluteLayout.LayoutBounds="0, 0.5, 0.1, 1" 
          AbsoluteLayout.LayoutFlags="All"> 

     </control:SquareLayout> 
     <control:SquareLayout BackgroundColor="White" ScalingBase="Height" 
          AbsoluteLayout.LayoutBounds="1, 0.5, 0.1, 1" 
          AbsoluteLayout.LayoutFlags="All"> 

     </control:SquareLayout> 
    </AbsoluteLayout> 
    ... 
    </AbsoluteLayout> 
</ContentPage.Content> 

を持っています!

私は私が得る唯一の値は..ですWidthHeight何も..私はBoundsをしようとしただけでなく、あなたが例で見ることができますが、何も1つのより多くの時間を表示するように

を試してみました - 1

誰かが私を助けることができますか?少し早いですがお礼を !

+0

これを解決できましたか?あなた自身の質問に答えることができます – Zany

+0

本当に:/すみません – Emixam23

答えて

1

Xamarinは初心者のためのドキュメントが非常に少なく、基本的にデザイン時に幅と高さのプロパティのデフォルト値が-1であり、これは設計中に知る方法がないこのイベントがトリガーされる実行時にレイアウトの境界が設定されている場合は、SizeChangedイベントを使用して幅と高さのプロパティを取得するために 'イベント'を使用する必要があります。

xamlコードビハインドでは、このイベントを使用してyourlayoutのwidhとheightを取得しています。ラムダ関数を使用していますが、これは多くの方法で実装できます。

yourlayout.SizeChanged+=(sender,e)=> 
{ 
//Add your child objects here and set its values using yourlayout.Height etc 
}; 

この関数内で子を追加する代わりに、この関数を使用して値を返して代わりに使用することができます。

注:これがMVVMのベストプラクティスかどうかはわかりません

関連する問題