C# 在Xamarin Forms项目中,如何为页面使用不同大小的BackgroundImage属性?
我在参考资料中添加了一些类型的图像C# 在Xamarin Forms项目中,如何为页面使用不同大小的BackgroundImage属性?,c#,xamarin,xamarin.ios,xamarin.forms,C#,Xamarin,Xamarin.ios,Xamarin.forms,我在参考资料中添加了一些类型的图像 - 320 x 480 login-background.png - 640 x 960 login-background@2x.png - 640 x 1136 login-background-568h@2x.png - 750 x 1334 login-background-667h@2x.png 然后我在xaml中填充了BackgroundImage属性,如“Image/login background” 但它仍然不起作用。设备和模拟器都
- 320 x 480 login-background.png
- 640 x 960 login-background@2x.png
- 640 x 1136 login-background-568h@2x.png
- 750 x 1334 login-background-667h@2x.png
然后我在xaml中填充了BackgroundImage属性,如“Image/login background”
但它仍然不起作用。设备和模拟器都呈现320 x 480。Xaml无法识别iOS的
-568h
或@2x
e.t.c。它选择与确切名称匹配的图像,不带扩展名。它在android中工作,因为所有图像都有相同的名称,并且分辨率文件夹不同
作为一种解决方法,您可以通过覆盖OnSizeAllocated方法查看高度/宽度,从C#代码后面设置图像
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
string BackGroundImgName = "myimage";
Device.OnPlatform(iOS: () =>
{
if (width >= 414)
// iPhone 6 Plus
this.BackgroundImage = BackGroundImgName + "-736h@3x.png";
else if (width >= 375)
// iPhone 6
this.BackgroundImage = BackGroundImgName + "-667h@2x.png";
else if (width >= 320 && height >= 500)
// iPhone 5
this.BackgroundImage = BackGroundImgName + "-568h@2x.png";
else if (width >= 320)
// iPhone 4
this.BackgroundImage = BackGroundImgName + "@2x.png";
else
this.BackgroundImage = BackGroundImgName + ".png";
},
Android: () => { this.BackgroundImage = BackGroundImgName + ".png"; }
);
}
Xaml无法识别iOS的
-568h
或@2x
e.t.c。它选择与确切名称匹配的图像,不带扩展名。它在android中工作,因为所有图像都有相同的名称,并且分辨率文件夹不同
作为一种解决方法,您可以通过覆盖OnSizeAllocated方法查看高度/宽度,从C#代码后面设置图像
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
string BackGroundImgName = "myimage";
Device.OnPlatform(iOS: () =>
{
if (width >= 414)
// iPhone 6 Plus
this.BackgroundImage = BackGroundImgName + "-736h@3x.png";
else if (width >= 375)
// iPhone 6
this.BackgroundImage = BackGroundImgName + "-667h@2x.png";
else if (width >= 320 && height >= 500)
// iPhone 5
this.BackgroundImage = BackGroundImgName + "-568h@2x.png";
else if (width >= 320)
// iPhone 4
this.BackgroundImage = BackGroundImgName + "@2x.png";
else
this.BackgroundImage = BackGroundImgName + ".png";
},
Android: () => { this.BackgroundImage = BackGroundImgName + ".png"; }
);
}