WPF Miscellaneous -- getting started and getting started with wpf

Source: Internet
Author: User

WPF Miscellaneous -- getting started and getting started with wpf

I love and hate WPF technologies. At present, the WPF market is not very popular. If you eat in WPF, you are afraid that you will starve to death in the streets. At the same time, face-to-face WEB development is even more prosperous. So if it is a new student, it is best not to use WPF as the main type. It is okay to study it as an alternative.

WPF Project

There are three types of projects related to WPF applications in VS development tools. Originally, in my opinion, he may have a separate place. Later I thought it was right. WPF is also called Window development. The three WPF project types are also normal under the template Window node. As follows:

For the "WPF Application" project, we all know that the project is executed. The only thing I don't understand is why there are two control library projects. In plain text, we can see that one is user and the other is custom. So what is the difference between the two? The author understands this: In terms of usage, user controls are mainly used for business reuse. This is similar to the Winform user control. Many controls currently exist. Naturally, the original control is pulled into the same box to form a new control. The custom control is further inherited and extended on the original control. It is equivalent to a brand new control. In terms of functions, either the user or the User-Defined owner can implement the functions of the other party. However, I think the customization will be more in-depth. The custom control will reset the Style and Template. (Here for better understanding) there will be some difference after the project is created. As follows:

User Control Project

Custom Controls

Whether it is WinForm development or WPF development. We can see the shadow of the Application class. I really don't like this. It is easy to put the two together. After a new WPF application project is created, two Xaml files are generated. As follows:

App. xaml is a subclass that inherits the Application class. The corresponding WPF operation is completed by him. However, many people do not like the default WPF method. Always want to be started in a similar way as WinForm. You may not forget WinForm. At least I have a point. How to implement it?

If all of them are windows application development, I think the Main function is indispensable. However, I clicked on the relevant source code file and did not find the corresponding Main function. Later, we found it through the following method. It was originally in the App. g. I. cs file. So I learned about it. App. Xaml has an attribute type.

The property type refers to the "generate operation" section in the property of App. xaml. The default value is ApplicationDefinition. If ApplicationDefinition is used, the Main function is generated by default. As follows.

After understanding the above mechanism, it will be much simpler. Set the property of App. xaml to Page and create a class called Program. The Code is as follows.

 public class Program    {        [System.STAThreadAttribute()]        [System.Diagnostics.DebuggerNonUserCodeAttribute()]        [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]        public static void Main()        {            WpfApplication2.App app = new WpfApplication2.App();            app.InitializeComponent();            app.Run();        }    }

From the above description, we can know that App. xaml seems to have a variety. If it is an ApplicationDefinition, you can start the project to call it directly. That is, you can delete the Main function and automatically generate it for you. If it is a Page, it will be SORRY. You must manually add the Main function. This is not difficult for A. NET developer. As I have mentioned, it is similar to the Winfrom entry function.

WPF Interface

The appearance of WPF programming is the most satisfying place for the author. He is not as rigid as a traditional WinForm. The introduction is similar to the B/S mode. I believe you have heard of CSS styles. No error. Very similar. What does it mean? WinForm UI editing is definitely a rigid method. The WPF interface references XAML for editing. This may make the interface more independent. At the same time, it makes the UI more agile and convenient. Because he also has a concept similar to HTML web pages-Style ). See below (reference to the open-source project (Modern UI for WPF ))

 1     <Style x:Key="SystemButton" TargetType="ButtonBase" BasedOn="{StaticResource SystemButtonBase}"> 2         <Setter Property="Width" Value="32" /> 3         <Setter Property="Height" Value="24" /> 4         <Setter Property="Foreground" Value="{DynamicResource ButtonText}"/> 5         <Style.Triggers> 6             <Trigger Property="IsMouseOver" Value="True"> 7                 <Setter Property="Background" Value="{DynamicResource ButtonBackgroundHover}" /> 8                 <Setter Property="Foreground" Value="{DynamicResource ButtonTextHover}"/> 9             </Trigger>10             <Trigger Property="IsPressed" Value="True">11                 <Setter Property="Background" Value="{DynamicResource ButtonBackgroundPressed}" />12                 <Setter Property="Foreground" Value="{DynamicResource ButtonTextPressed}" />13             </Trigger>14             <Trigger Property="IsEnabled" Value="false">15                 <Setter Property="Foreground" Value="{DynamicResource ButtonTextDisabled}" />16             </Trigger>17         </Style.Triggers>18     </Style>

The above is just a style called SystemButton. It is mainly used to modify the button. It is a little similar to the CSS style class style. Not to mention it. The red SystemButton is as follows.

 <Button x:Name="Maximize" Command="{Binding Source={x:Static SystemCommands.MaximizeWindowCommand}}" ToolTip="{x:Static modernui:Resources.Maximize}" Style="{StaticResource SystemButton}" >

We all know that HTML webpages must declare references in the header if they want to reference CSS style files. In this way, you can know which CSS style file is referenced on the current interface. It is undeniable that the WPF interface is similar to a subclass. The referenced file is not a CSS style file, but a resource file. I will discuss this later. However, B/S and C/S are essentially different. At least he can declare a resource file that acts on the entire application. As follows:

<Application x:Class="FirstFloor.ModernUI.App.App"             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"             StartupUri="MainWindow.xaml">    <Application.Resources>        <ResourceDictionary>            <ResourceDictionary.MergedDictionaries>                <ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.xaml" />                <ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.Light.xaml" />            </ResourceDictionary.MergedDictionaries>        </ResourceDictionary>    </Application.Resources></Application

To start a WPF Application, you must use the Application class or subclass. Execute the Application class to find the form. In the source code above, StartupUri = "MaiWindow. xaml" is displayed. Of course it is impossible to do this. You can use the code.

In WinForm, an application has several Form groups. However, the WPF interface is not necessarily Oh. You can have a Window, several usercontrols, or several pages. If you choose a method, you can do it as long as the project is suitable. If it is unclear, you can follow the open-source project Modern UI for WPF. It is composed of a Window and multiple usercontrols. (Window is equivalent to Form)

At the beginning, I did not directly view open-source projects. Instead, we first learned about VS's new generation. At least you need to know where to start. In what way. What is the interface. Understand the source of. You can start learning about open-source projects.

Related Article

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.