简体   繁体   中英

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 ?

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 ("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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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