2017-11-19 16 views
-2

あるクラスから拡張メソッドを参照しようとしていますが、次のエラーが発生しています: "エラー:UIColorに 'FromHex'の定義が含まれていません。私の設定は以下の通りです。私はエラーが発生している行の先頭に矢印を追加しました(矢印は自分のコードの一部ではありません)。拡張メソッドは呼び出し側クラスで利用できません

私は警告クラスがあります:私は私が間違っていることができるものを把握することはできません

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; 
     } 
    } 

} 

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クラスを持っています。ここで

は、私がこれまで試したものです:

  1. はIDE
  2. クリーニングを再起動し、私のアラートクラスの名前空間のライン上の組立ライン退治
  3. を再構築

答えて

0

UIColorはクラスのインスタンスではないので、UIColor.Red.FromHex(hex)のような処理をしたり、別の方法で処理する拡張メソッドを変更する必要があります。

関連する問題