Animating a custom property in a CALayer - xamarin.ios

I'm trying to get animation working on a custom property in a CALayer.
But I just just am not able to figure out how to get this working correctly. The key "myCounter" is never sent to NeedsDisplayForKey. Are there some steps I'm missing? Below is the class I'm testing which I add to a layer elsewhere. Has anyone got a custom property to animate using monotouch?
public class TestProperty : CALayer
{
//this line updated based on feedback below**********
public uint myCounter { [Export ("myCounter")] get; [Export setMyCounter:")] set; }
public TestProperty ()
{
CABasicAnimation anim = CABasicAnimation.FromKeyPath("myCounter");
anim.From = NSNumber.FromInt32(1);
anim.To = NSNumber.FromInt32(10);
anim.Duration = 1.0f;
anim.RepeatCount = float.MaxValue;
anim.AutoReverses = true;
this.AddAnimation(anim,null);
}
[Export ("needsDisplayForKey:")]
static bool NeedsDisplayForKey (NSString key)
{
Console.WriteLine("{0}", key.ToString());
if(key.Equals("myCounter"))
{
return true; //never gets here
}
else
return false;
}
}

MonoTouch doesn't have the same automatic KVC registration support that MonoMac has yet, so you should use:
public uint myCounter { [Export ("myCounter")] get; [Export ("setMyCounter:")] set; }

This has unfortunately been impossible to do with MonoTouch - but we've fixed it for the next beta (5.3.3) which will hopefully be released soon.
Once 5.3.3 has been released you can use this sample: https://github.com/xamarin/monotouch-samples/tree/monotouch-5.4/CustomPropertyAnimation to see how to do it.

Related

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();
}
}

I'm sorry, but it's another: Found shared references to a collection

Recently I've been adding some features to my local project, and I'm struggling with this one part. The short of it is NHibernate give me the line:
Found shared references to a collection: Page.Menus
The simple part of it is, I only want to save the relational map that binds menus to pages, which you can see the Reference below in PageMap. I should add that loading data works great, it's the saving that's killing me.
I spent a lot of time yesterday digging through here, and the good ole web, trying to find the answer, and I just kept striking out. Maybe it's bad searching on my part, but I feel I tried everything. If you know where it is can you please supply it? (thanks)
For the actual details, I've tried to simplify what's going on. I've added the PageReposity, UnitOfWork, and the proxy objects, as well as their mappings.
Where I get hazy on is the cascade, and how to save the relationship table (many to many)
For the first part, here is what happens when I save (Add). I've actually done this a few ways within the PageRepository. Since I'm strugging with the Add(), I've included it here:
public override bool Add(Page entity)
{
UnitOfWork.Save(entity);
/* I have also tried doing the following below, which doesn't help
for (var index = 0; index < entity.Menus.Count; index++)
{
UnitOfWork.Save(entity.Menus[index]);
}
*/
UnitOfWork.Commit(); // bam, error!
return true;
}
In the UnitOfWork I've setup the following in the ctor (the ISession is injected each time via ninject like so:
// DomainModule
...
Bind<ISFactory>().To<NHibernateSessionFactory>()
.InSingletonScope()
.WithConstructorArgument("connectionString", _connectWeb);
...
// Back to the UnitOfWork
...
private ISession Session { get; set; }
...
public UnitOfWork(ISFactory sessionFactory)
{
_sessionFactory = sessionFactory.GetSessionFactory();
Session = _sessionFactory.OpenSession();
Session.FlushMode = FlushMode.Never; // I have also tried FlushMode.Commit
_transaction = Session.BeginTransaction(IsolationLevel.ReadCommitted);
}
...
public void Save(object obj)
{
Session.Save(obj);
}
...
public void Commit()
{
if (!_transaction.IsActive)
{
throw new InvalidOperationException("Oops! We don't have an active transaction");
}
try
{
_transaction.Commit();
Session.Flush(); // I did this FlushMode.Never was set
}
catch (Exception exception)
{
_transaction.Rollback();
throw;
}
}
I've got 3 classes here:
Page, Menu, and Link.
public class Page : IEntity<int>
{
public virtual int Id { get; set; }
public virtual IList<Menu> Menus { get; set; }
}
public class Menu : IEntity<int>
{
public virtual int Id { get; set; }
public virtual IList<Link> Links { get; set; }
}
public class Link : IEntity<int>
{
public virtual int Id { get; set; }
public virtual DateTime CreatedDate { get; set; }
public virtual string Url { get; set; }
}
Then I also have a Mappings:
public class PageMap : ClassMap<Page>
{
public PageMap()
{
Id(x => x.Id).GeneratedBy.Native();
HasManyToMany(x => x.Menus)
.Table("MenuToPage")
.ParentKeyColumn("FkPageId")
.ChildKeyColumn("FkMenuId").Cascade.SaveUpdate(); // the cascade is new here just trying to see if it helps
}
}
public class MenuMap : ClassMap<Menu>
{
public MenuMap()
{
Id(x => x.Id); // I had .GeneratedBy.Native(); attached here too.
HasManyToMany(x => x.Links)
.Table("MenuToLinks")
.ChildKeyColumn("FkLinksId")
.ParentKeyColumn("FkMenuId")
.OrderBy("MenuOrder ASC")
.Not.LazyLoad()
.Cascade.None(); // the cascade is new here just trying to see if it helps
}
}
public class LinkMap : ClassMap<Link>
{
public LinkMap()
{
Id(x => x.Id).GeneratedBy.Native();
Map(x => x.Url);
Map(x => x.CreatedDate);
Map(x => x.ModifiedDate);
References(x => x.MetaData, "FkMetaDataId").Not.Nullable().Not.LazyLoad();
}
}
Can anyone help me or point me in a direction, I'd really appreciate your help.
Like always thank you,
Kelly
Unfortunately you have posted everything but the construction of your objects before you safe them.
Usually this error can occur if you assign the same collection of entities to different instances. For example (pseudo code)
var menuList = new List<Menu>();...
pageA.Menus = menuList;
pageB.Menus = menuList;
This would set the reference of menuList to both, pageA.Menus and pageB.Menus.
Instead, assign all items of menuList to each page with pageA.AddRange(menuList) or a loop or whatever...

UICollectionView - Curious flow layout

I'm experiencing with the UICollectionView(Controller) for the first time. Actually it should be as simple as working with TableViews, it's not though.
Rather than showing all images in a flow (several rows) just the top row is displayed. All the other images are somewhere... scrolling is enabled but nothing happens, no bouncing, no scrolling, ...
And after an orientation change (and back) some more images are visible but they appear randomly. After every orientation change they appear at an other location.
My example should have 7 images.
My properties in IB:
First time:
After rotating (and back):
And my source code to implement the photo gallery.
using System;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Collections.Generic;
using Xamarin.Media;
using MonoTouch.AssetsLibrary;
using MonoTouch.CoreGraphics;
using System.Diagnostics;
using System.Linq;
using System.Drawing;
namespace B2.Device.iOS
{
public partial class TagesRapportDetailRegieBilderCollectionViewController : UICollectionViewController
{
private const string Album = "Rapport";
public TagesRapportDetailRegieBilderSource Source { get; private set; }
private TagesRapportDetailRegieBilderDelegate _delegate;
public TagesRapportDetailRegieBilderCollectionViewController (IntPtr handle) : base (handle)
{
Source = new TagesRapportDetailRegieBilderSource(this);
_delegate = new TagesRapportDetailRegieBilderDelegate(this);
// Delegate - Muss im konstruktor sein. ViewDidLoad geht nicht!
CollectionView.Delegate = _delegate;
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Cell Klasse registrieren
CollectionView.RegisterClassForCell (typeof(ImageCell), new NSString("imageCell"));
// DataSource
CollectionView.Source = Source;
// Bilder laden
LoadImages();
}
private void LoadImages()
{
Source.Images.Clear();
var assetsLibrary = new ALAssetsLibrary();
assetsLibrary.Enumerate(ALAssetsGroupType.Album, GroupsEnumeration, GroupsEnumerationFailure);
}
private void GroupsEnumeration(ALAssetsGroup group, ref bool stop)
{
if (group != null && group.Name == Album)
{
//notifies the library to keep retrieving groups
stop = false;
//set here what types of assets we want,
//photos, videos or both
group.SetAssetsFilter(ALAssetsFilter.AllPhotos);
//start the asset enumeration
//with ALAssetsGroup's Enumerate method
group.Enumerate(AssetsEnumeration);
CollectionView.ReloadData();
}
}
private void AssetsEnumeration(ALAsset asset, int index, ref bool stop)
{
if (asset != null)
{
//notifies the group to keep retrieving assets
stop = false;
//use the asset here
var image = new UIImage(asset.AspectRatioThumbnail());
Source.Images.Add(image);
}
}
private void GroupsEnumerationFailure(NSError error)
{
if (error != null)
{
new UIAlertView("Zugriff verweigert", error.LocalizedDescription, null, "Schliessen", null).Show();
}
}
}
public class TagesRapportDetailRegieBilderDelegate : UICollectionViewDelegateFlowLayout
{
private TagesRapportDetailRegieBilderCollectionViewController _controller;
public TagesRapportDetailRegieBilderDelegate(TagesRapportDetailRegieBilderCollectionViewController controller)
{
_controller = controller;
}
public override System.Drawing.SizeF GetSizeForItem(UICollectionView collectionView, UICollectionViewLayout layout, NSIndexPath indexPath)
{
var size = _controller.Source.Images[indexPath.Row].Size.Width > 0
? _controller.Source.Images[indexPath.Row].Size : new SizeF(100, 100);
size.Width /= 3;
size.Height /= 3;
return size;
}
public override UIEdgeInsets GetInsetForSection(UICollectionView collectionView, UICollectionViewLayout layout, int section)
{
return new UIEdgeInsets(50, 20, 50, 20);
}
}
public class TagesRapportDetailRegieBilderSource : UICollectionViewSource
{
private TagesRapportDetailRegieBilderCollectionViewController _controller;
public List<UIImage> Images { get; set; }
public TagesRapportDetailRegieBilderSource(TagesRapportDetailRegieBilderCollectionViewController controller)
{
_controller = controller;
Images = new List<UIImage>();
}
public override int NumberOfSections(UICollectionView collectionView)
{
return 1;
}
public override int GetItemsCount(UICollectionView collectionView, int section)
{
return Images.Count;
}
public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
{
var cell = collectionView.DequeueReusableCell(new NSString("imageCell"), indexPath) as ImageCell;
cell.Image = Images[indexPath.Row];
return cell;
}
}
public class ImageCell : UICollectionViewCell
{
UIImageView imageView;
[Export ("initWithFrame:")]
public ImageCell (System.Drawing.RectangleF frame) : base (frame)
{
imageView = new UIImageView(frame);
imageView.AutoresizingMask = ~UIViewAutoresizing.None;
ContentView.AddSubview (imageView);
}
public UIImage Image
{
set
{
imageView.Image = value;
}
}
}
}
If all you want to do is displaying the cells in an uniform grid, overriding GetSizeForItem shouldn't be necessary in the first place. Just configure the cellsize property of your flow layout either in IB or programatically during ViewDidLoad and be done with it.
There's another problem with your code:
group.Enumerate(AssetsEnumeration)
This will run asynchronously. That means:
CollectionView.ReloadData();
Will only cover a small subset of your images. It would be better to issue a ReloadData() when group == null, which indicates that the enumeration is complete.
You could also avoid ReloadData alltogether and call CollectionView.InsertItem() everytime you've added an image. This has the benefit of your items become immediately visible instead of all of them at once after everything has been enumerated - which may take some time (on the device). The downside is that you've got to be careful to not run into this.

Automapper ObservableCollection – refreshing is not working

I have small WPF application. There are 5 projects in solution.
I want separate DOMAIN classes with UI ENTITIES and I want to use AUTOMAPPER.
You can download whole solution here: TestWPFAutomapper.zip
Domain class(Domain.Source.cs) with UI Entity(Entities.Destination.cs) have same signature.
In Entities.Destination.cs I would like to put other logic.
namespace DOMAIN
{
public class Source
{
public int Id { get; set; }
public int Position { get; set; }
}
}
using System.ComponentModel;
namespace ENITITIES
{
public class Destination : INotifyPropertyChanged
{
private int _id;
private int _position;
public int Id
{
get { return _id; }
set
{
_id = value;
OnPropertyChanged("Id");
}
}
public int Position
{
get { return _position; }
set
{
_position = value;
OnPropertyChanged("Position");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
My data comes from DAL.DataContext using Entity Framework with CodeFirst. Here I´m using Source class.
using System.Data.Entity;
using DOMAIN;
namespace DAL
{
public class DataContext : DbContext
{
public DbSet<Source> Sources { get; set; }
}
}
Mapping is in BL.MyAppLogic.cs . In this class I have property Items which is ObservableCollection.
After puting another item into DB for Source class collection get refresh but for Destination is not refreshing.
using System.Collections.ObjectModel;
using System.Data.Entity;
using System.Linq;
using AutoMapper;
using DAL;
using DOMAIN;
using ENITITIES;
namespace BL
{
public class MyAppLogic
{
private readonly DataContext _dataContext = new DataContext();
public ObservableCollection<Source> Items { get; set; }
//public ObservableCollection<Destination> Items { get; set; }
public MyAppLogic()
{
Database.SetInitializer(new MyInitializer());
Mapping();
_dataContext.Sources.Load();
Items = _dataContext.Sources.Local;
//Items = Mapper.Map<ObservableCollection<Source>, ObservableCollection<Destination>>(_dataContext.Sources.Local);
}
private void Mapping()
{
Mapper.CreateMap<Source, Destination>().ReverseMap();
// I tried also Mapper.CreateMap<ObservableCollection<Source>, ObservableCollection<Destination>>().ReverseMap();
}
public int GetLastItem()
{
return _dataContext.Database.SqlQuery<int>("select Position from Sources").ToList().LastOrDefault();
}
public void AddNewItem(Destination newItem)
{
_dataContext.Sources.Add(Mapper.Map<Destination, Source>(newItem));
_dataContext.SaveChanges();
}
}
}
My problem is not with mapping, that’s works good, but with refreshing collection after adding or removing items from db. If I use DOMAIN.Source class everything works, collection is refreshing. But when I’m using ENTITIES.Destination data comes from DB and also I can put som new data to DB but refresing ObservableCollection is not working.
Please try to comment lines(14 & 23) in BL.MyAppLogic.cs and uncomment(15 & 24) and you’ll see what I mean.
Thank you for any help.
I got it but I don´t know if is correct.
Local has CollectionChanged event
so in constructor I put these lines
public MyAppLogic()
{
Database.SetInitializer(new MyInitializer());
Mapping();
_dataContext.Sources.Load();
_dataContext.Sources.Local.CollectionChanged += SourcesCollectionChanged;
Items = Mapper.Map<ObservableCollection<Source>, ObservableCollection<Destination>>(_dataContext.Sources.Local);
}
and handler looks
private void SourcesCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
var source = sender as ObservableCollection<Source>;
Mapper.Map(source, Items);
}
Now is my collection automating refreshing when I put something to DB in my UI.
Looks like automapper don´t put reference into Items, but create new instance.

Cannot access my properties of a class in sharepoint

I have a problem with my Sharepoint application. I started programming it as a normal project and now I like to “move” it into a Sharepoint application. My app hast two projects one is the actual project for sharepoint and one contains the classes. I signed my classes-project, it has a strong name and is referenced. So far so good… But now I have the problem that I cannot access the public methods within my class anymore. So when instantiate my class:
var myName = new ClassName();
and later try to access it, for example:
myName.Size = 20;
“Size” will be marked red with the error “cannot resolve symbol”. But before putting it all into a sharepoint app it worked just fine! I googeld up and down but couldn’t find a solution for this problem.
Does someone know this problem?
UPDATE:
This is my code in the main project:
protected override void CreateChildControls()
{
...
HPlaner hp = new HPlaner();
hp.Entries.Add(new HPlanerEntry("Micky Mouse", new DateTime(2012, 12, 24), new DateTime(2013, 1, 13)));
hp.Scale = 2;
hp.Year = year;
hp.Summary = true;
this.Controls.Add(hp);
}
and this is the class:
public class HPlaner : WebControl
{
private List<HPlanerEntry> _Entries;
public List<HPlanerEntry> Entries
{
get
{
if (_Entries == null)
{
_Entries = new List<HPlanerEntry>();
}
return _Entries;
}
}
public bool Summary { get; set; }
public int Scale { get; set; }
public int DayCount { get; set; }
public int UserCount { get; set; }
public int Year { get; set; }
public string[,] UserList { get; set; }
... (myMethods)
}
My guess is that your code is written targeting x86-architecture while the SharePoint-code target 64-bit. You can change the setting of your code by right-clicking your project in VS and select Properties->Build->Platform Target. Select x64 and recompile.
Sorry guys, the answer was simple! The Main-Class in the normal project was _Default and in the Sharpoint Application HPlaner, which is the same name as my other class. You could not see this, since the class-name was not inluced in my listing. How timeconsuming can "little" things like that be!!

Resources