Xamarin Forms - Android - 无法获得imageview的宽度或高度

时间:2017-09-21 12:01:03

标签: c# xaml xamarin xamarin.forms xamarin.android

我正试图在android平台上获取我的图像视图的宽度。宽度为“-1”。我在OnAppearing()方法中尝试了这个。它在iOS平台上运行良好。

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>
...

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

您可以订阅comboBox1.Items.Clear(); connection.Open(); MySqlCommand command = new MySqlCommand(service.selectOnlyWorkerName(), connection); MySqlDataAdapter da = new MySqlDataAdapter(command); using (DataTable dt = new DataTable()) { da.Fill(dt); foreach (DataRow dr in dt.Rows) { comboBox1.Items.Add(dr["worker_name"]); } } connection.Close(); 事件(请参阅here

SizeChanged

或覆盖private void Image_OnSizeChanged(object sender, EventArgs e) { // image size should be set here } (请参阅here

OnSizeAllocated

大小最终应该是正确的。使用第二种方法请注意,第一次通话时,您将无法获得实际尺寸。您可能必须检查宽度/高度是否等于-1。