2017-12-18 9 views
-2

xamarin.from에서 문자열을 색상으로 변환하는 방법 Color.fromName 메서드가 없습니다? Xamarin.Forms github에 ColorUnitTests.cs에서 테스트 TestColorTypeConverter에서 문자열 값으로 ColorTypeConverter를 사용String to Color Xamarin.Form

string colorStr = "Blue"; 
BoxView objBoxView = new BoxView 
{ 
    HeightRequest = double.Parse(HeightRequest), 
    HorizontalOptions = LayoutOptions.Fill, 
    VerticalOptions = LayoutOptions.End, 
    BackgroundColor = colorStr 
}; 
+0

'의 backgroundColor = Color.Blue' 내가, 내가 원하는 것을 알고 그것을 – heinst

+0

Maaan을 할 것입니다 "blue"가 json 파일에서 왔기 때문에 Color로 변환 할 문자열이지만 간단하게 "blue"라고 썼습니다! – manDig

+1

ColorTypeConverter ->이 클래스를 살펴 봐야합니다. – Dilmah

답변

0

몇 가지 예 :

var input = new[] 
{ 
    "blue", "Blue", "Color.Blue",  // by name 
    "#0000ff", "#00f",    // by hex code 
    "#a00f",       // by hex code with alpha 
    "rgb(0,0, 255)", "rgb(0,0, 300)", // by RGB 
    "rgba(0%,0%, 100%, .8)",   // by RGB percent with alpha 
    "hsl(240,100%, 50%)",    // by HSL 
    "hsla(240,100%, 50%, .8)",  // by HSL with alpha 
    "Accent",       // by Accent color 
    "Default", "#12345"    // not a valid color 
}; 

ColorTypeConverter converter = new ColorTypeConverter(); 

foreach (var str in input) 
{ 
    Color color = (Color)(converter.ConvertFromInvariantString(str)); 
    Debug.WriteLine("{0} is {1} Color", str, color.IsDefault ? "not a" : "a"); 
}