如何在自定义服务器控件ASP中使用.resx和.resource文件? [英] How to use .resx and .resource files in custom server control asp?

查看:121
本文介绍了如何在自定义服务器控件ASP中使用.resx和.resource文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写自己的服务器端控件,并且正在使用存储在 .resx 文件中的图像.在控制台应用程序中,此代码可以正常工作:

I am writing my own server side control and I am using images that are being stored in a .resx file. In the console application this code works fine:

     ResXResourceReader rsxr = new ResXResourceReader("Resource1.resx");
        
     foreach (DictionaryEntry d in rsxr)
     {
        Console.WriteLine(d.Key.ToString() + ":\t" + d.Value.ToString());
     }

     rsxr.Close();

但是在这里

      protected override void RenderContents(HtmlTextWriter output)
    {
        ResXResourceReader rsxr = new ResXResourceReader("Resource1.resx");
  
        base.RenderContents(output);

        foreach (DictionaryEntry d in rsxr)
        {
            output.Write(d.Key.ToString());
        }
        
    }

我收到此错误:

找不到文件'C:\ Program Files \ Common Files \ Microsoft Shared \ DevServer \ 10.0 \ Resource1.resx'

Could not find file 'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\Resource1.resx'

我尝试使用ResourceManager,但是它需要一个 .resource 文件.我无法访问 resgen 工具(命令提示符不理解 resgen 命令)并安装它(在尝试中发生了一些错误).

I tried to use the ResourceManager, but it requires a .resource file. I can't access the resgen tool (command prompt does not understand the resgen command) and install it (during the attempt some errors ocured).

我的问题是:

  1. 为什么我不能阅读 .resx ?

如何正确安装 resgen 工具?

谢谢.

推荐答案

根据您的描述,我了解您需要找到并访问用户控件的资源文件.我发现它可以通过以下方式很好地工作:

From your description, I understand you need to locate and access the user control's resource file. I found that it works nicely the following way:

  1. 在项目级别上创建一个 App_GlobalResources (通过上下文菜单 Add-> Add ASP.NET Folder-> App_GlobalResources )

使用与控件相同的名称创建资源文件,但在 App_GlobalResources 中.例如,如果控件名为 myControl.ascx ,则默认语言的资源文件名称必须为 myControl.ascx.resx

Create the ressource file with the same name as the control, but inside the App_GlobalResources. For example, if the control is named myControl.ascx, then the ressource file's name for the default language has to be myControl.ascx.resx

为所需的每种语言创建其他资源文件.例如,如果您需要德语(" de-DE "),则添加myControl.ascx.de.resx

Create additional ressource files for each language you require. For instance, if you need German ("de-DE"), then add myControl.ascx.de.resx

添加类 MultiLanguageUserControl 如下:

public class MultiLanguageUserControl : System.Web.UI.UserControl
{
    public string getResValue(string id)
    {
        var ctrlPath = TemplateControl.AppRelativeVirtualPath;
        var ctrlFile = System.IO.Path.GetFileName(ctrlPath);
        var resObj = GetGlobalResourceObject(ctrlFile, id);
        if (resObj!=null)
                    return resObj.ToString();
        else
                    return string.Format("UNRESOLVED[{0}]", id);        
    }
}

  • 打开myControl后面的代码,并使其继承自 MultiLanguageUserControl ,而不是继承自 System.Web.UI.UserControl :

    公共子类myControl:MultiLanguageUserControl {//...

    在HTML代码中,使用新功能,例如:<%= getResValue("resid")%> ,其中"resid" 是您要查找的资源字符串的名称.您还可以根据需要使用HTML编码标记<%:代替<%= .另外,您可以在用户控件中服务器端C#代码中的任何位置使用 getResValue 从资源文件中检索值.

    In the HTML code, use the new function, e.g.: <%=getResValue("resid")%>, where "resid" is the name of the ressource string you want to look up. You can also use the HTML-encoding tag <%: instead of <%=, depending on your requirements. Alternatively, you can use getResValue anywhere in your server-sided C# code in your user control to retrieve the value from the ressource file.

    确保在页面的 Page_Load 事件中支持语言检测,该事件使用用户控件.此处(请参见函数 InitializeCulture ).

    Ensure that you support the language detection in the Page_Load event of the page, which uses the user control. How you can do this is described here (look for the function InitializeCulture).

    注意::如果您想从用户控件内部读取页面的本地资源字符串,请查看这里.

    NOTE: If you want to read the page's local resource strings from inside the user control then take a look here.

    这篇关于如何在自定义服务器控件ASP中使用.resx和.resource文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆