C# 事件处理程序触发但不执行UI代码(MonoTouch)
我有一个uivewcontroller,它在视图加载时具有事件处理程序。它包含在后台触发的代码以及UI,因此对于UI代码,我使用InvokeOnMainThread。它工作正常,直到我导航到另一个控制器并返回到它。当事件激发时,它不会执行UI代码。每次我按下这个控制器,我都在创建它的一个新实例。所以我试着让它只是这个控制器的一个实例,它工作得很好!!!!有人能解释一下为什么会发生这种事吗C# 事件处理程序触发但不执行UI代码(MonoTouch),c#,xamarin.ios,C#,Xamarin.ios,我有一个uivewcontroller,它在视图加载时具有事件处理程序。它包含在后台触发的代码以及UI,因此对于UI代码,我使用InvokeOnMainThread。它工作正常,直到我导航到另一个控制器并返回到它。当事件激发时,它不会执行UI代码。每次我按下这个控制器,我都在创建它的一个新实例。所以我试着让它只是这个控制器的一个实例,它工作得很好!!!!有人能解释一下为什么会发生这种事吗 public override void ViewDidLoad () {
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
if (hubConnection == null) {
hubConnection = new HubConnection ("http://" + JsonRequest.IP + ":8070/", "userId=" + userId);
hubProxy = hubConnection.CreateHubProxy ("myChatHub");
hubConnection.EnsureReconnecting ();
//}
if (hubConnection.State == ConnectionState.Disconnected) {
hubConnection.Start ();
}
hubConnection.Received += HandleReceived;
}
}
void HandleReceived (string obj)
{
InvokeOnMainThread (delegate {
discussion.Root [0].Add (new ChatBubble (true, text));
});
}
首先,这里不需要使用InvokeOnMainThread,因为TouchUpInside保证在主线程上触发
第二个问题是,sendButton字段是静态的,但控制器实例不是静态的。这就是为什么它只被添加到控制器的第一个实例中。删除static关键字,它应该可以工作。首先,这里不需要使用InvokeOnMainThread,因为toucupinside保证在主线程上触发
第二个问题是,sendButton字段是静态的,但控制器实例不是静态的。这就是为什么它只被添加到控制器的第一个实例中。删除static关键字,它应该可以工作。首先,这里不需要使用InvokeOnMainThread,因为toucupinside保证在主线程上触发
第二个问题是,sendButton字段是静态的,但控制器实例不是静态的。这就是为什么它只被添加到控制器的第一个实例中。删除static关键字,它应该可以工作。首先,这里不需要使用InvokeOnMainThread,因为toucupinside保证在主线程上触发
第二个问题是,sendButton字段是静态的,但控制器实例不是静态的。这就是为什么它只被添加到控制器的第一个实例中。删除static关键字,它应该可以工作。您应该,几乎永远不要使用
静态
UI组件,这几乎总是会导致问题。任何类型的UI构建通常在LoadView
方法中完成,任何类型的事件连接/视图设置都应该在ViewDidLoad
中完成,例如
public class TestController : UITableViewController
{
private UIButton sendButton;
...
public override void LoadView()
{
base.LoadView();
if (sendButton == null)
{
sendButton = new UIButton (UIButtonType.RoundedRect)
{
Frame = new RectangleF (100, 100, 80, 50),
BackgroundColor = UIColor.Blue
};
View.AddSubview(sendButton);
}
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
sendButton.TouchUpInside += HandleTouchUpInside;
}
public override void ViewDidUnload()
{
if (sendButton != null)
{
sendButton.Dispose();
sendButton = null;
}
}
}
几点注意:
/ViewDidLoad
这样您就不需要再做这种事情了,建议您将清理代码放在ViewDidUnload
方法中DidReceiveMemoryWarning
- 您的代码已经在主循环中运行-
是不必要的InvokeOnMainThread
静态
UI组件,这几乎总是会导致问题。任何类型的UI构建通常在LoadView
方法中完成,任何类型的事件连接/视图设置都应该在ViewDidLoad
中完成,例如
public class TestController : UITableViewController
{
private UIButton sendButton;
...
public override void LoadView()
{
base.LoadView();
if (sendButton == null)
{
sendButton = new UIButton (UIButtonType.RoundedRect)
{
Frame = new RectangleF (100, 100, 80, 50),
BackgroundColor = UIColor.Blue
};
View.AddSubview(sendButton);
}
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
sendButton.TouchUpInside += HandleTouchUpInside;
}
public override void ViewDidUnload()
{
if (sendButton != null)
{
sendButton.Dispose();
sendButton = null;
}
}
}
几点注意:
/ViewDidLoad
这样您就不需要再做这种事情了,建议您将清理代码放在ViewDidUnload
方法中DidReceiveMemoryWarning
- 您的代码已经在主循环中运行-
是不必要的InvokeOnMainThread
静态
UI组件,这几乎总是会导致问题。任何类型的UI构建通常在LoadView
方法中完成,任何类型的事件连接/视图设置都应该在ViewDidLoad
中完成,例如
public class TestController : UITableViewController
{
private UIButton sendButton;
...
public override void LoadView()
{
base.LoadView();
if (sendButton == null)
{
sendButton = new UIButton (UIButtonType.RoundedRect)
{
Frame = new RectangleF (100, 100, 80, 50),
BackgroundColor = UIColor.Blue
};
View.AddSubview(sendButton);
}
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
sendButton.TouchUpInside += HandleTouchUpInside;
}
public override void ViewDidUnload()
{
if (sendButton != null)
{
sendButton.Dispose();
sendButton = null;
}
}
}
几点注意:
/ViewDidLoad
这样您就不需要再做这种事情了,建议您将清理代码放在ViewDidUnload
方法中DidReceiveMemoryWarning
- 您的代码已经在主循环中运行-
是不必要的InvokeOnMainThread
静态
UI组件,这几乎总是会导致问题。任何类型的UI构建通常在LoadView
方法中完成,任何类型的事件连接/视图设置都应该在ViewDidLoad
中完成,例如
public class TestController : UITableViewController
{
private UIButton sendButton;
...
public override void LoadView()
{
base.LoadView();
if (sendButton == null)
{
sendButton = new UIButton (UIButtonType.RoundedRect)
{
Frame = new RectangleF (100, 100, 80, 50),
BackgroundColor = UIColor.Blue
};
View.AddSubview(sendButton);
}
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
sendButton.TouchUpInside += HandleTouchUpInside;
}
public override void ViewDidUnload()
{
if (sendButton != null)
{
sendButton.Dispose();
sendButton = null;
}
}
}
几点注意:
/ViewDidLoad
这样您就不需要再做这种事情了,建议您将清理代码放在ViewDidUnload
方法中DidReceiveMemoryWarning
- 您的代码已经在主循环中运行-
是不必要的InvokeOnMainThread