简体   繁体   English

如何在Xamarin Forms中保持图像的正常高度的同时使用它的全宽度?

[英]How to make an image use it's full width while keeping it's normal height in Xamarin Forms?

Having big problems to fit an image to the screen width while keeping it's normal height. 在使图像适合屏幕宽度同时保持其正常高度方面存在很大的问题。 I gather different images from my database (depending on what you clicked previously) and therefor the image height/width are different all the time. 我从数据库中收集了不同的图像(取决于您之前单击的内容),因此图像的高度/宽度一直都不同。 How can I make so the image always fills the screen width while keeping it's normal height? 如何使图像始终保持屏幕正常宽度,同时又保持其正常高度?

Right now some images fill the width while keeping it's normal height while some images does not. 现在,有些图像会填充宽度,同时保持其正常高度,而有些图像则不会。

What I have tried: 我尝试过的

AspectFill crops out parts of the image so this solution does not work. AspectFill出图像的一部分,因此该解决方案不起作用。

Fill stretches the image which also is not a good solution. Fill拉伸图像,这也不是一个好的解决方案。

theimage.WidthRequest = App.ScreenWidth; I also tried to do this to fix the issue and I get the ScreenWidth from the AppDelegete but theimagedoes not change it's width at all. 我也尝试这样做以解决此问题,并且从AppDelegete获取了ScreenWidth,但是image根本不改变其宽度。

I also then tried to add a grid.column like this: 然后,我还尝试添加如下所示的grid.column:

<Grid.RowDefinitions>

<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>

</Grid.RowDefinitions>

<Grid.ColumnDefinitions>

<ColumnDefinition x:Name = "width"/>

</Grid.ColumnDefinitions>

<Image x:Name="theimage" Grid.Row="1" Grid.Column = "0"  HorizontalOptions = "StartAndExpand" VerticalOptions = "StartAndExpand" />

And connecting it in the code like this: 并将其连接在这样的代码中:

width.Width = App.ScreenWidth;

But some of the images remains the same and does not fill the screen width. 但是某些图像保持不变,并且无法填满屏幕宽度。

Do I have to do a custom renderer in order to sucesssfully make an image fill the screens width while keeping it's normal height? 我是否必须做一个自定义渲染器,以使图像成功填满屏幕宽度,同时又保持其正常高度?

This is the current code I have (that i have changed a million times with the code examples i have above): 这是我当前拥有的代码(我在上面的代码示例中已更改了一百万次):

<ScrollView HorizontalOptions = "FillAndExpand" VerticalOptions = "FillAndExpand" >
<StackLayout HorizontalOptions = "FillAndExpand" VerticalOptions = "FillAndExpand">
<Grid>

<Grid.RowDefinitions>

    <RowDefinition Height="Auto"/>
    <RowDefinition Height="100"/>

</Grid.RowDefinitions>

<StackLayout Grid.Row="0" BackgroundColor = "Red" HorizontalOptions = "FillAndExpand" VerticalOptions = "FillAndExpand">

    <Image Grid.Row="1"  x:Name="theimage" Aspect = "AspectFit"  HorizontalOptions = "FillAndExpand" VerticalOptions = "FillAndExpand" />

    <Button Clicked="clickFunc" TextColor="White" Grid.Row="1" HeightRequest="80" WidthRequest="120" BorderRadius="40" HorizontalOptions="End" VerticalOptions="Start" />

<StackLayout Grid.Row="1" Padding="10,20,10,0">

    <Label TextColor = "#474747" x:Name="title" Text="" HorizontalOptions="Start" FontFamily="Helvetica Neue" FontAttributes="Bold" />

</StackLayout>
</StackLayout>
</Grid> 
</StackLayout>
</ScrollView>

I am running out of ideas to solve this issue now. 我已经没有足够的想法来解决这个问题了。 Do I have to do a renderer in order to make it work? 我必须要做一个渲染器才能使其工作吗?

best option is for you to learn how to use MVVM.(steep but worth the experience) 最好的选择是让您学习如何使用MVVM。(陡峭但值得一试)

what you can do is 你能做的是

View 视图

<Image Source="{Binding ImageSource,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged }" Width="100" Height="{Binding SourceImageHeight,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"></Image>

Using this you can Set the values of Xaml properties inside a ViewModel(class) which allows your code to be VERY dynamic. 使用此方法,可以在ViewModel(class)内设置Xaml属性的值,该值可使代码非常动态。

View Code Behind (setting the datacontext(can also be set inside the view)) 后面的查看代码 (设置datacontext(也可以在视图内部设置))

InitializeComponent();
viewModel = new YourViewModel();
this.DataContext = viewModel;

ViewModel 视图模型

public class YourViewModel
{
  private String _imageSource = "";

  public String ImageSource
  {
   get{return _imageSource;}
   set{_imageSource = value;
       NotifyPropertyChanged(m => m.ImageSource);
      }
  }

  private Int _sourceImageHeight = 0;

  public Int SourceImageHeight 
  {
   get{return _sourceImageHeight ;}
   set{_sourceImageHeight = value;
       NotifyPropertyChanged(m => m.SourceImageHeight);
      }
  }
}

all you have to do from here is 您从这里要做的就是

ImageSource = ImageLocation

and

SourceImageHeight = 1200

Note: In the Viewmodel, if you dont use the private variables(_imageSource/_sourceImageHeight) and rather use the public Variable name(ImageSource/SourceImageHeight) in the setter, you will create an Infinite Loop. 注意:在Viewmodel中,如果不使用私有变量(_imageSource / _sourceImageHeight),而是在setter中使用公共变量名(ImageSource / SourceImageHeight),则会创建一个无限循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM