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();
}
}
Related
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);
}
}
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();
}
}
}
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>();
}
}
I am trying to re-use the service registrations in an assembly that I use through a few services in my solution. I follow the example listed from the NServiceBus website to implement the solution. When following that, unless I add the IWantCustomInitialization interface, my Init method (and IoC container implementation) appears not to function. When I have that interface implemented, I get exceptions (listed in SO questions here and here). I can't seem to get it to work that there are no exceptions AND the dependencies in my MessageHandler are being populated properly. Here is my current EndpointConfig implementation.
[EndpointSLA("00:00:30")]
public class EndpointConfig : IConfigureThisEndpoint, AsA_Server, UsingTransport<Msmq>, INeedInitialization {
public void Init() {
Configure.With().ObjectBuilderAdapter();
}
}
public class ObjectBuilderAdapter : IContainer {
readonly IDependencyInjector injector;
public ObjectBuilderAdapter(IDependencyInjectionBuilder dependencyInjectionBuilder) {
injector = dependencyInjectionBuilder.Create(); //This method does all the common service registrations that I am trying to re-use
//injector.RegisterType<ExtractIncomingPrincipal, PrincipalExtractor>();
}
public void Dispose() {
injector.Dispose();
}
public object Build(Type typeToBuild) {
return injector.Resolve(typeToBuild);
}
public IContainer BuildChildContainer() {
return new ObjectBuilderAdapter(new DependencyInjectorBuilder());
}
public IEnumerable<object> BuildAll(Type typeToBuild) {
return injector.ResolveAll(typeToBuild);
}
public void Configure(Type component, DependencyLifecycle dependencyLifecycle) {
injector.RegisterType(component);
}
public void Configure<T>(Func<T> component, DependencyLifecycle dependencyLifecycle) {
injector.RegisterType(component);
}
public void ConfigureProperty(Type component, string property, object value) {
if (injector is AutofacDependencyInjector) {
((AutofacDependencyInjector)injector).ConfigureProperty(component, property, value);
} else {
Debug.WriteLine("Configuring {0} for property {1} but we don't handle this scenario.", component.Name, property);
}
}
public void RegisterSingleton(Type lookupType, object instance) {
injector.RegisterInstance(lookupType, instance);
}
public bool HasComponent(Type componentType) {
return injector.IsRegistered(componentType);
}
public void Release(object instance) { }
}
public static class Extensions {
public static Configure ObjectBuilderAdapter(this Configure config) {
ConfigureCommon.With(config, new ObjectBuilderAdapter(new DependencyInjectorBuilder()));
return config;
}
}
Note: When I use the INeedInitialization interface, I get the ComponentNotRegisteredException when it's looking for IStartableBus.
When you are trying to swap the built in container, then you need to implement IWantCustomInitialization in the same class that implements IConfigureThisEndpoint.
You can use your own container and register all your types in there and tell NSB to use that container.
For example:
public class EndpointConfig : IConfigureThisEndpoint, AsA_Server, IWantCustomInitialization
{
public void Init()
{
var container = new ContainerBuilder().Build();
Configure.With()
.AutofacBuilder(container);
}
}
The following view crashes the iPhone
If I comment out the
using (webView = new UIWebView(webRect)) {
}
section - it does not.
using System;
using MonoTouch.UIKit;
using MonoTouch.Foundation;
using System.Drawing;
namespace WmcStarClient
{
public class EventDetailView : UIViewController
{
public WmcStarEventService.Event _event;
public UIWebView webView;
public EventDetailView ()
{
}
public EventDetailView (WmcStarEventService.Event eventParam)
{
_event = eventParam;
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
var webRect = new RectangleF(0f, 0f, 320f, 460f);
using (webView = new UIWebView(webRect)) {
}
}
}
}
has anyone seen this before?
w://
This was a bug that was fixed in the first beta of the 1.5 series:
http://monotouch.net/Releases/MonoTouch_1.4.99
When you're using the "using" construct, you are calling Dispose() on the webview, which is mapped to the "release" message in the Cocoa runtime.
So you are releasing the webview object, but you're still holding on to the reference in your class. The first time you try to access it, you're accessing a released instance.
Aren't you adding the webview to your view as a subview? If so, at what point?
The correct code, IMHO, would be:
public class EventDetailView : UIViewController
{
public WmcStarEventService.Event _event;
public UIWebView webView;
public EventDetailView ()
{
}
public EventDetailView (WmcStarEventService.Event eventParam)
{
_event = eventParam;
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
var webRect = new RectangleF(0f, 0f, 320f, 460f);
webView = new UIWebView(webRect);
View.AddSubView(webView);
}
protected override void Dispose(bool disposing)
{
if (disposing)
webView.Dispose();
base.Dispose(disposing);
}
}
Note that overriding Dispose() is not required, but it should help to release memory earlier than simply relying on the garbage collector.
This is potentialy a different/new bug - i've gotten a better response over at the monotouch forums:
http://forums.monotouch.net/yaf_postsm1500.aspx#post1500
w://
It seems very strange to me to wrap that in a using statement. It's as if your disposing something while still loading. Are you sure that's right?