需要解决方法来覆盖 RoutedUICommand.Text 属性

发布于 2024-12-28 19:06:08 字数 1060 浏览 0 评论 0原文

我有一个像这样的静态命令类(但有更多命令):

class GuiCommands
{
    static GuiCommands()
    {
        addInterface = new RoutedUICommand(DictTable.getInst().getText("gui.addInterface"), "addInterface", typeof(GuiCommands));
        removeInterface = new RoutedUICommand(DictTable.getInst().getText("gui.removeInterface"), "removeInterface", typeof(GuiCommands));
    }

    public static RoutedUICommand addInterface { get; private set; }
    public static RoutedUICommand removeInterface { get; private set; }
}

它应该使用我的字典来获取正确语言的文本,这是行不通的,因为执行静态构造函数时我的字典没有初始化。

我的第一次尝试是创建一个派生自 RoutedUICommand 的新命令类,覆盖 Text 属性并在 get 方法中调用 dict。但 Text 属性不是虚拟的,它调用的 GetText() 方法也不是虚拟的。

我唯一能想到的是在此类中提供一个静态初始化方法来翻译所有字典键。但这不是很干净,恕我直言,因为我必须像这样再次命名每个命令

addInterface.Text = DictTable.getInst().getText(addInterface.Text);

,如果我忘记命名一个,不会有错误,只是没有翻译。 我什至不喜欢这样的做法:我必须在此类中两次命名该命令,并在 XAML 命令绑定中再次命名一次。

您有什么想法可以更优雅地解决这个问题吗?

我非常喜欢 RoutedUICommands,但像这样它们对我来说毫无用处。为什么微软不能更频繁地添加“虚拟”这个小词? (或者像JAVA一样将其设置为默认?!)

I have a static Command class like this (but with many more commands):

class GuiCommands
{
    static GuiCommands()
    {
        addInterface = new RoutedUICommand(DictTable.getInst().getText("gui.addInterface"), "addInterface", typeof(GuiCommands));
        removeInterface = new RoutedUICommand(DictTable.getInst().getText("gui.removeInterface"), "removeInterface", typeof(GuiCommands));
    }

    public static RoutedUICommand addInterface { get; private set; }
    public static RoutedUICommand removeInterface { get; private set; }
}

It should use my dictionary to get the texts in the right language, which doesn't work, because my dictionary isn't initialized when the static constructor is executed.

My first attempt was to create a new command-class which derives from RoutedUICommand, override the Text property and call the dict in the get method. But the Text property isn't virtual and neither is the GetText()-Method it calls.

The only thing i can think of is provide a static initialize method in this class that translates all the dict-keys. But this is not very clean IMHO because i have to name every command once again like this

addInterface.Text = DictTable.getInst().getText(addInterface.Text);

and if i forget to name one, there won't be an error, just no translation.
I don't even like that i have to name the command twice in this class and once again in the XAML commandbindings.

Do you have any ideas how this can be solved more elegantly?

I like RoutedUICommands much, but like this they're useless to me. Why couldn't Microsoft add the little word 'virtual' a little more often?? (or make it default like JAVA does?!)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云淡风轻 2025-01-04 19:06:08

我找到了一种可以接受的方法,即使用反射自动翻译所有命令。
这样我至少不必将所有命令添加到另一个方法中。
我在初始化字典后立即调用翻译方法。

public static void translate()
{
    // get all public static props
    var properties = typeof(GuiCommands).GetProperties(BindingFlags.Public | BindingFlags.Static);

    // get their uicommands
    var routedUICommands = properties.Select(prop => prop.GetValue(null, null)).OfType<RoutedUICommand>(); // instance = null for static (non-instance) props

    foreach (RoutedUICommand ruic in routedUICommands)
        ruic.Text = DictTable.getInst().getText(ruic.Text);
}

I found an acceptable way by translating all commands automatically using reflection.
This way i at least don't have to add all the commands to another method.
I call the translate-method right after i initialized my dictionary.

public static void translate()
{
    // get all public static props
    var properties = typeof(GuiCommands).GetProperties(BindingFlags.Public | BindingFlags.Static);

    // get their uicommands
    var routedUICommands = properties.Select(prop => prop.GetValue(null, null)).OfType<RoutedUICommand>(); // instance = null for static (non-instance) props

    foreach (RoutedUICommand ruic in routedUICommands)
        ruic.Text = DictTable.getInst().getText(ruic.Text);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文