2017-09-21 12 views
1

アンドロイドプラットフォームでイメージビューの幅を取得しようとしています。幅は "-1"です。私はOnAppearing()メソッドでこれを試しました。 iOSプラットフォームで正常に動作しています。助けのためのXamarinフォーム - Android - イメージビューの幅または高さを取得できません

C#の

protected override void OnAppearing() 
{ 
    base.OnAppearing(); 
    myWidth = imagePusher.Width; //Width = -1 at android 
} 

XAML

... 
<ScrollView x:Name="scrollPusher" Orientation="Horizontal" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=1}"> 
    <RelativeLayout x:Name="rlPusher"> 
     <StackLayout x:Name="stackPusher" Orientation="Horizontal" Spacing="0"> 
      <Image x:Name="imagePusherSpacer" Source="pusherSpacer.jpg" Aspect="AspectFit"> 
      </Image> 
      <Image x:Name="imagePusher" Source="pusher.jpg" Aspect="AspectFit"> 
      </Image> 
     </StackLayout> 
    </RelativeLayout> 
</ScrollView> 
... 

感謝。あなたはSizeChangedイベント(hereを参照)

private void Image_OnSizeChanged(object sender, EventArgs e) 
{ 
    // image size should be set here 
} 

OnSizeAllocatedを上書き(hereを参照)

protected override void OnSizeAllocated(double width, double height) 
{ 
    base.OnSizeAllocated(width, height); //must be called 
    if (this.width != width || this.height != height) 
    { 
     this.width = width; 
     this.height = height; 

     // do what ever you like here 
    } 
} 

サイズを購読することができますいずれか

答えて

2

は最終的にそれらのいずれかで正しいはずです。 2番目の方法で注意してください、最初の呼び出しで実際のサイズを取得しません。幅/高さが-1であることを確認する必要があるかもしれません。

+0

ありがとうございますが、OnSizeAllocatedでは、送信者が指定されていないため、私が探しているビューはいつどのようにわかりますか? – anguish

+0

または、istの読み込みが完了したかどうかをチェックするためだけにOnSizeAllocatedですか? – anguish

+0

'OnSizeAllocated'を' View'や 'Page'でオーバーライドします。イメージのサイズが変更されているかどうかを手動で確認する必要があります。送信者はありません。 –

関連する問題