Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 扩展方法在调用类中不可用_C#_Xamarin_Xamarin.ios_Xamarin.forms_Extension Methods - Fatal编程技术网

C# 扩展方法在调用类中不可用

C# 扩展方法在调用类中不可用,c#,xamarin,xamarin.ios,xamarin.forms,extension-methods,C#,Xamarin,Xamarin.ios,Xamarin.forms,Extension Methods,我试图引用一个类中的扩展方法,但得到以下错误:“错误:UIColor不包含'FromHex'的定义。”。我的设置如下。我已经在错误产生的行首添加了一个箭头(箭头不是代码的一部分) 我有一个警告类: using System; using UIKit; using System.Linq; using MyApp.iOS; [assembly: Xamarin.Forms.Dependency(typeof(MyApp.iOS.Alert))] namespace MyApp.iOS {

我试图引用一个类中的扩展方法,但得到以下错误:“错误:UIColor不包含'FromHex'的定义。”。我的设置如下。我已经在错误产生的行首添加了一个箭头(箭头不是代码的一部分)

我有一个警告类:

using System;
using UIKit;
using System.Linq;
using MyApp.iOS;

[assembly: Xamarin.Forms.Dependency(typeof(MyApp.iOS.Alert))]
namespace MyApp.iOS {

    public class Alert : IAlert {
        public void ShowAlert(string title, string message, string button) {
            ShowAlert(title, message, button, null);
        }

        public void ShowAlert(string title, string message, string button, string hexColor) {
            var okAlertController = UIAlertController.Create(title, message, UIAlertControllerStyle.Alert);
            okAlertController.AddAction(UIAlertAction.Create(button, UIAlertActionStyle.Default, null));

            if (hexColor != null) {
                UIView firstSubView = okAlertController.View.Subviews.First();
                UIView alertContentView = firstSubView.Subviews.First();
                foreach (UIView subSubView in alertContentView.Subviews) {
                    --> subSubView.BackgroundColor = UIColor.FromHex(hexColor); // This line is where I'm getting the error
                }

                okAlertController.View.TintColor = UIColor.Black;
            }

            var window = UIApplication.SharedApplication.KeyWindow;
            var viewController = window.RootViewController;
            while (viewController.PresentedViewController != null) viewController = viewController.PresentedViewController;

            var navigationController = viewController as UINavigationController;
            if (navigationController != null) viewController = navigationController.ViewControllers.Last();

            viewController.PresentViewController(okAlertController, true, null);
        }
    }

}
我有一个UIColorExtensions类:

using System;
using System.Globalization;
using UIKit;

namespace MyApp.iOS {

    public static class UIColorExtensions {
        public static UIColor FromHex(this UIColor color, string hex) {
            float a = 255, r = 0, g = 0, b = 0;

            if (hex.StartsWith("#")) {
                string hexColor = hex.Substring(1);

                if (hexColor.Length == 8) {
                    float.TryParse(hexColor.Substring(0, 2), NumberStyles.HexNumber, CultureInfo.CurrentCulture, out a);
                    hexColor = hexColor.Substring(2, 6);
                }

                if (hexColor.Length == 6) {
                    float.TryParse(hexColor.Substring(2, 2), NumberStyles.HexNumber, CultureInfo.CurrentCulture, out r);
                    float.TryParse(hexColor.Substring(4, 2), NumberStyles.HexNumber, CultureInfo.CurrentCulture, out g);
                    float.TryParse(hexColor.Substring(6, 2), NumberStyles.HexNumber, CultureInfo.CurrentCulture, out b);
                }

                return UIColor.FromRGBA(r, g, b, a);
            }

            return null;
        }
    }

}
我不知道我做错了什么

以下是我迄今为止所尝试的:

  • 重新启动IDE
  • 清理、重建
  • 在我的Alert类中除去命名空间行上方的装配行

  • 问题是,
    UIColor
    不是该类的实例,因此必须执行类似于
    UIColor.Red.FromHex(hex)
    的操作,或者修改扩展方法以用另一种方式处理它