如何在switch语句中使用字符串资源

斯沃西46

有没有办法在switch语句中使用字符串资源?尝试以编程方式获取字符串资源后,将返回错误。

错误

'resourceLoader'是变量,但像类型一样使用

C#

public class PageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        var resourceLoader = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView();

        Type page = null;
        switch (value as string)
        {
            case resourceLoader.GetString("SteveJohnson/Text"):
                page = typeof(FirstPage);
                break;
            case resourceLoader.GetString("PeteDavidson/Text"):
                page = typeof(SecondPage);
                break;
            case resourceLoader.GetString("OneDrive/Text"):
                page = typeof(FistPage);
                break;
            case resourceLoader.GetString("Twitter/Text"):
                page = typeof(SecondPage);
                break;
            default:
                break;
        }

        return page;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}
CodeCaster

我认为您需要退后一步,看看为什么要使用switch语句:可能要具有可读的代码块。您可以使用几个if()s轻松地完成相同的操作:

var resourceLoader = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView();

Type page = null;
if (value == resourceLoader.GetString("SteveJohnson/Text"))
    page = typeof(FirstPage);
else if (value == resourceLoader.GetString("PeteDavidson/Text"))
    page = typeof(SecondPage);
else if (value == resourceLoader.GetString("OneDrive/Text"))
    page = typeof(FistPage);
else if (value == resourceLoader.GetString("Twitter/Text"))
    page = typeof(SecondPage);      

return page;

现在您可以使用patternwhen子句将其重写以将其替换为一个开关,但是我认为这不是合适的结构,因为它并没有真正提高可读性。您可以选择带有选项的词典:

var pageTypes = new Dictionary<string, Type>
{
    { resourceLoader.GetString("SteveJohnson/Text"), typeof(FirstPage) },
    { resourceLoader.GetString("PeteDavidson/Text"), typeof(SecondPage) },
    { resourceLoader.GetString("OneDrive/Text"), typeof(FirstPage) },
    { resourceLoader.GetString("Twitter/Text"), typeof(SecondPage) },
};

// Defaults to `null` when not mached
pageTypes.TryGetValue((string)value, out Type page);
return page;

您甚至可以通过初始化一次此字典来缓存资源查找,例如在包含类的构造函数中。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在println语句中使用字符串和变量

将参数解析为bool或仅在switch语句中使用字符串

在Switch语句中使用字符串

使用map <string,int>在switch-case语句中使用字符串

如何在资源文件中使用字符串插值?

如何在MySQL语句中使用.format()字符串?

如何在BigQuery中使用标准SQL在LEFT JOIN的ON子句中使用字符串函数?

我们如何在声明语句中使用字符串变量?

如何在Firebird sql语句中使用非ascii字符串文字?

如何在SQL WHERE IN语句中的R中使用字符串

在C ++中,有没有一种方法可以在switch语句中使用字符串?

在if语句中使用字符串和逻辑运算符

如何在bash脚本中连接字符串以在SQL语句中使用?

如何在数组中使用字符串?

在if语句中使用字符串作为变量子句

在if语句中使用字符串内容作为条件

如何在Android中使用字符串名称调用资源?

如何在MySQL存储过程的IN语句中使用查询字符串

通过从用户获取输入,在switch语句中使用字符串

如何在systemverilog案例语句中使用通配符字符串

jdk 8不允许在switch语句中使用字符串?为什么

如何在bash中使用字符串数组

如何在 Javascript 的 if/else 语句中调用字符串数组

Python Neo4j 在 Cypher 语句中使用字符串变量

Java - 如何在 switch 语句中使用字符数组?

如何在带有 int 的 if 语句中使用字符串?

我如何在 switch case 中使用字符串作为动作?

Python - 在 If 语句中使用字符串作为条件运算符

关于如何在条件语句中使用 Python 添加字符串的问题