Hello,
Welcome to our Microsoft Q&A platform!
You got this error because there is no view in AppDelegate
, I add the RootViewController
and add this LogoImage
into its View
, you could refer to the following code:
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
Window = new UIWindow(UIScreen.MainScreen.Bounds);
var MainViewController = new UIViewController();
Window.RootViewController = MainViewController;
Window.MakeKeyAndVisible();
MainViewController.View.BackgroundColor = UIColor.White;
LogoImage = new UIImageView(UIImage.FromBundle("Launchscreenlogo"));// load the Assets.xcassets image
LogoImage.ContentMode = UIViewContentMode.ScaleAspectFill;
LogoImage.Tag = 1234;
LogoImage.Frame = MainViewController.View.Frame;
MainViewController.View.AddSubview(LogoImage);
MainViewController.View.BringSubviewToFront(LogoImage);
return true;
}
You could also create a new controller to load a imageview and add a button to test, right click the iOS project ->new Item -> UIViewController(Apple) ->named( my class name is MyViewController
) , then refer to the following code:
private UIImageView LogoImage;
public override void ViewDidLoad()
{
View = new UniversalView();
base.ViewDidLoad();
View.BackgroundColor = UIColor.White;
LogoImage = new UIImageView(UIImage.FromBundle("Launchscreenlogo"));
LogoImage.ContentMode = UIViewContentMode.ScaleAspectFill;
LogoImage.Tag = 1234;
LogoImage.Frame = new CoreGraphics.CGRect(0,0,UIScreen.MainScreen.Bounds.Size.Width, UIScreen.MainScreen.Bounds.Size.Height);// important
View.AddSubview(LogoImage);
UIButton mybutton = new UIButton(new CoreGraphics.CGRect(0, 100, 100, 40));
mybutton.SetTitle("click to remove", UIControlState.Normal);// add this button to test
mybutton.TouchUpInside += (sender, e) => {
G_RemoveLogo(sender, e);
};
View.AddSubview(mybutton);
}
private void G_RemoveLogo(object sender, System.EventArgs e)
{
//remove image
var LogoView = View.ViewWithTag(1234);
if (null != LogoView)
LogoView.RemoveFromSuperview();
}
FinishedLaunching
in AppDelegate
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
Window = new UIWindow(UIScreen.MainScreen.Bounds);
var MainViewController = new MyViewController();
Window.RootViewController = MainViewController;
Window.MakeKeyAndVisible();
return true;
}
You could also load ViewController from forms refer to https://stackoverflow.com/questions/63812069/display-viewcontroller-from-native-forms.
About how to displaythe image in Xamarin.iOS, you could refer to https://learn.microsoft.com/en-us/xamarin/ios/app-fundamentals/images-icons/displaying-an-image?tabs=macos
------------update-----------
Program class(use MyViewController
)
using System.Linq;
using System.Text;
using UIKit;
namespace ImageViewDemo.iOS
{
[Register("AppDelegate")]
public class Program : UIApplicationDelegate
{
private MyViewController MainViewController;
public override void FinishedLaunching(UIApplication application)
{
var Window = new UIWindow(UIScreen.MainScreen.Bounds);
MainViewController = new MyViewController();
Window.RootViewController = MainViewController;
Window.MakeKeyAndVisible();
}
}
}
Program class(use UIViewController
)
namespace ImageViewDemo.iOS
{
[Register("AppDelegate")]
public class Program : UIApplicationDelegate
{
private UIViewController MainViewController;
private UIImageView LogoImage;
public override void FinishedLaunching(UIApplication application)
{
var Window = new UIWindow(UIScreen.MainScreen.Bounds);
MainViewController = new UIViewController();
Window.RootViewController = MainViewController;
Window.MakeKeyAndVisible();
MainViewController.View.BackgroundColor = UIColor.White;
LogoImage = new UIImageView(UIImage.FromBundle("Launchscreenlogo"));
LogoImage.ContentMode = UIViewContentMode.ScaleAspectFill;
LogoImage.Tag = 1234;
LogoImage.Frame = MainViewController.View.Frame;
MainViewController.View.AddSubview(LogoImage);
MainViewController.View.BringSubviewToFront(LogoImage);
RunGame();
}
private void G_RemoveLogo(object sender, System.EventArgs e)
{
//remove the image
......
}
internal static void RunGame()
{
......
}
}
}
Pay attition to comment out this line of code in Appdelegate.cs
// [Register("AppDelegate")]
Best Regards,
Wenyan Zhang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.