how to use resources manually in C#(no IDE act)?

前端 未结 1 1596
伪装坚强ぢ
伪装坚强ぢ 2021-01-29 15:56

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 j

相关标签:
1条回答
  • 2021-01-29 16:19

    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

    0 讨论(0)
提交回复
热议问题