【问题标题】:How to convert string to Color in SharePoint Client Object model for silverlight?如何在 Silverlight 的 SharePoint 客户端对象模型中将字符串转换为颜色?
【发布时间】:2012-05-08 12:39:09
【问题描述】:

我需要将具有颜色名称(例如“红色”)的字符串转换为 System.Windows.Media.Color 的对象。

我正在使用以下代码:

using System.ComponentModel;

TypeConverter tc = new TypeConverter();
Color bgColor = (Color)(tc.ConvertFrom((li["Background_x0020_Color"].ToString())));

代码构建成功,但抛出运行时异常:“ConvertFrom not implemented in base TypeConverter.”

非常感谢任何帮助。谢谢!

【问题讨论】:

    标签: sharepoint typeconverter sharepoint-clientobject system.componentmodel system.windows.media


    【解决方案1】:

    试试这个

    Color c;
    Type colorType = (typeof(System.Windows.Media.Colors));
    if (colorType.GetProperty(slist.color) != null)
    {
        object o = colorType.InvokeMember("Red", BindingFlags.GetProperty, null, null, null);
        if (o != null)
        {
            c = (Color)o;
        }
        else
        {
            c = Colors.Black;
        }
    }
    else
    {
        c = Colors.Black;
    }
    Brush color = new SolidColorBrush(c);
    

    http://jyothsnag.blogspot.in/2011/04/convert-string-to-color-object-in.html

    【讨论】:

    • 完美!像魅力一样工作。非常感谢!
    【解决方案2】:

    错误意味着 TypeConverter 级别太低而无法执行此操作,它甚至在 ConvertFrom 方法中都没有代码(称为实现),请使用System.Web.UI.WebControls.WebColorConverter

    【讨论】:

    • 你说得对,我看了标签,忽略了标题的结尾
    猜你喜欢
    • 1970-01-01
    • 2016-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多