I am creating an iOS application using Xamarin.Forms.Labs.iOS library.
I followed the steps as presented in this link https://github.com/XLabs/Xamarin-Forms-Labs/wiki
and replaced
UIApplicationDelegate
with
XFormsApplicationDelegate
in my AppDelegate.cs.
Here is my AppDelegate.cs :
[Register("AppDelegate")]
public partial class AppDelegate : XFormsApplicationDelegate
{
UIWindow window;
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
Forms.Init();
window = new UIWindow(UIScreen.MainScreen.Bounds);
window.RootViewController = App.GetMainPage().CreateViewController();
window.MakeKeyAndVisible();
return true;
}
}
and my Main.cs file has
public class Application
{
// This is the main entry point of the application.
static void Main(string[] args)
{
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main(args, null, "AppDelegate");
}
}
Is there anything wrong in my implementation ? I added all the assemblies and the code compiles without any errors. The app won't start and crashes with the following exception.
Objective-C exception thrown. Name: NSInternalInconsistencyException Reason: Unable to instantiate the UIApplication delegate instance. No class named AppDelegate is loaded.
Kindly, guide me in the right direction.
Thanks.
This is so silly. I just had to do rebuild instead of build to make that work.
Related
I am working on an Android c# app where I have an class extends from Application called MyApplication. Inside MyApplication file, I have this method called getDataFromDB(). I am trying to call this method from my Activity but I am getting this exception during runtime:
System.InvalidCastException:
MyApplication.cs
public class MyApplication : Application
{
public MyApplication(IntPtr handle, JniHandleOwnership ownerShip) : base(handle, ownerShip)
{
}
public override void OnCreate()
{
base.OnCreate();
getDataFromDB();
}
public void getDataFromDB()
{ // code
}
}
Activity.cs
MyApplication application = ((MyApplication)this.ApplicationContext); //here's the location of the exception
application.getDataFromDB();
I don't have a clue why this exception is thrown. It doesn't seems to me that my casting is wrong. Would you please help me ?
The ApplicationContext is not necessarily the same object as the Application instance. I've seen this most often in emulators, but it can also be device specific.
In a Java app, you could cast the object returned by Activity#getApplication(). But according to a post on the Xamarin forum, an equivalent method does not exist in Xamarin. Instead, you can cast the Application property:
MyApplication app = (MyApplication) Application;
I'm not sure when the property is set, so this may not work in a field initializer.
Another option is to have MyApplication save a static reference to itself in OnCreate(), and provide a static getter. Although static fields are usually evil, this works because the Application instance is effectively a singleton, and its OnCreate will be called before any other component is created. The static reference can't leak the application because the application already has the same lifetime as the process.
Well, I solved it out by removing the Arguments of the contructor of MyApplication like that:
public class MyApplication : Application
{
public MyApplication() : base(handle, ownerShip) //here's the editing location
{
}
public override void OnCreate()
{
base.OnCreate();
getDataFromDB();
}
public void getDataFromDB()
{ // code
}
}
Then I called the Application:
MyApplication application = new MyApplication();
there are similar questions here but this one is specific. I have a solution and in it two projects. Main project and run time project, in order to use run time project i need to add reference to it in main project. In run time project i need to use static object from main project, in order to do that i need to add reference which i cant to because there would be circular dependence. I read that i could use API function how can I implement that?
Thanks.
namspace mainProject
{
public static MyClass Object;
}
public sealed class RuntimeComponentClass : IBackgroundTask
{
BackgroundTaskDeferral _deferral;
public void Run(IBackgroundTaskInstance taskInstance)
{
_deferral = taskInstance.GetDeferral();
//here i need to access static object from main project
_deferral.Complete();
}
}
First thing is that you cannot declare a static object just inside the namespace. I am assuming you have a class declared inside the namespace and static object is a member of that class
if you have following
namspace mainProject
{
public class AStaticClass{
public static MyClass Object;
}
}
you can definitely access the public static member from other classes.
public sealed class RuntimeComponentClass : IBackgroundTask
{
BackgroundTaskDeferral _deferral;
public void Run(IBackgroundTaskInstance taskInstance)
{
_deferral = taskInstance.GetDeferral();
AStaticClass.Object is accessible
//here i need to access static object from main project
_deferral.Complete();
}
}
However this is not a good idea to have static public member if you have any multi threading scenario.
This is in support of my comment on the original post.
I we solved the problem by moving all the classes in run time component project, in this way that one reference that exists is enough. Thanks everybody!
I'm trying to access an icon from my resources and it's not available. It seems to be available in a window. How can I access them?
namespace My_Application
{
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
/* Compile error "'System.Collections.IDictionary' does not contain a definition for 'Resources'…" */
Properties.Resources.Application;
base.OnStartup(e);
}
}
This one works! It's in a Window Class
public partial class MainWindow : Window
{
public MainWindow()
{
/* Available */
Properties.Resources.Application;
}
}
So for some reason within App.xaml.cs, it's necessary to disambiguate using the project namespace whenever using automatic Settings or Resources classes. using directives and assembly references are verified to be irrelevant.
I've been searching all over for the answer to this question, and while I realize it's likely very trivial, somehow the answer eludes me.
I need to show a second window (launched from clicking a menu item, if that matters). I know perfectly well how to do this with winforms, but I'm not sure what the monomac/NSWindow equivalent is. I need to do this:
MyWindowClass myWindow = new MyWindowClass();
myWindow.Show();
The best info I can find on the subject says that the following should work:
MyWindowClass myWindow = new MyWindowClass();
myWindow.MakeKeyAndOrderFront(this);
But when I try that, it tells me that MyWindowClass() needs an overload, so I look at the constructor and see that it's asking for an IntPtr. Not knowing any better, I try this:
MyWindowClass myWindow = new MyWindowClass(new IntPtr());
myWindow.MakeKeyAndOrderFront(this);
This code will run without error, yet nothing happens when I try to launch the window.
I'm sure I'm just making a silly mistake, but I just haven't been able to find anything on the subject. Any help would be greatly appreciated.
Edit: For reference, here's the relevant portion of the constructor:
public MyWindowClass (IntPtr handle) : base (handle)
{
Initialize ();
}
Additional info: I'm trying to run the code above from within AppDelegate.cs in the following method:
partial void showWindow (MonoMac.Foundation.NSObject sender){
MyWindowClass myWindow = new MyWindowClass(new IntPtr());
myWindow.MakeKeyAndOrderFront(this);
}
EVEN MORE CODE AHOY:
public partial class ViewPaths : MonoMac.AppKit.NSWindow
{
#region Constructors
public ViewPaths ()
{
Initialize();
}
// Called when created from unmanaged code
public ViewPaths (IntPtr handle) : base (handle)
{
Initialize ();
}
// Called when created directly from a XIB file
[Export ("initWithCoder:")]
public ViewPaths (NSCoder coder) : base (coder)
{
Initialize ();
}
// Shared initialization code
void Initialize ()
{
}
#endregion
}
And then the actual instantiation:
public partial class AppDelegate : NSApplicationDelegate
{
MainWindowController mainWindowController;
ViewPaths display;
public AppDelegate ()
{
}
public override void FinishedLaunching (NSObject notification)
{
mainWindowController = new MainWindowController ();
mainWindowController.Window.MakeKeyAndOrderFront (this);
}
partial void viewPaths (MonoMac.Foundation.NSObject sender){
display = new ViewPaths();
display.MakeKeyAndOrderFront(this);
}
}
}
This shows a window with no UI elements of any kind.
Just add a default constructor without the handle parameter. Make sure MyWindowClass subclasses NSWindow and it should work.
Also, you may need to keep a reference to your myWindow around - so that it does not get garbage collected.
For clarity, here's the final code that solved the problem, should anyone else ever google this:
Since I had created a new monomac window with a controller, I needed to create an instance of that controller, and show the controller's window:
MyWindowController myWindow = new MyWindowController();
myWindow.Window.MakeKeyAndOrderFront(this);
This did not need a new constructor without a handle parameter implemented - that solution was working around the problem I'd created by instantiating the incorrect thing.
I have a strange problem in my project. I have a class that inherits from a base class (which again inherits from another base class) and overrides a function. However, when that function is called it never calls the overridden function, but the base function.
However, when I override that function in the middle class it is called. But this is confusing: let's explain with a drawing :)
lib GuiShared
class bScreen
virtual function InitializeRoc
lib TigerControlRoot
class bTigerScreen
override function InitializeRoc <-- when overriden here it gets called
lib TigerControlRootCommonScreens
class CheckInRules
override function InitializeRoc <-- not called :s
The constructor gets called however...
Here's my (simplified) code:
The shared base class
namespace Ppb.GuiShared.Screens {
public partial class bScreen<T> : Ppb.Controls.pPanel where T : FrameworkMiddleware.Framework.Remoting.Remotable, FrameworkMiddleware.IInitialize, new() {
public virtual void Load(bMain<T>.LoadEventArgs args) {
log.Trace("InitializeRoc " + this.GetType().FullName);
InitializeRoc(args);
_hasLoaded = true;
}
protected virtual void InitializeRoc(bMain<T>.LoadEventArgs args) { }
}
}
project base class
namespace Tiger.ControlRoot.Screens {
public partial class bTigerScreen : Ppb.GuiShared.Screens.bScreen<roc.Tiger> {
public bTigerScreen(GuiSettings settings, roc.Tiger tiger)
: base(settings, tiger) {
InitializeComponent();
InitializeMenu();
}
}
}
The failing class (or any other class from that lib)
namespace Tiger.ControlRoot.CommonScreens {
[ControlRoot.Screens.TigerScreenInfo("Testje", Tiger.ControlRoot.Screens.TigerScreenInfoAttribute.elevel.User, true)]
public class CheckInRules : ControlRoot.Screens.bTigerScreen {
public CheckInRules(GuiSettings settings, roc.Tiger tiger)
: base(settings, tiger) {
}
protected override void InitializeRoc(Ppb.GuiShared.bMain<TigerMiddleware.TigerRoc.Tiger>.LoadEventArgs args) {
base.InitializeRoc(args);
}
}
}
And if that wasn't enough, when I try to call some function on the base class I receive a TypeLoadException.
GenericArguments[0], 'TigerMiddleware.TigerRoc.Tiger', on 'Ppb.GuiShared.bMain`1+LoadEventArgs[T]' violates the constraint of type parameter 'T'.
Similar code with the same GuiShared lib is used in another project and there there are no issues.
Okay, thanks for all (the) response(s), but I fixed it in the meantime.
The problem was the following:
The failing class is in an dll from which its output path in debug mode is set to the executable's plugin folder. No problem so far, but it also copies its dependencies into that folder.
However, some of the dependencies are already copied to the executable's root folder. The executable when it startups searches all the plugins in the plugin folder and when required instantiates the plugin.
The problem then is that the plugin uses the dependencies from in the plugins folder while the executable uses the dependencies from the root folder which are basically the same file in a different dir, so while running the clr sees them as 2 different dll's and that really confuses the clr :).
So when the shared dependencies aren't copied to the plugins folder, everything runs fine because the plugins use the depedencies from the root folder and thus the same dll's.