Xamarin 绑定类别返回错误:无法在静态类中声明实例成员

Posted

技术标签:

【中文标题】Xamarin 绑定类别返回错误:无法在静态类中声明实例成员【英文标题】:Xamarin binding Category return error: cannot declare instance members in a static class 【发布时间】:2014-03-19 04:40:09 【问题描述】:

我正在尝试将 ReFrostedViewController 绑定到 c#。我使用了 Objective Sharpie 来生成接口。但是当我使用 Xamarin 编译它时,它返回错误。

 /REFrostedViewController_UIViewController.g.cs(10,10): Error CS0708: `REFrostedMenu.REFrostedViewController_UIViewController.__mt_FrostedViewController_var': cannot declare instance members in a static class (CS0708) (REFrostedBinding)

这是我的代码:

[Export ("frostedViewController", ArgumentSemantic.Retain)]
REFrostedViewController FrostedViewController  get; 

我把它改成如下:

[Export ("frostedViewController", ArgumentSemantic.Retain)]
REFrostedViewController FrostedViewController()

它可以编译,但我无法从 UIViewController 中获取它。

REFrostedViewController reFrost = base.FrostedViewController();

返回错误:

/MainViewController.cs(43,43): Error CS0117: `MonoTouch.UIKit.UIViewController' does not contain a definition for `FrostedViewController' (CS0117) (ios)

更新: 这是完整的代码

using System;
using System.Drawing;
using MonoTouch.ObjCRuntime;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace REFrostedMenu

[BaseType (typeof (UIViewController))]
public partial interface REFrostedContainerViewController 

    [Export ("screenshotImage", ArgumentSemantic.Retain)]
    UIImage ScreenshotImage  get; set; 

    [Export ("frostedViewController", ArgumentSemantic.Assign)]
    REFrostedViewController FrostedViewController  get; set; 

    [Export ("animateApperance")]
    bool AnimateApperance  get; set; 

    [Export ("containerView", ArgumentSemantic.Retain)]
    UIView ContainerView  get; 

    [Export ("panGestureRecognized:")]
    void PanGestureRecognized (UIPanGestureRecognizer recognizer);

    [Export ("hide")]
    void Hide ();

    [Export ("resizeToSize:")]
    void ResizeToSize (SizeF size);

//      [Export ("hideWithCompletionHandler:")]
//      void HideWithCompletionHandler(HideCompletionHandler handler);

    [Export ("refreshBackgroundImage")]
    void RefreshBackgroundImage ();


[BaseType (typeof (UIViewController))]
public partial interface REFrostedViewController 

    [Export ("panGestureRecognizer", ArgumentSemantic.Retain)]
    UIPanGestureRecognizer PanGestureRecognizer  get; 

    [Export ("panGestureEnabled")]
    bool PanGestureEnabled  get; set; 

//      [Export ("direction")]
//      REFrostedViewControllerDirection Direction  get; set; 

    [Export ("backgroundFadeAmount")]
    float BackgroundFadeAmount  get; set; 

    [Export ("blurTintColor", ArgumentSemantic.Retain)]
    UIColor BlurTintColor  get; set; 

    [Export ("blurRadius")]
    float BlurRadius  get; set; 

    [Export ("blurSaturationDeltaFactor")]
    float BlurSaturationDeltaFactor  get; set; 

    [Export ("animationDuration")]
    double AnimationDuration  get; set; 

    [Export ("limitMenuViewSize")]
    bool LimitMenuViewSize  get; set; 

    [Export ("menuViewSize", ArgumentSemantic.Assign)]
    SizeF MenuViewSize  get; set; 

    [Export ("liveBlur")]
    bool LiveBlur  get; set; 

//      [Export ("liveBlurBackgroundStyle")]
//      REFrostedViewControllerLiveBackgroundStyle LiveBlurBackgroundStyle  get;    set; 

//      [Export ("delegate", ArgumentSemantic.Assign)]
//      REFrostedViewControllerDelegate Delegate  get; set; 

    [Export ("contentViewController", ArgumentSemantic.Retain)]
    UIViewController ContentViewController  get; set; 

    [Export ("menuViewController", ArgumentSemantic.Retain)]
    UIViewController MenuViewController  get; set; 

    [Export ("initWithContentViewController:menuViewController:")]
    IntPtr Constructor (UIViewController contentViewController, UIViewController menuViewController);

    [Export ("presentMenuViewController")]
    void PresentMenuViewController ();

    [Export ("hideMenuViewController")]
    void HideMenuViewController ();

    [Export ("resizeMenuViewControllerToSize:")]
    void ResizeMenuViewControllerToSize (SizeF size);

//      [Export ("hideMenuViewControllerWithCompletionHandler:")]
//      void HideMenuViewControllerWithCompletionHandler (HideMenuViewControllerCompletionHandler handler);

    [Export ("panGestureRecognized:")]
    void PanGestureRecognized (UIPanGestureRecognizer recognizer);


[BaseType (typeof (NSObject))]
[Model]
public partial interface REFrostedViewControllerDelegate 

    [Export ("frostedViewController:willAnimateRotationToInterfaceOrientation:duration:")]
    void WillAnimateRotationToInterfaceOrientation (REFrostedViewController frostedViewController, UIInterfaceOrientation toInterfaceOrientation, double duration);

    [Export ("frostedViewController:didRecognizePanGesture:")]
    void DidRecognizePanGesture (REFrostedViewController frostedViewController, UIPanGestureRecognizer recognizer);

    [Export ("frostedViewController:willShowMenuViewController:")]
    void WillShowMenuViewController (REFrostedViewController frostedViewController, UIViewController menuViewController);

    [Export ("frostedViewController:didShowMenuViewController:")]
    void DidShowMenuViewController (REFrostedViewController frostedViewController, UIViewController menuViewController);

    [Export ("frostedViewController:willHideMenuViewController:")]
    void WillHideMenuViewController (REFrostedViewController frostedViewController, UIViewController menuViewController);

    [Export ("frostedViewController:didHideMenuViewController:")]
    void DidHideMenuViewController (REFrostedViewController frostedViewController, UIViewController menuViewController);


[BaseType (typeof (UIImage))]
[Category]
public partial interface REFrostedViewController_UIImage 

    [Export ("re_applyBlurWithRadius:tintColor:saturationDeltaFactor:maskImage:")]
    UIImage Re_applyBlurWithRadius (float blurRadius, UIColor tintColor, float saturationDeltaFactor, UIImage maskImage);

    [Export ("re_screenshot")]
    UIImage Re_screenshot ();


[BaseType (typeof (UIViewController))]
[Category]
public partial interface REFrostedViewController_UIViewController 

    [Export ("frostedViewController", ArgumentSemantic.Retain)]
    REFrostedViewController FrostedViewController();

    [Export ("re_displayController:frame:")]
    void Re_displayController (UIViewController controller, RectangleF frame);

    [Export ("re_hideController:")]
    void Re_hideController (UIViewController controller);


请帮助我,我非常感谢!

【问题讨论】:

【参考方案1】:

我在 Github 上创建了一个绑定项目并作为 fork 示例:https://github.com/therealjohn/REFrostedViewController

“不能在静态类中声明实例成员”错误是因为

[Export ("frostedViewController", ArgumentSemantic.Retain)]
REFrostedViewController FrostedViewController  get; 

它试图在一个类别中创建一个属性,这基本上是一种扩展方法。没有“扩展”C# 属性,因此您对方法的更改“修复”了该错误。

我不确定您还有什么其他问题,但我所做的项目应该适合您。

享受吧!

【讨论】:

以上是关于Xamarin 绑定类别返回错误:无法在静态类中声明实例成员的主要内容,如果未能解决你的问题,请参考以下文章

Xamarin - 绑定到 ControlTemplate 中的静态类

在 Xamarin.iOS 中为 2 个依赖静态库创建绑定

Java继承

Xamarin 形成绑定标签

Xamarin 绑定到 objC 支付 api 总是从 api 返回超时

Databinding使用篇二