2013-07-17 52 views
0

VB.NET for Windows Store應用程序中沒有可用的FromName方法。如何將字符串轉換爲顏色?我想通過傳遞顏色的字符串值來設置對象的fillcolor。Windws Store App - 在VB.NET中將字符串轉換爲顏色

我發現這個解決方法C#

public static Color FromName(string name) 
{ 
    var property = typeof(Colors).GetRuntimeProperty(name); 
    return (Color)property.GetValue(null); 
} 

我似乎無法把它翻譯成對應的VB.NET代碼。

我也嘗試使用上面的代碼編寫Windows運行時組件,但它說GetRunTimeProperty甚至沒有在任何地方定義。

回答

2
Public Function FromName(ColName As String) As SolidColorBrush 
     Dim [property] = System.Reflection.RuntimeReflectionExtensions.GetRuntimeProperty(GetType(Windows.UI.Colors), ColName) 
     Return New SolidColorBrush(DirectCast([property].GetValue(Nothing), Windows.UI.Color)) 
End Function 

而且完成了!

+0

太棒了!沒有比解決自己的問題更有價值的了。 –

+0

是的(:對象瀏覽器來搶救:P我搜索了GetRuntimeProperty,這是我的希望來生活的地方。 –

0

這不行嗎?

Public Shared Function FromName(name As String) As Color 
    Dim [property] = GetType(Colors).GetRuntimeProperty(name) 
    Return DirectCast([property].GetValue(Nothing), Color) 
End Function 
+0

我已經嘗試過bot翻譯Carl。不,它不起作用。 –

+0

它說GetRuntimeProperty不是System.Type的成員 –

+0

非常奇怪[RuntimeReflectionExtensions.GetRuntimeProperty方法]的MSDN文檔(http://msdn.microsoft.com/zh-cn/library/system.reflection.runtimereflectionextensions.getruntimeproperty。 aspx)表示它在Windows 8中受支持。 –