如何利用人工资源在C#中(没有IDE的行为)? [英] how to use resources manually in C#(no IDE act)?

查看:315
本文介绍了如何利用人工资源在C#中(没有IDE的行为)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习C#和Visual Studio,我想要去更深一点。

我想知道如何手动使用项目资源?不IDE。我搜索的网站,但我刚刚看到这个帅哥张贴如何创建和使用在.NET 资源,但它不是我想要的。

我定义了投影为自己手动设置窗体图标。

我尝试这样做:

  1. 第一,我添加一个文件夹到项目,并更名为资源
  2. 然后,我添加了一个 .ICO 文件并重新命名为图标
  3. 然后我将它定义在 Resources.Designer.cs 像这样

     内部静态System.Drawing.Icon图标{
        得到 {
            obj对象= ResourceManager.GetObject(图标,resourceCulture);
            返程((System.Drawing.Icon)(OBJ));
        }
    }
     

  4. 我添加了图标的形式设计是这样的:

      this.Icon = Properties.Resources.icon;
     

一切都显得精细,而图标短语可在VS智能感知,当我键入 Properties.Resources。,但在第一次尝试执行的图标没有设置

和其他的尝试,我收到此错误:

  

类型setting_icon_from_resources.Properties.Resources没有名为'图标属性。   C:\ Users \用户迈赫迪\文档\的Visual Studio 2012 \项目\设置从资源\设置从资源图标图标\ Form1.Designer.cs

我是什么做错了吗?

解决方案

也终于让我找到我的问题的答案。 我真的没有得到正确的,并从互联网搜索和提问的最佳答案。

所有有关资源,一切从外面投影使用必须经过以下步骤:

  1. 在进口/下载到项目文件夹。

(它是真正共同把文件复制到一个资源文件夹作为投影子文件夹)

<醇开始=2>
  • 定义/提交/转换为局部或全局投影的的.resx 文件。
  • 的.resx 文件是一个XML)

    <醇开始=3>
  • 在得到C#.NET手柄可访问。
  • (从的.resx 设计文件)

    ( - 通常为一个属性 - 这是通过定义进行全球或本地C#.NET 的.resx 设计师)

    如果我们假设地方的.resx 作为个人背包类和投影(全局)的.resx 文件作为公共抽屉,每个班从自己的背包和公众抽屉提供他们的资源。提供资源的传统的直接方法,Visual Studio IDE中不要让类使用其他类资源。但如果你走得更远一点到的.resx 文件和设计文件,这将是可能的。

    首先让我解释了为什么一个类可以使用其他类的资源。

    当要将文件导入到本地的.resx 硬连接codeD导入的XML文件的副本的.resx 。如果你打开​​,你可以访问本地的.resx 文件的XML中的的.resx 用XML编辑器文件。

    例如,如果您导入一个图标文件到一个表单 你会看到C $ CS这些$在相关的图标XML:

    (访问XML编辑器的VS只是轻松地用鼠标右键单击的.resx 文件,然后单击用然后选择XML编辑器打开)

     &lt;数据NAME =$ this.IconTYPE =System.Drawing.Icon,System.Drawing中
     MIMETYPE =应用程序/ x-microsoft.net.object.bytearray.base64&GT;
    &LT;值GT;
    AAABAAkAMDAAAAEACACoDgAAlgAAACAgAAABAAgAqAgAAEAPAAAYGAAAAQAIAMgGAAD
    oFwAAEBAAAAEACABoBQAAsB4AAAAAAAABACAA510BABgkAAAwMAAAAQAgAKglAAAAggEAICAA
    AAEAIACoEAAAqKcBABgYAAABACAAiAkAAFC4AQAQEAAAAQAgAGgEAADYwQEAKAAAADAAAABgAAA
    AAQAIAAAAAAAACQAAAAAAAAAAAAAAAQAAAAEAAAAAAAAZHBQAHR4ZABgkFAAaKRYAHyAaAB0rGQ
    AeMBkAIiQcACQpHgAiNBwAJDkeACUn
    。
    。
    。
    + Pn5D / J5 + Q / 4 + FKP + Pn5D / J5 + Q / 4 + FKP + Pn5D / J5 + Q / 4 + FK
    P + Pn5D / J5 + Q / 4 + fkPAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP // AAD // wAA4AMAAAABA
    AAAAAAAAAEAAAADAAAAAwAAAAMAAAADAAAAAwAAAAMAAAADAAAAAwAA
    &LT; /值GT;
    &LT; /数据&GT;
     

    该文件连接codeD,你可以看到VS给它一个名字($ this.Icon),并指定其类型和MIMETYPE那里。所以这些物业现在多变

    有关访问资源的设计或构造文件 System.Resources.ResourceManager的对象必须创建。 的ResourceManager 的构造函数有两个重载,他们只是使用 resourceSource 键入作为输入参数之一。它必须是资源的主机的资源类型。我们可以得到这样的:

     的typeof(Form1中);
     

    因此​​创造了的ResourceManager 目标:

      System.Resources.ResourceManager资源=新System.Resources.ResourceManager(typeof运算(Form1中))
     

    对象资源现在有一个 GetObject的()方法来访问的.resx 导入的文件由他们的名字,例如用于图标:

     ((System.Drawing.Icon)(resources.GetObject($ this.Icon)))
     

    如果您打开窗体的设计文件,你可以看到C $ CS中的$该窗体的图标:

      this.Icon =((System.Drawing.Icon)(resources.GetObject($ this.Icon)));
     

    资源是由IDE的基本形式类创建的,所以你可以在这里使用了的ResourceManager 对象设计文件很容易。

    如果你想使用例如在窗口2 Form1的图标,你可以参考它是这样的:

      this.AutoScaleDimensions =新System.Drawing.SizeF(6F,13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize =新System.Drawing.Size(284,262);
    ** this.Icon =(System.Drawing.Icon)((新System.Resources.ResourceManager(typeof运算(Form1中)))GetObject的($ this.Icon)); ***
    this.Name =窗体2;
    this.Text =窗体2;
    this.ResumeLayout(假);
     

    (仔细看行开始与**)

    也许它能够更好地将它定义为一个公共静态属性的形式构造,以避免嵌套过多的打字:

     公共静态System.Drawing.Icon图标
    {
      得到
      {
         返程(System.Drawing.Icon)((新System.Resources.ResourceManager(typeof运算(Form1中)))GetObject的($ this.Icon));
      }
    }
     

    所以在窗口2设计师:

      this.Icon = Form1.icon;
     

    目前使用其他类的资源透露,现在让我们谈谈有关定义和使用手动全球预测的资源!

    有导入文件的一个全球性的的.resx 文件中没有硬拷贝。但只是一个参考吧。

    例如您输入图标的IDE,它创建了一个资源子文件夹,把该文件在里面。 现在如果你打开​​属性在解决方案管理器并打开Resource.resx用XML编辑器(右键点击>打开方式> XML编辑器),你可以看到这个code:

     &LT;装配别名为System.Windows.Forms的NAME =System.Windows.Forms的,版本= 4.0.0.0,文化=中性公钥= b77a5c561934e089/&GT;
    &lt;数据NAME =PerfCenterCplTYPE =System.Resources.ResXFileRef,System.Windows.Forms的&GT;
    &LT;价值&GT; .. \资源\ icon.ico; System.Drawing.Icon,System.Drawing中,版本= 4.0.0.0,文化=中性公钥= b03f5f7f11d50a3a&LT; /值GT;
    &LT; /数据&GT;
     

    您可以在上面看到的价值它指定的参考地址和类型,甚至它的版型!和它的文化类型,真的很有趣它有一个关键!

    这是它的地址,它似乎只是文件加入到投影完整性。全局资源可以是在投影夹每一个地方,在计算机中,在网络中或在互联网!

    它具有独特的关键!看来,我们可以通过其键进入该项目也是资源!

    现在的时间资源,获取其手柄 如果在解决方案管理器打开Resource.resx并打开资源设计,你可以看到它通过属性获取其句柄:

     内部静态System.Drawing.Icon图标{
            得到 {
                obj对象= ResourceManager.GetObject(图标,resourceCulture);
                返程((System.Drawing.Icon)(OBJ));
            }
           }
     

    这样的资源与 Properties.Resources.icon 访问 并且不需要任何类型转换:

      this.Icon = Properties.Resources.icon;
     

    现在,如果对资源进一步处理需要,全球资源可以是一个方法,接受输入参数!

    在我的问题我给的资源句柄,但没有任何资源引用在XML

    I'm learning C# with visual studio and i want to go a little deeper.

    I want to know about how to use project resources manually? Not by IDE. I searched the site but I just saw this dudes post How to create and use resources in .NET but it's not what I want.

    I defined a projection for myself to set a form icon manually.

    I tried this :

    1. first I added a folder to project and renamed it to Resources
    2. then I added an .ico file and renamed it to icon
    3. then I defined it in Resources.Designer.cs like this

          internal static System.Drawing.Icon icon {
          get {
              object obj = ResourceManager.GetObject("icon", resourceCulture);
              return ((System.Drawing.Icon)(obj));
          }
      }
      

    4. I added the icon in the form designer like this :

      this.Icon = Properties.Resources.icon;
      

    Everything seems fine and the icon phrase is available in the VS intellisense when I typed Properties.Resources., but at first execution attempt the icon didn't set

    And in other attempts I received this error :

    The type 'setting_icon_from_resources.Properties.Resources' has no property named 'icon'. C:\Users\mehdi\Documents\Visual Studio 2012\Projects\setting icon from resources\setting icon from resources\Form1.Designer.cs

    What am I doing wrong ?

    解决方案

    well finally i found the answer of my question . i really didn't get the correct and the best answer from searching the internet and asking questions .

    its all about resources , everything from outside used in a projection must go through these steps :

    1. imported/downloaded to project folder.

    (it's really common to put the files to a Resource folder as a projection sub-folder)

    1. defined/submitted/converted to a local or global projection's .resx file.

    (.resx file is a XML)

    1. get a handle from C#.net to be accessible .

    (from .resx designer file)

    (this is done by defining - usually as a property - in the global or local C#.net .resx designer)

    if we assume local .resx as a personal backpack for classes and projection(global) .resx file as a public drawer , every classes provide their resources from their own backpack and the public drawer . by conventional direct procedure of providing resources , Visual Studio IDE don't let the classes use other classes resources . but if you go a little further into .resx files and the designer files this would be possible .

    first let me explain how a class can use other classes resources .

    when you import files into a local .resx a hard encoded copy of the files imported into the XML of the .resx. you can access XML of the local .resx file if you open the .resx file with a XML-editor .

    for example if you imported an icon file into a form you will see these codes in the XML related to the icon :

    (to access the XML-editor in VS just easily right-click the .resx file and click open with then choose XML-editor)

    <data name="$this.Icon" type="System.Drawing.Icon, System.Drawing"
     mimetype="application/x-microsoft.net.object.bytearray.base64">
    <value>
    AAABAAkAMDAAAAEACACoDgAAlgAAACAgAAABAAgAqAgAAEAPAAAYGAAAAQAIAMgGAAD
    oFwAAEBAAAAEACABoBQAAsB4AAAAAAAABACAA510BABgkAAAwMAAAAQAgAKglAAAAggEAICAA
    AAEAIACoEAAAqKcBABgYAAABACAAiAkAAFC4AQAQEAAAAQAgAGgEAADYwQEAKAAAADAAAABgAAA
    AAQAIAAAAAAAACQAAAAAAAAAAAAAAAQAAAAEAAAAAAAAZHBQAHR4ZABgkFAAaKRYAHyAaAB0rGQ
    AeMBkAIiQcACQpHgAiNBwAJDkeACUn
    .
    .
    .
    +Pn5D/j5+Q/4+fkP+Pn5D/j5+Q/4+fkP+Pn5D/j5+Q/4+fk
    P+Pn5D/j5+Q/4+fkPAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP//AAD//wAA4AMAAAABA
    AAAAAAAAAEAAAADAAAAAwAAAAMAAAADAAAAAwAAAAMAAAADAAAAAwAA
    </value>
    </data>
    

    the file encoded , you can see VS gives it a name ($this.Icon) and specify its type and mimetype there . so these property's now is changeable

    for accessing resources in designer or constructor files an object of System.Resources.ResourceManager must be created . the constructor of ResourceManager has two overload , one of them just use resourceSource type as the input parameter . it has to be the resource type of the host of resources . one can get it like this :

    typeof(Form1) ;
    

    so creating a ResourceManager object :

    System.Resources.ResourceManager resource = new System.Resources.ResourceManager(typeof(Form1))
    

    the object resource now has a GetObject() method to access .resx imported files by their name , for example for the icon :

    ((System.Drawing.Icon)(resources.GetObject("$this.Icon")))
    

    if you open the designer file of the form you can see the codes for the icon of the form :

    this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
    

    resource is a ResourceManager object created in the base form class by the IDE so you can use it here in the designer file easily .

    if you want to use for example the icon of form1 in form2 you can refer to it like this :

    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(284, 262);
    **this.Icon = (System.Drawing.Icon)((new System.Resources.ResourceManager(typeof(Form1))).GetObject("$this.Icon"));***
    this.Name = "Form2";
    this.Text = "Form2";
    this.ResumeLayout(false);
    

    (look carefully the line started with **)

    maybe its better to defined it as a public static property in the form constructor to avoid too much typing of nestings :

    public static System.Drawing.Icon icon
    {
      get
      {
         return (System.Drawing.Icon)((new System.Resources.ResourceManager(typeof(Form1))).GetObject("$this.Icon"));
      }
    }
    

    so in the form2 designer :

    this.Icon = Form1.icon ;
    

    so far using resources of other classes revealed now lets talk about defining and using global projections resources manually !

    there's no hard-copy of the imported files in a global .resx file . there's just a reference to it .

    for example you imported icon with the IDE , it creates a Resource sub-folder and put the file in it . now if you open properties in the solution manager and open the Resource.resx with a XML editor (Right-Click > Open With > XML editor) you can see this code :

    <assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <data name="PerfCenterCpl" type="System.Resources.ResXFileRef, System.Windows.Forms">
    <value>..\Resources\icon.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
    </data>
    

    you can see above in the value it specified its reference address and its type and even its type version ! and its culture type and really interesting it has a key !

    from its address , it seems files just added to projection for integrity . global resources can be every where in the projection folder , in the computer , in the network or in the internet !

    it has a unique key ! seems we can access resources by their key in the project too !

    now its time for resource to gets its handle if you open Resource.resx in the solution manager and open the Resource Designer you can see it gets its handle by a property :

           internal static System.Drawing.Icon icon {
            get {
                object obj = ResourceManager.GetObject("icon", resourceCulture);
                return ((System.Drawing.Icon)(obj));
            }
           }
    

    so the resource is accessible with Properties.Resources.icon and doesnt need any type casting :

    this.Icon = Properties.Resources.icon ;
    

    now if further processes on the resources needed , global resources can be a method accepting input parameters !

    in my problem i give the resource a handle but there isn't any resource reference in the XML

    这篇关于如何利用人工资源在C#中(没有IDE的行为)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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