ContentView中的按钮会导致MonoTouch运行时崩溃。 Monotouch 4.0中的错误? [英] Button in ContentView causes crash in MonoTouch runtime. Bug in Monotouch 4.0?

查看:138
本文介绍了ContentView中的按钮会导致MonoTouch运行时崩溃。 Monotouch 4.0中的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序在MT 3.0中运行良好。现在我升级了。当按钮在ContentView中时,我看到错误。单击按钮时发生崩溃。代码:

My application worked fine in MT 3.0. Now when I upgraded. I am seeing errors when a button is in a ContentView. Crashes happen when the button is clicked. Code:

public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPa
    float width = tableView.Bounds.Width - 70;

    var cell = tableView.DequeueReusableCell(kCellIdentifier);
    //if (cell == null)
    //{
    cell = new UITableViewCell(UITableViewCellStyle.Subtitle, kCellIdentifier);
    // }

    var behavior = tvc.behaviors.ElementAt(indexPath.Row);
    cell.TextLabel.Text = behavior.Name;
    cell.TextLabel.Font = UIFont.BoldSystemFontOfSize(22f);
    cell.DetailTextLabel.Text = behavior.Definition;
    var view = UIButton.FromType(UIButtonType.Custom);
    view.Tag = indexPath.Row;
    view.SetImage(UIImage.FromBundle("Images/plus.png"), UIControlState.Normal);
    view.Frame = new RectangleF(width - 50, 10, 50, 50);

    view.TouchUpInside += IncrementBehavior;

    var label = new UILabel(new RectangleF(width - 80, 10, 50, 50));
    label.Text = behavior.CurrentCount.ToString();
    label.BackgroundColor = UIColor.Clear;
    label.Font = UIFont.BoldSystemFontOfSize(24f);
    cell.ContentView.AddSubview(view);
    cell.ContentView.AddSubview(label);
    //cell.BackgroundColor = UIColor.Clear;)

    return cell;
}

void IncrementBehavior(object sender, EventArgs e) {
    var button = (UIButton)sender;
    var tag = button.Tag;
    var behavior = tvc.behaviors[tag];

    var indexpath = NSIndexPath.FromRowSection(tag, 0);
    var newBehavior = Repository.GetBehavior(behavior.Id);
    newBehavior.CurrentCount++;
    Repository.Update(newBehavior);
    tvc.behaviors[tag] = newBehavior;


    tvc.TableView.ReloadRows(new[] { indexpath }, UITableViewRowAnimation.None);

}

我可以互换地获得这些错误:

I am getting these Errors interchangeably:

Name: NSInvalidArgumentException Reason: -[__NSCFSet BridgeSelector]: unrecognized selector sent to instance 0x5c3c570

AND

No constructor found for MonoTouch.UIKit.UIControlEventProxy::.ctor(System.IntPtr)


推荐答案

不确定是否这是问题所在,但当我升级到4.0时,我也遇到了一些随机崩溃。事实证明,4.0 GC更具侵略性,而我以前离开的东西不再是犹太人。

Not sure if this is the problem, but when I upgraded to 4.0, I also got some random crashes. It turned out that the 4.0 GC is more aggressive, and that things I was previously getting away with were no longer kosher.

特别是,如果我分配了一个事件处理程序对于一个按钮,我需要确保按钮是在类级别声明的。如果它在方法中被声明为本地,那么GC会在超出范围时清除引用,然后当事件处理程序尝试触发时,它的引用不再存在。

In particular, if I had a event handler assigned to a button, I needed to be sure the button was declared at the class level. If it was declared locally in the method, the GC would purge the reference when it went out of scope, and then later when the event handler tried to fire, it's reference was no longer there.

因此,请尝试在您的方法之外移动按钮的声明。

So try moving the declaration of your button outside of your method.

这篇关于ContentView中的按钮会导致MonoTouch运行时崩溃。 Monotouch 4.0中的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆