简体   繁体   English

如何将ResourceDictionary字符串指定为switch case Constant Expression?

[英]How to assign the ResourceDictionary string as switch case Constant Expression?

switch ("Case2")
        {
            case (string)Application.Current.FindResource("Case1");
                //Do Some logic
                break;
            case (string)Application.Current.FindResource("Case2");
                //Do Some logic
                break;
            case (string)Application.Current.FindResource("Case3");
                //Do Some logic
                break;
            default:
                break;
        }

I did this code But it is not work. 我做了这个代码但它不起作用。 Now i want to assign the string value( (string)Application.Current.FindResource("Case1") ) which getting from resource dictionary to constant expression.How is it possible or else is there any way ? 现在我想分配从资源字典到常量表达式的字符串值( (string)Application.Current.FindResource(“Case1”) )。怎么可能,否则有什么办法吗?

It is not possible. 这不可能。 A constant expression is, by definition, a compile -time constant. 根据定义, 常量表达式编译时常量。 A resource dictionary lookup must happen at run time. 资源字典查找必须在运行时进行。 The usual solution is to use a string of if statements: 通常的解决方案是使用一串if语句:

if ("Case2" == (string)Application.Current.FindResource("Case1"))
{
    //Do some logic
}
else if ("Case2" == (string)Application.Current.FindResource("Case2"))
{
    //Do some logic
}
else if ("Case2" == (string)Application.Current.FindResource("Case3"))
{
    //Do some logic
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM