我目前正在 Xamarin 表单中开发一个 PCL 项目,并开始研究 iOS 实现,因为我完成了 Android 项目。我遇到的问题是我的 View 的背景图像被放大(工具栏上的图标也是如此)。它可能使用全尺寸图像,所以我希望使用类似 Aspect=Aspect.AspectFit
的东西。但由于我使用的是 PCL 代码,所以我使用了 Background= Height>Width ? <picture_string_portrait> : <picture_string_landscape>
,它需要一个字符串。我似乎不知道如何解决这个问题+我只能共享默认代码,因为这是我目前工作的公司(所以恐怕也没有屏幕)。
这是我的 pcl 代码 (myview.xaml.cs) 的一部分,它在加载和方向改变时命中。在 Android 中,它可以很好地缩放,但在 iOS 中则不然。
private void OnSizeChanged(object sender, EventArgs eventArgs)
{
BackgroundImage = Height > Width ? "background_portrait.jpg" : "background_landscape.jpg";
}
如果这是一个菜鸟问题,我深表歉意,但我已经尽我所能地使用谷歌搜索,但似乎找不到任何有用的东西。一个适用于所有人的 PCL 解决方案将不胜感激。
提前致谢!
最佳答案
ContentPage.BackgroundImage
的问题是您无法控制它的纵横比。考虑到设备如何具有各种尺寸和形状,替代解决方案可能更适合您。您可以通过在 ContentPage
中使用 Image
并使用它的 Aspect
来获得相同的结果。一种方法是使用像这样的 AbsoluteLayout
:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Sample">
<AbsoluteLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Image AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" Source="background.png" Aspect="AspectFill"/>
<StackLayout AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0, 0, 1, 1">
// Content
</StackLayout>
</AbsoluteLayout>
</ContentPage>
另一个是使用 RelativeLayout
就像这个示例中那样:
http://dotnetbyexample.blogspot.nl/2015/02/using-full-size-none-stretched.html
关于c# - 背景图片 PCL xamarin forms iOS 放大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45014638/