[CSharp] 6. file attributes and usage of resource files,

Source: Internet
Author: User

[CSharp] 6. file attributes and usage of resource files,
I. C # file attributes

1. What is a file attribute?


File properties can be used to indicate the operations performed by the corresponding file of the project system. For more information, see. Visual Basic and Visual C # files have four attributes: "FileName", "BuildAcition", "CustomTool", and "CustomToolNamespace".


2. CopyToOutputDirectory attributes


This attribute specifies the conditions for copying the selected source file to the output directory. If you never copy files to the output directory, select "do not copy". If you always copy files to the output directory, select "always copy "; if the file is copied only when it is an existing file with the same name as the output directory, select "Update to copy ".

3. BuildAction attributes


None: this file is not included in the project output group and will not be compiled during the generation process. For example, a text file containing a document, such as a self-report file.

Compile: Compile the file into the generated output. This setting is used for code files.

Content: this file is not compiled, but is included in the Content output group. For example, this setting is the default value for. htm or other types of Web files.

Embedded Resource: embed the file as a DLL or executable file into the main project generation and output. This setting is usually used for resource files.

ApplicationDefinition: similar to Page, but only used for the launch Page of Silverlight (default: App. xaml ).

Page: All usercontrol/page/childwindowxaml in Silverlight belong to the "Page" build. Other buildactions cannot connect the build action to the codebehind file.

CodeAnalysisDictionary: Custom CodeAnalysis dictionary.

Resource: embedsthe file in a shared (by all files in the assembly with similar setting) assembly manifest resource named AppName. g. resources

SplashScreen: Welcome Page for Silverlight.

DesignData: Sampledata types will be created as faux types. Use this Build Action when the sampledata types are not creatable or have read-only properties that you want todefined sample data values.

DesignDataWithDesignTimeCreatableTypes: Sampledata types will be created using the types defined in the sample data file. Usethis Build Aciton when the sample data types are creatable using their defaultempty constructor.

EntityDeploy: Applicable to the Entity Framework.

Note: When an image is added to the resource editor, it sets "generate operation" to "NONE" because the. resx file references the image file. During creation, the image will be dragged into the. resources file created from the. resx file. Then, you can easily access the image by automatically generating a strong class for the. resx file. Therefore, you should not change this setting to "embedded resources", because this will include images twice in the Assembly. 

The usage of resource files is described below.

4. FileName attributes

Click "FileName" in the "properties" window and enter a new name to rename the file. Note that if you change the file name, Visual Studio automatically renames any. vb or. resx files associated with it.

5. CustomTool attributes

Custom tools are components that can be used to convert files from one type to another at design time. For example, a custom tool can be a dataset code generator that reads data from an XML schema (. xsd) file and generates classes in code files that expose their tables and columns programmatically. The product has a predefined list of available custom tools. This attribute allows you to know which custom tool applies to a file. In rare cases, you may need to change the value of this attribute. The value of this attribute must be blank or one of the built-in custom tools.

To set or change a custom tool, click "CustomTool" in the "properties" window and enter the name of the custom tool.

6. CustomToolNamespaces attributes

If a custom tool has been assigned to a project, the CustomToolNamespace attribute allows you to specify a namespace for the code generated by the custom tool. When a value is specified for the CustomToolNamespace attribute, the code generated by the custom tool will be placed in the specified namespace. If this attribute is blank, the generated code is placed in the default namespace of the folder where the converted file is located. For Visual Basic, It is the root namespace of the project, for Visual C #, it corresponds to the setting of the "DefaultNamespace" attribute of the folder.

Ii. Use of resource files


1. What is a resource file?

Resource files are generally any non-executable data files used by applications, such as, audio, and video files. Resource files may have specific meanings in some contexts. For example, in the context of application localization, the resource file refers to the. resx file. You can deploy these files in a localized accessory.

Generally, resource files are referenced in code or XAML by using a uniform resource identifier (URI. The required URI format depends on the method of the deployment file. You can also specify relative or absolute URI.

Supplement: What is relative and absolute URI?

A) specify the resource file location relative to the application root or reference XAML relative to the URI. For example, "http://www.example.com/resources/picture.png" indicates a file in the resource folder in the specified domain. If the domain where the resource file is located is not the host domain of the application package, you must specify an absolute URI.

 

B) Absolute URI specifies the exact location of the resource file, bypassing the rollback mechanism.

Supplement: What is the URI relative to the application root, relative to referencing XAML, and pointing to other sets?

The application root is the root folder of the application package or the location of the application package on the server. Compared with the URI rollback mechanism, the application package is searched first, and then the server is searched. If the path is included after the leading slash, search for the same folder hierarchy in the two locations.

If reference XAML is in a folder, you can reference files in other folders relative to this location. However, you cannot search on the application root, for example, "../resources/image.png ". This URI searches for the resource folder at the level of the folder containing XAML. It is assumed that the folder is not the application root.

 

If the referenced resource file is loaded from the application package but the referenced resource file is not found, the default rollback mechanism is used. The XAML path referenced in the application package is used for searching on the server. For example, consider the XAML file in the following position in the application package:/pages/xaml/MyPage. xaml. In this case, the rollback mechanism searches for "../resources/image.png":/pages/resources/image.png at the following locations on the server.

If you compile reference XAML into an assembly, you must embed the source file in the same assembly. In this case, the URI rollback mechanism is provided by the ResourceManager class. This rollback mechanism uses language and regional settings to find the resource files in the affiliated Assembly.

C) You can also reference the resource file embedded in an assembly. This assembly is not an Assembly that contains reference to XAML. In this case, URI uses the following format: "/assemblyShortName; component/resourceLocation, for example,"/SilverlightLibraryAssembly; component/image.png ". Note that you need to use the leading slash and the component keyword (followed by a slash ).

For other embedded resources, the URI format uses the rollback mechanism provided by the ResourceManager class.

2. Resource file type

The System. Resources namespace supports three types of resource files:

. Txt file, which can only contain string resources. Because it cannot be embedded into Assembly, it is easy to expose and modified by the customer. The biggest drawback is that only string resources are supported, so it is not recommended.

The. resx file is composed of XML and can be added to any resource, including binary. Cannot be embedded into Assembly. There is a dedicated read/write class in the System. Resources namespace. VS. NET creates such a file, converts it to A. resources file, and embeds it into Assembly according to the settings.

. Resources file, in PE format, can be added to any resource. The only file that can be embedded into the Assembly has a dedicated read/write class in the System. Resources namespace.

3. How to configure resource files


In Visual Studio, you can add resource files to the Silverlight project and configure these resource files for deployment by setting its "generate operation" value. You can use the following generation operations for resource files in the Silverlight project:

  • Resource: This generation operation embeds the file into the project assembly. You can use this option for applications and library projects, and deploy these assembly inside or outside the application package.
  • Content: This generation operation will include the file in the application package without embedding it in the project assembly. Use this option to use the resource files shared by multiple assembly in the package.
  • None: This generation operation does not include files in application packages or datasets. Use this option to search resource files as needed. Usually deploy on-demand files in the same location as the server where the application package is located.

Note: The "properties" window in Visual Studio provides the other values in the "generate operation" drop-down list. However, to apply to a Silverlight project, you can only select the first three items. For example, the Silverlight embedded resource always uses the "resource" generation operation instead of the "embedded resource" generation operation because the latter uses a format that cannot be recognized by Silverlight.


3. Grasp the usage of resource files in practice


You must know how to add background images to the Button space in XAML. Let's take a look at how to add background images to the Button space in the background. Create a new WPF project, add the Button space in XAML, and double-click to process the click event.

private void btn15_Click(object sender, RoutedEventArgs e){    ImageBrush ib = new ImageBrush();    ib.ImageSource = new BitmapImage(new Uri(@"Images/1.jpg", UriKind.Relative));    btn.Background = ib;}


Set the image to Content ).

However, if you copy the compiled exe file to another location and open it, the image will not be loaded. In this case, you need to add the image to the resource file.


Double-click the resource file to be added, select the file type to be added, and select "add existing file.



The following style will appear after adding.


Return to the background code. Because the resource file is of the Bitmap type, it needs to be converted.

public static BitmapSource ChangeBitmapToBitmapSource(Bitmap bmp){    BitmapSource returnSource;    try    {        returnSource = Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());     }     catch     {         returnSource = null;     }     return returnSource;}

After conversion, add the Background attribute to the Button directly.

private void btn_Click(object sender, RoutedEventArgs e){    var img2 = Game.Resource1._2;    btn.Background = new ImageBrush(ChangeBitmapToBitmapSource(img2));}

OK. This blog is so far. Thank you very much for your guidance from Aran.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.