2017-12-18 19 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"); 
}