WPF Miscellaneous -- resource files and wpf miscellaneous resource files
Compiling an application will inevitably use the WPF control. Both WinForm and web pages have their own controls. However, the write method and usage are different. The control name is inseparable from those words. So you don't have to worry about deciding which one is a button and which is a text box. For example.
HTML
<Input type = "button" value = "Twist"/>
WinForm
private System.Windows.Forms.Button button1;
WPF
<Button Content = "Button"/>
See it. Essentially, there is no difference. You only need to learn his attribute usage.
When designing the business interface, we must do not need to layout the interface accordingly. DIV + CSS is often used for layout on webpages. What I want to talk about is not what technology is needed. But the knowledge used. For example, the layout elements in HTML include DIV, SPAN, and Table. For example, Panel is often used in WinForm. Is there any knowledge similar to this in WPF. Of course. It is worth noting that PC (C/S) and web pages (B/S) have different la S. The PC end has an additional knowledge point for layout. Generally, it can be divided into absolute layout (AbsoluteLayout), border layout (BorderLayout), flow layout (FlowLayout), table layout (TableLayout), and AnchorLayout. But not all la s exist. It depends on whether the people who design this technology have included all the la S.
The layout elements of WPF are Grid, StackPanel, and WrapPanel. These are all layout knowledge. As follows:
Grid: table layout or absolute layout.
StackPanel: Linear layout (either vertical or horizontal ).
WrapPanel: stream layout.
It can be said that the above three elements are enough for you to layout the Target stream interface. However, this is only used for Division on the interface. Style is required for the beauty and beauty ).
I learned about the WPF style and started with CSS style. For example, CSS can be implemented in three ways: External styles, internal styles, and embedded styles. How many methods can be used to implement WPF styles? I got it from the development process-two types. No embedded style exists. But there is another concept. This concept is related to controls. So I downloaded an image on the webpage. As follows:
We can see from the picture that all commonly used controls will inherit from the FrameworkElement class. In the FrameworkElement class, there is an attribute called Resources. This attribute allows you to create and store various styles. This means that each control has its own style storage place. That is to say, the role of a style can be controlled to the scope of a control. You can create CSS styles anytime, anywhere on a webpage. You only need to declare the Style. But it still acts on the entire web page. Therefore, the style declared in the control or Window form by WPF is called an internal style.
The external style on the webpage references an external file. This external file stores a large number of CSS styles. The same way does WPF. But there is a difference -- range. An external file on a webpage can only serve as a webpage, but WPF can serve as a whole application. No matter how many windows, how many usercontrols. Set the referenced external files in the Application as described in the previous chapter. It is also an external style that I think. As follows:
<ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.xaml" />
Now, each control has its own style resource. The question is: if the two styles are modified on the same object, what should they choose? Let's take a small example.
<Window x:Class="WpfApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <Style TargetType="TextBlock"> <Setter Property="Foreground" Value="Red"></Setter> </Style> </Window.Resources> <Grid> <Grid.Resources> <Style TargetType="TextBlock"> <Setter Property="Foreground" Value="Blue"></Setter> </Style> </Grid.Resources> <TextBlock Text="1223"></TextBlock> </Grid></Window>
Running result:
In the Window form, I wrote a style to modify the TextBlock control and set the foreground color to red. In Grid, I also wrote a style to modify the TextBlock control, but the color is blue. The result shows that the latest style is selected.
In the above column, the author modifies all TextBlock controls. It is similar to the element selector in CSS styles. It is usually used for custom controls. Another method is similar to the class selector in the CSS style. As follows:
1 <Style x:Key="AlertButton" TargetType="ButtonBase" BasedOn="{StaticResource SystemButtonBase}"> 2 <Setter Property="Cursor" Value="Hand" /> 3 <Setter Property="Margin" Value="8"/> 4 <Setter Property="Padding" Value="4"/> 5 <Style.Triggers> 6 <Trigger Property="IsMouseOver" Value="True"> 7 <Setter Property="Opacity" Value=".7" /> 8 </Trigger> 9 <Trigger Property="IsPressed" Value="True">10 <Setter Property="Opacity" Value=".4" />11 </Trigger>12 </Style.Triggers>13 </Style>
The above Code comes from FirstFloor. ModernUI, an open-source project. "X: Key" is equivalent to "." In the CSS style ".". In addition, we can see that baseOn is used for inheritance. I believe everyone can understand what it means. Setter is used to set the current property value. Style. Triggers is used to set trigger actions. For example, what changes will happen when you press the button. Property = "IsPressed" Value = "True" is equivalent to IF. When the current press is pressed, The IsPressed value is changed to True. The server is found to match the current trigger condition. Then the corresponding settings are executed. For example, the above transparency (Opacity) will be. 4.
In addition to the style mentioned above, resources can also store other things. For example, color. Forget it. Only text descriptions cannot be obvious or direct. Let me give you a column.
1 <Window x:Class="WpfApp.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:sys="clr-namespace:System;assembly=mscorlib" 5 Title="MainWindow" Height="350" Width="525"> 6 <Window.Resources> 7 <sys:String x:Key="TestString">I am Aomi</sys:String> 8 </Window.Resources> 9 <Grid>10 <TextBlock Text="{StaticResource TestString}"></TextBlock>11 </Grid>12 </Window>
Running result:
We can see that. Resource files are not just styles, but also system values. However, you must pay attention to references. Xmlns: sys = "clr-namespace: System; assembly = mscorlib ".
In terms of referenced styles, WPF is also divided into static and dynamic differences. For example, the above is a static reference. The keyword DynamicResource is used for dynamics. Then the concept of dynamic and static resources comes out. I remember someone asked me before what is the difference between them. I cannot even figure out what dynamic resources are and what Static resources are.
Static Resource: It is initialized only once when the xaml file is loaded. At the same time, you cannot perform operations on resources. This means that if the resource is modified later, the corresponding references will not be synchronized.
Dynamic Resource: It is initialized during running and can be operated at the same time. Obviously, modifications to the resources will be synchronized to the corresponding references.
After knowing this, let me give an example. Modify the column according to the column above.
<Window x: Class = "WpfApp. mainWindow "xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation "xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "xmlns: sys =" clr-namespace: System; assembly = mscorlib "Title =" MainWindow "Height =" 350 "Width =" 525 "> <Window. resources> <sys: String x: Key = "TestString"> I am Aomi </sys: String> </Window. resources> <Grid. rowDefinitions> <RowDefinition Height = "auto"/> <RowDefinition Height = "auto"/> </Grid. rowDefinitions> <TextBlock x: Name = "TbText" Grid. row = "0" Text = "{DynamicResource TestString}"> </TextBlock> <Button Grid. row = "1" Content = "change" Click = "Button_Click"> </Button> </Grid> </Window>
Backend code:
private void Button_Click(object sender, RoutedEventArgs e) { this.Resources["TestString"] ="aaa"; }
Dynamic Resource execution result:
Change the above reference to the following static reference.
<TextBlock x:Name="TbText" Grid.Row="0" Text="{StaticResource TestString}"></TextBlock>
Static resource execution result:
Is Dynamic Resources good or static resources good? I understand this as follows: static resources are loaded only once during compilation. In the future, even if you modify other resources, it will not change. Dynamic resources are also initialized during compilation, but the value at this time is not initialized. The value is loaded only when required during running. So? If you do not need to modify the changes later in the project process, you can try to use static resources. Understand that dynamic resources are not loaded once. Performance needs to be considered.