Interface Builder does not see my subclassed UILabel - xamarin.ios

In MonoTouch I have defined the following class:
using System;
using MonoTouch.UIKit;
using MonoTouch.Foundation;
using System.Drawing;
namespace MyApp
{
public class WUILabel : UILabel
{
public WUILabel () : base() { this.SetDefaults(); }
public WUILabel(NSCoder coder) : base(coder) { this.SetDefaults(); }
public WUILabel(NSObjectFlag t) : base(t) { this.SetDefaults(); }
public WUILabel(IntPtr handle) : base(handle) { this.SetDefaults(); }
public WUILabel(RectangleF frame) : base(frame) { this.SetDefaults(); }
private void SetDefaults()
{
this.Font = UIFont.FromName("AmericanTypewriter", 15f);
this.TextColor = UIColor.Black;
}
}
}
In Interface Builder, I am setting the "Class" attribute of my label to "WUILabel".
When I run the application, the Application Output contains the error "Unknown class WUILabel in Interface Builder file." Has anyone successfully gotten this to work, or can anyone tell me what I'm doing wrong?

You have to decorate your class with the RegisterAttribute.
Check this answer.

Related

Xamarin.ios native ads admob

I have been trying to figure out how to implement native google ads (admob) in my Xamarin.iOS app. (not xamarin forms)
I have googled like crazy but can't find any good examples.
Does anyone have any code that they could share?
This is my code so far, but the adloader.delegate is always null..
public partial class ExploreController : UIViewController
{
readonly AdLoader adLoader;
public ExploreController (IntPtr handle) : base (handle)
{
adLoader = new AdLoader(
"ca-app-pub-3940256099942544/3986624511",
this,
new AdLoaderAdType[] { AdLoaderAdType.Native },
new AdLoaderOptions[] { new AdLoaderOptions() });
adLoader.Delegate = new MyAdLoaderDelegate();
var request = Request.GetDefaultRequest();
}
}
public class MyAdLoaderDelegate : NSObject, IUnifiedNativeAdLoaderDelegate
{
public MyAdLoaderDelegate()
{
}
public void DidReceiveUnifiedNativeAd(AdLoader adLoader, NativeAd nativeAd)
{
Debug.WriteLine("DidReceiveUnifiedNativeAd");
}
public void DidFailToReceiveAd(AdLoader adLoader, NSError error)
{
Debug.WriteLine("DidFailToReceiveAd");
//base.DidFailToReceiveAd(adLoader, error);
}
public void DidFinishLoading(AdLoader adLoader)
{
Debug.WriteLine("DidFinishLoading");
//base.DidFinishLoading(adLoader);
}
}

Show Master on UISplitViewController by Default

When I create a new "Master Detail App" with Visual Studio for Mac (v8.4.5), the default behavior of the UISplitViewController is to show the Detail page first when it appears on an iPhone in Portrait mode.
I would rather (as I think most people would rather) have the Master page show by default. In my case, the Master page is a table view that holds a list of contacts.
This question is similar to: UISplitViewController in portrait on iPhone shows detail VC instead of master but for Xamarin.iOS
Similar to the solutions suggested there, I have attempted to assign a delegate without success:
public class ContactsSplitViewControllerDelegate : UISplitViewControllerDelegate
{
public override bool EventShowViewController(UISplitViewController splitViewController, UIViewController vc, NSObject sender)
{
return true;
}
public override bool EventShowDetailViewController(UISplitViewController splitViewController, UIViewController vc, NSObject sender)
{
return true;
}
}
public partial class ContactsSplitViewController : UISplitViewController
{
public ContactsSplitViewController (IntPtr handle) : base (handle)
{
this.Delegate = new ContactsSplitViewControllerDelegate();
}
}
set the PreferredDisplayMode
public override void ViewDidLoad()
{
base.ViewDidLoad();
this.PreferredDisplayMode = UISplitViewControllerDisplayMode.AllVisible;
}
After some experimentation, it seems that overriding CollapseSecondViewController on the delegate will work though I'm not yet convinced it's the proper solution.
using Foundation;
using System;
using UIKit;
namespace MasterDetailTest
{
public class SplitViewControllerDelegate : UISplitViewControllerDelegate
{
public override bool CollapseSecondViewController(UISplitViewController splitViewController, UIViewController secondaryViewController, UIViewController primaryViewController)
{
return true;
}
}
public partial class MainPageSplitViewController : UISplitViewController
{
public MainPageSplitViewController (IntPtr handle) : base (handle)
{
this.Delegate = new SplitViewControllerDelegate();
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// When implemented in my project, I found I needed to set this
// or the delegate would not be called.
this.SetNeedsFocusUpdate();
}
}
}

Xamarin iOS crashes in NSBundle.MainBundle.LoadNib("CustomView", this, null)

I have created new CustomView.xib file and associated to code-behind class. But when i try to initialise the view, the app getting crashed as below mentioned error.
MonoTouch: Could not install sigaction override, unexpected sigaction implementation.
NSBundle.MainBundle.LoadNib("CustomView", this, null); // App crashes here.
CS class file:
public partial class CustomView : UIView
{
public CustomView(IntPtr handle) : base(handle)
{
}
public override void AwakeFromNib()
{
base.AwakeFromNib();
}
}
Designer class:
[Register("CustomView")]
partial class CustomView
{
[Outlet]
UIKit.UIView contentView { get; set; }
void ReleaseDesignerOutlets ()
{
if (contentView != null) {
contentView.Dispose ();
contentView = null;
}
}
}
Note:
Build Action is set to interfaceDefinition.
File owner of the xib is set the class name "CustomView"
AwakeFromNib override method also implemented.
Thanks in advance.
Edited:
We came across with similar kind of issue after a long time but with different exceptions says "Object Null Reference" on "contentView" in the AwakeFromNib method. App crashes when you try to access the ContentView inside the AwakeFromNib method. This happened only on iOS 9.3.x. The following fix might help someone if they face the same kind of issue.
[Export("initWithFrame:")]
public SampleHeaderView(CGRect frame) : base(frame)
{
Initialize();
}
[Export("initWithCoder:")]
public SampleHeaderView(NSCoder coder) : base(coder)
{
Initialize();
}
public override void AwakeFromNib()
{
base.AwakeFromNib();
}
private void Initialize()
{
if (this.ContentView != null)
{
this.ContentView.BackgroundColor = UIColor.Red();
}
}

ShowViewModel does not work if called too early

In MvvmCross 4.1.4 for Window Universal App (UWP) platform, if we call ShowViewModel too early within ViewModel (like in Constructor, Init, or Start event) then it does not navigate to another model.
public class FirstViewModel : MvxViewModel
{
public FirstViewModel()
{
ShowViewModel<SecondViewModel>();
}
}
Note that it works just fine for iOS and Android platform.
It is a bug of MvvmCross (according to this https://github.com/MvvmCross/MvvmCross/issues/1223).
The work around solution is to trigger the navigation from some events like View_Loaded or View_GotFocus within the View:
public sealed partial class FirstView : MvxWindowsPage
{
public FirstView()
{
this.InitializeComponent();
this.Loaded += FirstView_Loaded;
}
private void FirstView_Loaded(object sender, RoutedEventArgs e)
{
var viewModel = base.ViewModel as FirstViewModel
if (viewModel != null)
{
viewModel.Initialise();
}
}
}
ViewModel updated:
public class FirstViewModel : MvxViewModel
{
public FirstViewModel()
{
}
public void Initialise()
{
//Navigate here
ShowViewModel<SecondViewModel>();
}
}

servicestack AppHostHttpListenerBase handlerpath parameter not working?

not sure if I am missing something here. I am using the AppHostHttpListenerBase in a unit test to test a service and in its constructor I pass "api" for the handlerPath parameter. I have a service registered at /hello/{Name} and am using version 3.9.17 of servicestack.
Within the Config method of my appHost class if I access
EndpointHostConfig.Instance.ServiceStackHandlerFactoryPath
it retrurns "api"
Once I am back in the unit test the same call returns null
If I try and call the service with /hello/test it works.
If I use /api/hello/test it fails
It appears that the AppHostHttpListenerBase is loosing the handlerPath ?
Does this sound like a bug or am I missing something ?
below is the code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using ServiceStack.ServiceClient.Web;
using ServiceStack.ServiceInterface;
using ServiceStack.Text;
using ServiceStack.WebHost.Endpoints;
namespace Bm.Tests
{
/// <summary>
/// Test self hosting for unit tests
/// </summary>
[TestFixture]
public class TestService
{
private TestServiceAppHost _apphost;
private const string HOST_URL = #"http://localhost:1337/";
[TestFixtureSetUp]
public void TestFixtureSetUp()
{
_apphost = new TestServiceAppHost();
_apphost.Init();
_apphost.Start(HOST_URL);
}
[Test]
public void TestHelloServiceJson()
{
var prefix = EndpointHostConfig.Instance.ServiceStackHandlerFactoryPath;
Assert.AreEqual("api", prefix, "Should be api");
var client = new JsonServiceClient(HOST_URL);
var response = client.Send<HelloResponseTest>(new HelloTest() { Name = "Todd" });
Assert.AreEqual("Hello, Todd", response.Result);
}
[TestFixtureTearDown]
public void TestFixtureTearDown()
{
_apphost.Stop();
_apphost.Dispose();
}
}
public class HelloTest
{
public string Name { get; set; }
}
public class HelloResponseTest
{
public string Result { get; set; }
}
public class HelloServiceTest : ServiceBase<HelloTest>
{
protected override object Run(HelloTest request)
{
return new HelloResponseTest { Result = "Hello, " + request.Name };
}
}
//Define the Web Services AppHost
public class TestServiceAppHost : AppHostHttpListenerBase
{
public TestServiceAppHost() : base("testing HttpListener", "api", typeof(HelloServiceTest).Assembly) { }
public override void Configure(Funq.Container container)
{
// this works and returns api
var prefix = EndpointHostConfig.Instance.ServiceStackHandlerFactoryPath;
Routes
.Add<HelloTest>("/hello")
.Add<HelloTest>("/hello/{Name}");
}
}
}
If you want the handler root path to be /api you need to add that to the listener url, i.e:
_apphost.Start("http://localhost:1337/api/");

Resources