Error CS1061: AppDelegate does not contain a definition for GetNativeField - xamarin.ios

I'm writting my first iPhone app using mono touch. I have only written a little bit of code, and suddenly, it does not compile anymore, I get this error:
/MyRoute/MainWindow.xib.designer.cs(85,85): Error CS1061: Type xxxx.AppDelegate' does not contain a definition forGetNativeField' and no extension method GetNativeField' of typexxx.AppDelegate' could be found (are you missing a using directive or an assembly reference?) (CS1061) (xxx)
Any idea where this error comes from and how to solve it?
Thanks! :-D
Edited: Added aditional information:
THIS IS THE MAINWINDOW DESIGNER CODE:
namespace GuiaTeleIphone {
// Base type probably should be MonoTouch.Foundation.NSObject or subclass
[MonoTouch.Foundation.Register("AppDelegate")]
public partial class AppDelegate {
private MonoTouch.UIKit.UIWindow __mt_window;
#pragma warning disable 0169
[MonoTouch.Foundation.Connect("window")]
private MonoTouch.UIKit.UIWindow window {
get {
this.__mt_window = ((MonoTouch.UIKit.UIWindow)(this.GetNativeField("window")));
return this.__mt_window;
}
set {
this.__mt_window = value;
this.SetNativeField("window", value);
}
}
}
}
THIS IS THE MAIN.CS:
namespace GuiaTeleIphone
{
public class Application
{
static void Main (string[] args)
{
UIApplication.Main (args);
}
}
// The name AppDelegate is referenced in the MainWindow.xib file.
public partial class AppDelegate : UIApplicationDelegate
{
List<RSSChannel> RemoteChannelsData;
// This method is invoked when the application has loaded its UI and its ready to run
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
// If you have defined a view, add it here:
// window.AddSubview (navigationController.View);
window.MakeKeyAndVisible ();
// do things here
return true;
}
}
}

The other partial class of xxxx.AppDelegate is missing or in a different namespace. It specifies the base class.

Related

Haxe: Native Interface properties implementable?

I've got this compiletime errors when I make some class implement an interface with properties that have been fromerly defined in some native sub class, like openfl.display.Sprite. It occurs when I'm targeting flash, not js.
Field get_someValue needed by SomeInterface is missing
Field set_someValue needed by SomeInterface is missing
Field someValue has different property access than in SomeInterface (var should be (get,set))
In contrast, there's no problem with interface definitions of 'native' methods or 'non-native' properties. Those work.
Do I have to avoid that (not so typical) use of interfaces with haxe and rewrite my code? Or is there any way to bypass this problem?
Thanks in advance.
Example:
class NativePropertyInterfaceImplTest
{
public function new()
{
var spr:FooSprite = new FooSprite();
spr.visible = !spr.visible;
}
}
class FooSprite extends Sprite implements IFoo
{
public function new()
{
super();
}
}
interface IFoo
{
public var visible (get, set):Bool; // Cannot use this ):
}
TL;DR
You need to use a slightly different signature on the Flash target:
interface IFoo
{
#if flash
public var visible:Bool;
#else
public var visible (get, set):Bool;
#end
}
Additional Information
Haxe get and set imply that get_property():T and set_property(value:T):T both exist. OpenFL uses this syntax for many properties, including displayObject.visible.
Core ActionScript VM classes (such as Sprite) don't use Haxe get/set, but are native properties. This is why they look different.
Overriding Core Properties
If you ever need to override core properties like this, here is an example of how you would do so for both Flash and other targets on OpenFL:
class CustomSprite extends Sprite {
private var _visible:Bool = true;
public function new () {
super ();
}
#if flash
#:getter(visible) private function get_visible ():Bool { return _visible; }
#:setter(visible) private function set_visible (value:Bool):Void { _visible = value; }
#else
private override function get_visible ():Bool { return _visible; }
private override function set_visible (value:Bool):Bool { return _visible = value; }
#end
}
Overriding Custom Properties
This is not needed for custom properties, which are the same on all platforms:
class BaseClass {
public var name (default, set):String;
public function new () {
}
private function set_name (value:String) {
return this.name = value;
}
}
class SuperClass {
public function new () {
super ();
}
private override function set_name (value:String):String {
return this.name = value + " Q. Public";
}
}
Need to provide the method signatures in an Interface. Currently its just a property declaration.
The error message is saying it all.
Field get_someValue needed by SomeInterface is missing
Field set_someValue needed by SomeInterface is missing
Hopefully that helps.

Ninject MVC 2 - problems with EF 4 ObjectContext

I've been dealing with this issue for a while, and still can't seem to find a solution. I have several repositories which wrap an EF 4 ObjectContext. An example is below:
public class HGGameRepository : IGameRepository, IDisposable
{
private readonly HGEntities _context;
public HGGameRepository(HGEntities context)
{
this._context = context;
}
// methods
public void SaveGame(Game game)
{
if (game.GameID > 0)
{
_context.ObjectStateManager.ChangeObjectState(game, System.Data.EntityState.Modified);
}
else
{
_context.Games.AddObject(game);
}
_context.SaveChanges();
}
public void Dispose()
{
if (this._context != null)
{
this._context.Dispose();
}
}
}
And I have the following NinjectModule:
public class DIModule : NinjectModule
{
public override void Load()
{
this.Bind<HGEntities>().ToSelf();
this.Bind<IArticleRepository>().To<HGArticleRepository>();
this.Bind<IGameRepository>().To<HGGameRepository>();
this.Bind<INewsRepository>().To<HGNewsRepository>();
this.Bind<ErrorController>().ToSelf();
}
}
Since I'm using the MVC 2 extension, these bindings default to InRequestScope().
My problem is that the ObjectContext isn't being handled properly. I get what's described here: https://stackoverflow.com/a/5275849/399584 Specifically, I get an InvalidOperationException that states:
The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.
This happens every time I try to update an Entity.
If I set my repos to bind InSingletonScope() it works, but seems like a bad idea.
What am I doing wrong?
EDIT: For clarity, I have just one ObjectContext that I want to share with all my repos per request.
You have to specify InRequestScope() in your module. Based on this article the default to transient, which is why you are getting more than one context.
public class DIModule : NinjectModule
{
public override void Load()
{
this.Bind<HGEntities>().ToSelf().InRequestScope();
this.Bind<IArticleRepository>().To<HGArticleRepository>().InRequestScope();
this.Bind<IGameRepository>().To<HGGameRepository>().InRequestScope();
this.Bind<INewsRepository>().To<HGNewsRepository>().InRequestScope();
this.Bind<ErrorController>().ToSelf().InRequestScope();
}
}
Also did you add ninject to your project via nuget package manager or the old fashion way?

Using LogManager.GetLogger with Unity

Given this class:
class Foo
{
readonly ILog log;
public Foo(ILog log)
{
this.log = log;
}
...
}
I'd like to configure Unity to inject ILog. That's easy:
container.RegisterInstance<ILog>(LogManager.GetLogger(typeof(XYZ)));
But I'd like to make Unity call LogManager.GetLogger with the type of the parent type being resolved.
This is close:
container.RegisterType<ILog>(new InjectionFactory((c, t, s) => LogManager.GetLogger(t)));
But t in this case is the type being resolved (ILog), not the type that the object is being resolved for (Foo).
I know I can do this:
container.RegisterType<Foo>(new InjectionFactory(c => new Foo(LogManager.GetLogger(typeof(Foo)));
But I don't want to have to add that crazy declaration every time I register an object.
I know this can be done in Autofac, and I know the Real Answer is not to use Unity in the first place, but can this be done? :)
Unity might not give you all the goodies some of the other containers offer but I have yet to find a feature you can't easily add.
var container = new UnityContainer();
container.AddNewExtension<TrackingExtension>();
container.RegisterType<ILog>(
new InjectionFactory((ctr, type, name) =>
{
var tracker = ctr.Resolve<ITracker>();
var parentType = tracker.CurrentBuildNode.Parent.BuildKey.Type;
return LogManager.GetLogger(parentType);
}));
var sut = container.Resolve<UsesLog>();
Assert.AreEqual(typeof(UsesLog), sut.Log.Type);
You can find the source code for the TrackingExtension here. Its located in the TecX.Unity project folder.
If you want a DI container to return you a logger based on the class’ type information, then put the type information into the public interface so the DI container can see it. It removes the need for any container specific override features and then it won’t matter if you are using Unity or AutoFac.
Someone that knows the log4net object model well might be able to give you a more efficient implementation, but try something like this:
using System;
using Microsoft.Practices.Unity;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnityLoging
{
public interface ILog<T> : log4net.ILog
{ }
public class MyLogger<T> : log4net.Core.LogImpl, ILog<T>
{
public MyLogger() : base(log4net.LogManager.GetLogger(typeof(T).Name).Logger)
{ }
}
public class ClassToLog
{
private readonly log4net.ILog log;
public ClassToLog(ILog<ClassToLog> log)
{
this.log = log;
}
public void LogMe()
{
log.Debug("Got here");
}
}
[TestClass]
public class TestClass
{
[TestMethod]
public void GenericLogRegistrationTest()
{
log4net.Config.XmlConfigurator.Configure();
IUnityContainer container = new UnityContainer();
container.RegisterType(typeof(ILog<>), typeof(MyLogger<>));
ClassToLog c = container.Resolve<ClassToLog>();
c.LogMe();
log4net.LogManager.Shutdown();
}
}
}
This seems like a very clean approach: https://github.com/roblevine/UnityLoggingExtensions

Inheritance and method invocation through an interface instance

I've been reading "The C# Programming Language. 4th Edition" and found the following code sample:
interface I<T>
{
void F();
}
class Base<U>: I<U>
{
void I<U>.F() {...}
}
class Derived<U,V>: Base<U>, I<V>
{
void I<V>.F() {...}
}
...
I<int> x = new Derived<int,int>();
x.F();
Authors state that after calling x.F() the method in Derived will be invoked, because
"Derived<int,int> effectively reimplements I<int>"
I've checked with C# 4.0 compiler and found that this statement actually invokes the method in Base. Can you explain such behaviour?
Thanks in advance.
edit: here is the working code used for check:
using System;
interface I<T>
{
void F();
}
class Base<U>: I<U>
{
void I<U>.F()
{
Console.WriteLine("F() in Base");
}
}
class Derived<U,V>: Base<U>, I<V>
{
void I<V>.F()
{
Console.WriteLine("F() in Derived");
}
}
public class MainClass
{
public static void Main()
{
I<int> x = new Derived<int,int>();
x.F();
}
}
It outputs "F() in Base", so I don't know where I am wrong.
Because both Base and Derived interface from I.
void I<V>.F() {...} The method may be called/triggered from "Derived" but is executing the method defined in the interface.
void F();

can't take address of function unless createing delegate instance

I have a class defined as below:
ref class myClass
{
PictureBox^ pic2;
public:
void setPic2() { pic2 = gcnew PictureBox; }
template<typename UnaryOperator>
void setPic2Click(Form^ x, UnaryOperator op) { pic2->Click += gcnew EventHandler(x, op); }
};
And in my Windows form class:
namespace testProject
{
public ref class Form1 : public System::Windows::Forms::Form
{
void Form1_Load(Object^ sender, EventArgs^ e)
{
rect1.setPic2();
rect1.setPic2Click(this, std::bind1st(std::mem_fun(&Form1::pic2_Click), this));
}
void pic2_Click(Object^ sender, EventArgs^ e)
{
// do something...
}
When compiled, it generated this error which is related to the rect1.setPic2Click call...:
error C3374: can't take address of 'testProject::Form1::pic2_Click' unless creating delegate instance
Basically, I tried to encapsulate the interface of the picturebox by create the instance method setPic2Click. Is this the right approach? Any suggestion how to remedy this error?
Your only mistake is that you're trying to mix managed and unmanaged C++/CLI code in a way that doesn't work (and doesn't make sense).
.NET delegates already have a bound first parameter. All you need is:
class1->setPic2Click(gcnew System::EventHandler(this, &Form1::pic2_Click));
and
void setPic2Click(System::EventHandler^ op) {pic2->Click += op;}

Resources