如何使用C#中的简单本地方法简化数组的设置

问题描述 投票:0回答:1

我的代码像这样设置图标svg:

    Current.Resources["HomePageIcon5"] = (new[] {
      "resource://Japanese.Resources.5_Light.svg", 
      "resource://Japanese.Resources.5_Gray.svg", 
      "resource://Japanese.Resources.5_Dark.svg" })[thc];

对于更多的图标和相同的字符串也相同

"resource://Japanese.Resources."

出现多次。

有没有一种方法可以创建可以为我做到这一点的本地方法?我正在寻找的是我可以这样称呼的东西:

Current.Resources["HomePageIcon5"] = X("5_Light","5_Gray","5_Dark");
c# .net
1个回答
1
投票

类似:

private static readonly _resourcesPath = "resource://Japanese.Resources.";

public void FillResource(string key, string value, int idx)
{
     var content = new[] 
     {
        _resourcesPath + value + "_Light.svg", 
        _resourcesPath + value + "_Gray.svg", 
        _resourcesPath + value + "_Dark.svg" 
     }
     Current.Resources[key] = content[idx];
}

并像这样使用它:

FillResource("HomePageIcon5", "5", thc);
© www.soinside.com 2019 - 2024. All rights reserved.