Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

c# - How in Xamarin Forms project use different sizes of BackgroundImage property for Page?

I've added some types of image in Resources

 - 320 x 480 login-background.png
 - 640 x 960 [email protected]
 - 640 x 1136 [email protected]
 - 750 x 1334 [email protected]

solution explorer pic

Then I've filled BackgroundImage property in xaml like "Image/login-background" xaml page pic

But it still does not work. Both the device andthe simulator render 320 x 480.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Xaml does not recognize the -568h or @2x e.t.c for iOS. It chooses the image which matches the exact name without extension. It works in android because all images have the same name and the resolution folders are different.

As a workaround you can set images from the C# code behind by looking at the height/width by Overriding the OnSizeAllocated method.

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 + "[email protected]";
        else if (width >= 375)
            // iPhone 6
            this.BackgroundImage = BackGroundImgName + "[email protected]";
        else if (width >= 320 && height >= 500)
            // iPhone 5
            this.BackgroundImage = BackGroundImgName + "[email protected]";
        else if (width >= 320)
            // iPhone 4
            this.BackgroundImage = BackGroundImgName + "@2x.png";
        else
            this.BackgroundImage = BackGroundImgName + ".png";
    },
    Android: () => { this.BackgroundImage = BackGroundImgName + ".png"; }
    );
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...