如何刷新PropertyGrid的本地化属性 [英] How to refresh localized attributes in PropertyGrid

查看:895
本文介绍了如何刷新PropertyGrid的本地化属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题,我的本地化属性,如:

I have problem with my localized attributes such as:

public class LocalizedDisplayNameAttribute : DisplayNameAttribute
{
    public LocalizedDisplayNameAttribute(string resourceId)
        : base(GetMessageFromResource(resourceId))
    { }

    private static string GetMessageFromResource(string resourceId)
    {
        var propertyInfo = typeof(Lockit).GetProperty(resourceId, BindingFlags.Static | BindingFlags.Public);
        return (string)propertyInfo.GetValue(null, null);
    }
}

当我使用的属性与此属性它在PropertyGrid中局部的,但是当我改变当前CultureInfo它不会刷新,即使我再创建此PropertyGrid中。我已经尝试通过手动调用此属性:

When I'm using properties with this attribute it's localized in PropertyGrid, but when I change the current CultureInfo it doesn't refresh, even if I create this PropertyGrid again. I've try to manually call this attribute by:

foreach (PropertyInfo propertyInfo in myPropertiesInfoTab)
{
    object[] custom_attributes = propertyInfo.GetCustomAttributes(false);
}

属性构造函数被调用,而新创建的PropertyGrid中仍然有旧文化的显示名称(总是像第一次创建相同的值)的属性。

The property constructor is called, but the newly created PropertyGrid still has the property for the old culture display name (always the same value as first created).

当我重新启动应用程序,但我不希望这样做它的工作原理。有没有什么解决办法吗?

It works when I restarting application, but I don't want to do this. Is there any solution?

推荐答案

我们可以在一个简单但完整的例子复制本(​​即简单地增加了柜台上的名字,重新present每因为它发生的翻译):

We can reproduce this in a simple but complete example (that simply adds a counter onto the name, to represent each translation as it happens):

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Show();
    Show();
}
static void Show()
{
    using(var grid = new PropertyGrid
        {Dock = DockStyle.Fill, SelectedObject = new Foo { Bar = "def"} })
    using(var form = new Form { Controls = { grid }})
    {
        form.ShowDialog();
    }
}

class Foo
{
    [CheekyDisplayName("abc")]
    public string Bar { get; set; }
}
public class CheekyDisplayNameAttribute : DisplayNameAttribute
{
    public CheekyDisplayNameAttribute(string resourceId)
    : base(GetMessageFromResource(resourceId))
    { }
    private static string GetMessageFromResource(string resourceId)
    {
        return resourceId + Interlocked.Increment(ref counter);
    }

    private static int counter;
}

这证明了的属性的被缓存调用之间。也许是解决这一问题的最简单方法是翻译推迟到显示名称查询时间:

This demonstrates that the attribute is being cached between calls. Perhaps the easiest way to fix this is to delay the translation to the time that DisplayName is queried:

public class CheekyDisplayNameAttribute : DisplayNameAttribute
{
    public CheekyDisplayNameAttribute(string resourceId)
        : base(resourceId)
    { }
    private static string GetMessageFromResource(string resourceId)
    {
        return resourceId + Interlocked.Increment(ref counter);
    }
    public override string DisplayName
    {
        get { return GetMessageFromResource(base.DisplayName); }
    }
    private static int counter;
}

但是,请注意,这是可以调用次数的地段的(36%显示);您的 的可能想用它缓存文化沿着缓存值:

However, note that this can be called lots of times (36 per showing); you might want to cache the value along with the culture it was cached for:

    private CultureInfo cachedCulture;
    private string cachedDisplayName;
    public override string DisplayName
    {
        get
        {
            var culture = CultureInfo.CurrentCulture;
            if (culture != cachedCulture)
            {
                cachedDisplayName = GetMessageFromResource(base.DisplayName);
                cachedCulture = culture;
            }
            return cachedDisplayName;
        }
    }

这篇关于如何刷新PropertyGrid的本地化属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆