扩展方法在调用中不可用 class
Extension method not available in calling class
我正在尝试引用一个 class 的扩展方法,但出现以下错误:"Error: UIColor does not contain a definition for 'FromHex'"。我的设置如下。我在我的错误来源行的开头添加了一个箭头(箭头不是我的代码的一部分)。
我有一个警报 class:
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 class:
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
- 清理、重建
- 删除我的警报中命名空间行上方的装配线 class
问题是 UIColor
不是 class 的实例,因此必须执行类似 UIColor.Red.FromHex(hex)
的操作或修改您的扩展方法以另一种方式处理它。
我正在尝试引用一个 class 的扩展方法,但出现以下错误:"Error: UIColor does not contain a definition for 'FromHex'"。我的设置如下。我在我的错误来源行的开头添加了一个箭头(箭头不是我的代码的一部分)。
我有一个警报 class:
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 class:
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
- 清理、重建
- 删除我的警报中命名空间行上方的装配线 class
问题是 UIColor
不是 class 的实例,因此必须执行类似 UIColor.Red.FromHex(hex)
的操作或修改您的扩展方法以另一种方式处理它。