C# 在MonoTouch中使用带@selector的PerformSelector

C# 在MonoTouch中使用带@selector的PerformSelector,c#,ios,xamarin.ios,performselector,C#,Ios,Xamarin.ios,Performselector,我正在尝试将以下iOS代码转换为MonoTouch,但无法确定@selector(removebar)代码的正确转换。有人能提供关于处理@selector的最佳方法的指导吗(因为我在其他地方也遇到过): -(无效)键盘将显示:(NSNotification*)注释{ [自执行选择器:@selector(removeBar),对象:nil afterDelay:0]; } 我的C#代码是: 我基本上是想隐藏键盘上显示的上一个/下一个按钮 提前感谢您的帮助。NSNotificationCenter

我正在尝试将以下iOS代码转换为MonoTouch,但无法确定@selector(removebar)代码的正确转换。有人能提供关于处理@selector的最佳方法的指导吗(因为我在其他地方也遇到过):

-(无效)键盘将显示:(NSNotification*)注释{
[自执行选择器:@selector(removeBar),对象:nil afterDelay:0];
}
我的C#代码是:

我基本上是想隐藏键盘上显示的上一个/下一个按钮

提前感谢您的帮助。

NSNotificationCenter.DefaultCenter.AddObserver(uikebox.WillShowNotification,removeBar);
其中
removeBar
是在别处定义的方法

void removeBar(NSNotification通知)
{
//在这里你想干什么就干什么
}
或者,如果您喜欢使用lambda:

NSNotificationCenter.DefaultCenter.AddObserver(uikebox.WillShowNotification,
通知=>{
/*在这里做你的事情*/
});
NSNotificationCenter.DefaultCenter.AddObserver(uikebox.WillShowNotification,removeBar);
其中
removeBar
是在别处定义的方法

void removeBar(NSNotification通知)
{
//在这里你想干什么就干什么
}
或者,如果您喜欢使用lambda:

NSNotificationCenter.DefaultCenter.AddObserver(uikebox.WillShowNotification,
通知=>{
/*在这里做你的事情*/
});

您必须考虑到:

[self performSelector:@selector(removeBar) withObject:nil afterDelay:0];
完全一样

[self removeBar];
performSelector
的调用只是使用反射的方法调用。因此,您真正需要翻译成C的是以下代码:

- (void)keyboardWillShow:(NSNotification *)note {
    [self removeBar];
}
protected virtual void RegisterForKeyboardNotifications()
{
    NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillHideNotification, OnKeyboardNotification);
    NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillShowNotification, OnKeyboardNotification);
}

private void OnKeyboardNotification (NSNotification notification)
{
    var keyboardVisible = notification.Name == UIKeyboard.WillShowNotification;

    if (keyboardVisible) 
    {
        // Hide the bar
    }
    else
    {
        // Show the bar again
    }
}
我猜这也是通知订阅,它总结为以下代码:

- (void)keyboardWillShow:(NSNotification *)note {
    [self removeBar];
}
protected virtual void RegisterForKeyboardNotifications()
{
    NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillHideNotification, OnKeyboardNotification);
    NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillShowNotification, OnKeyboardNotification);
}

private void OnKeyboardNotification (NSNotification notification)
{
    var keyboardVisible = notification.Name == UIKeyboard.WillShowNotification;

    if (keyboardVisible) 
    {
        // Hide the bar
    }
    else
    {
        // Show the bar again
    }
}
您通常希望在
ViewDidLoad
上调用
RegisterWorkeryBoardNotifications


干杯

您必须考虑到:

[self performSelector:@selector(removeBar) withObject:nil afterDelay:0];
完全一样

[self removeBar];
performSelector
的调用只是使用反射的方法调用。因此,您真正需要翻译成C的是以下代码:

- (void)keyboardWillShow:(NSNotification *)note {
    [self removeBar];
}
protected virtual void RegisterForKeyboardNotifications()
{
    NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillHideNotification, OnKeyboardNotification);
    NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillShowNotification, OnKeyboardNotification);
}

private void OnKeyboardNotification (NSNotification notification)
{
    var keyboardVisible = notification.Name == UIKeyboard.WillShowNotification;

    if (keyboardVisible) 
    {
        // Hide the bar
    }
    else
    {
        // Show the bar again
    }
}
我猜这也是通知订阅,它总结为以下代码:

- (void)keyboardWillShow:(NSNotification *)note {
    [self removeBar];
}
protected virtual void RegisterForKeyboardNotifications()
{
    NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillHideNotification, OnKeyboardNotification);
    NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillShowNotification, OnKeyboardNotification);
}

private void OnKeyboardNotification (NSNotification notification)
{
    var keyboardVisible = notification.Name == UIKeyboard.WillShowNotification;

    if (keyboardVisible) 
    {
        // Hide the bar
    }
    else
    {
        // Show the bar again
    }
}
您通常希望在
ViewDidLoad
上调用
RegisterWorkeryBoardNotifications


干杯

Stephane展示了一种方法,您可以使用我们改进的绑定来转换它

让我分享一个更好的。您需要的是键盘通知,我们方便地为其提供强大的类型,这将使您的生活更加轻松:


它包含一个完整的示例,向您展示了如何访问为您的通知提供的强类型数据。

Stephane展示了一种可以使用改进的绑定进行转换的方法

让我分享一个更好的。您需要的是键盘通知,我们方便地为其提供强大的类型,这将使您的生活更加轻松:

它包含一个完整的示例,演示如何访问为通知提供的强类型数据