WPF resource sequencer

Source: Internet
Author: User

1. connection between two resource files

Using this tool, two resource files are linked into a resource file:
Al/template: WpfLocalizationLocBaml.exe
/Embed: de \ WpfLocalizationLocBaml.g.de. resources
/Embed: .. \ obj \ WpfLocalization.Properties.Resources.de. resources
/Culture: de/out: de \ WpfLocalizationLocBaml. resources. dll

WpfLocalizationLocBaml.g.de. resources is a file generated using LocBaml, which can be found in obj \ Debug or generated using LocBaml:

LocBaml.exe/generate... \ obj \ WpfLocalizationLocBaml.g.de. resources
/Trans:... \ .. \ Res \ de.csv

WpfLocalization.Properties.Resources.de. resources is the resource file in the Properties folder of the project.

Finally, a WpfLocalizationLocBaml. resources. dll will be generated. Put it in the folder of the corresponding language. Here is de.

2. Use. net generic resources in Xaml

By default, the class in the cs file corresponding to the resource file generated by Visual Studio is internal and cannot be referenced in xaml. We need to change the tool used to compile the resource file, for example:

Right-click the resource file and change ResXFileCodeGenerator to PublicResXFileCodeGenerator In the attribute (in the 2005 era, this tool is calledResXFileCodeGeneratorExIn this way, the class of the generated cs file is pulic. It is best to check it again to avoid some misoperation to clear the cs file.

<Window x:Class="WpfLocalization1.Window1"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    Title="WPF_Localize" Height="155" Width="194"    xmlns:prop="clr-namespace:WpfLocalization1.Properties">    <Grid>        <Button Margin="30,20,35,26" Name="button1"                 Content="{x:Static prop:Resources.MainButtonText}" />    </Grid></Window>

After adding the Properties namespace in xaml, you can use StaticExtension to get the corresponding value in the resource file, as shown in the above Code segment.

3. Define Resource in xaml

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="Simple Window">    <Window.Resources>        <SolidColorBrush x:Key="backgroundBrush">Yellow</SolidColorBrush>        <SolidColorBrush x:Key="borderBrush">Red</SolidColorBrush>    </Window.Resources>    <Window.Background>        <StaticResource ResourceKey="backgroundBrush"/>    </Window.Background>    <DockPanel>        <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal"                HorizontalAlignment="Center">            <Button Background="{StaticResource backgroundBrush}"                BorderBrush="{StaticResource borderBrush}" Margin="5">                <Image Height="21" Source="zoom.gif"/>            </Button>            <Button Background="{StaticResource backgroundBrush}"                BorderBrush="{StaticResource borderBrush}" Margin="5">                <Image Height="21" Source="defaultThumbnailSize.gif"/>            </Button>            <Button Background="{StaticResource backgroundBrush}"                BorderBrush="{StaticResource borderBrush}" Margin="5">                <Image Height="21" Source="previous.gif"/>            </Button>            <Button Background="{StaticResource backgroundBrush}"                BorderBrush="{StaticResource borderBrush}" Margin="5">                <Image Height="21" Source="slideshow.gif"/>            </Button>            <Button Background="{StaticResource backgroundBrush}"                BorderBrush="{StaticResource borderBrush}" Margin="5">                <Image Height="21" Source="next.gif"/>            </Button>            <Button Background="{StaticResource backgroundBrush}"                BorderBrush="{StaticResource borderBrush}" Margin="5">                <Image Height="21" Source="counterclockwise.gif"/>            </Button>            <Button Background="{StaticResource backgroundBrush}"                BorderBrush="{StaticResource borderBrush}" Margin="5">                <Image Height="21" Source="clockwise.gif"/>            </Button>            <Button Background="{StaticResource backgroundBrush}"                BorderBrush="{StaticResource borderBrush}" Margin="5">                <Image Height="21" Source="delete.gif"/>            </Button>        </StackPanel>        <ListBox/>    </DockPanel></Window>

Classes inherited from FrameworkElement have the Resource attribute. They are ResourceDictionary classes, that is, key-value tables based on Hasgtable. The Key is represented by x: Key.

Resources can be placed at different levels according to different requirements, such as Application level (in App. xaml), windows level, and control level. The child level can reference the parent level Resource. The child level Resource can overwrite the parent level Resource. If the parent and child define the same key, the child level is the primary level.

You can place the Resource in different files (right-click and add Resource Dictionary in Visual Studio). You can use MergeredDictionaries to merge the Resource files together:

<Window. Resources>
<ResourceDictionary>
<ResourceDictionary. MergedDictionaries>
<ResourceDictionary Source = "file1.xaml"/>
<ResourceDictionary Source = "file2.xaml"/>
</ResourceDictionary. MergedDictionaries>
</ResourceDictionary>
</Window. Resources>

Sometimes you need to set resource for FrameworkElement or FrameworkContentElement. You can set resource x: Shared = false so that the referenced FrameworkElement is actually a different copy.

4. StaticResource and DynamicResource

StaticResource is usually loaded during windows or page Load, while DynamicResource is loaded only when it is used. DynamicResource is more overhead than StaticResource.
DynamicResource can only be set on dependency property. StaticResource does not have this restriction.
StaticResource must be declared before it can be referenced. DynamicResource does not have this restriction.

SystemColors, SystemFonts, and SystemParameters are defined by the system and can be set in the control panel. Therefore, DynamicResource is used.
<Button Background = "{DynamicResource {x: Static SystemColors. WindowBrush}"/>

5. Use resource in C #

Define resources:
Window. Resources. Add ("backgroundBrush", new SolidColorBrush ("Yellow "));
Window. Resources. Add ("borderBrush", new SolidColorBrush ("Red "));

StaticResource:
Button button = new Button ();
Button. Background = (Brush) button.FindResource("BackgroundBrush ");
Button. BorderBrush = (Brush) button.FindResource("BorderBrush ");

FindResource is not found, or the TryFindResource method is used. If no result is found, null is returned.

DynamicResource:
Button button = new Button ();
Button.SetResourceReference(Button. BackgroundProperty, "backgroundBrush ");
Button.SetResourceReference(Button. BorderBrushProperty, "borderBrush ");

From this we can see that DynamicResource can only be used on Dependency property.

Although the index can be used directly to retrieve resources:
Button button = new Button ();
Button. Background = (Brush) window. Resources ["backgroundBrush"];
Button. BorderBrush = (Brush) window. Resources ["borderBrush"];

But this method isNot advocateBecause directly retrieving Resource dictionary does not traverse the logical tree, unexpected results may occur in some cases. Of course, the logic tree is not traversed, which improves the performance a little.

6. Reference resources in other assemblies

Define resources:
<SolidColorBrush
X: Key = "{ComponentResourceKey TypeInTargetAssembly = {x: Type local: MyClass },
ResourceId = MyClassBrush} "> Yellow </SolidColorBrush>

The Key cannot use string. You need to use the Markup Extension ComponentResourceKey.

Resource usage:
<Button Background = "{DynamicResource {ComponentResourceKey TypeInTargetAssembly =
OtherAssembly: MyClass, ResourceId = MyClassBrush} "/>

 

For how WPF uses resource files, see http://www.codeproject.com/KB/WPF/WPFUsingLocbaml.aspx#link_4

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.