F # Adventure (8): Use F # To develop Windows Applications

Source: Internet
Author: User
Tags pixel coloring

Where are we?

The previous essays mainly focus on the basic concepts of F # and FP, with little attention to UI. We know that there is no way to interact with users without the UI, so write two articles to learn about this. This article focuses on how to use F # To develop winform applications. Later, we will introduce how to use F # To Develop ASP. NET applications.

But to be honest, I don't know much about winform development, so if you have any problems, please correct me.

The basic type in winform is the system. Windows. Forms. Form class. You can create a form based on your needs.
Draw. We can choose to use it when drawing. the API provided by net can also be used to "Draw" some images manually, such as pixel coloring, straight lines, circles, and so on. net, such
Text Box, buttons, and so on. Next, based on the event-driven features of Windows applications
Handler) enables users to interact with programs. In addition, if the standard controls or forms cannot meet the requirements, we can also expand them to create user controls and custom controls.

The following describes how to implement these processes.

Analysis

In the winform application developed using C #, we can see that the new form class we created inherits system. windows. forms. form class. In the main entry function of the program, there is a line of key code:

C # code

Application. Run (newmainform ());

That is to say, create an instance of the Form class and pass it to the application. Run method. Then the program starts execution. One major advantage of C # development is the Form Designer provided by vs ide. This allows you to set the properties of the form and control during design. Vs will generate code for us, this greatly improves the development efficiency.

Unfortunately, VS has not yet provided such design support for F #. That is to say, we must write all the code manually. If the interface is complex, do you have to be exhausted? Not all. First, consider the following simplified three-tier architecture:

This does not mean that the program must be divided into three layers, or three projects. Rather, it can be viewed in a logical sense. In most cases, only the UI Layer is related to the designer (do not forget
In other places, VS can also generate Code). If the UI is quite simple, it is no big problem to write the code completely manually. If the UI is quite complicated, you have to consider other methods.

F # is based on the. NET platform. At this point, it is equal to C # and VB. NET. This time, we can benefit from its interoperability)
Now! Since the C # class library can be called by F #, can we put the Form class in a class library? On the one hand, we can use the design support of Vs, and on the other hand, we can use the benefits of F #.
Why? In a back step, we do not need to use F # To develop the UI. We can use C # To develop the UI, and use F # To develop the UI. This is also a good method.

To sum up the above analysis, we can draw the conclusion that F # is used when developing a winform application. If the UI is very simple (for example, to draw a mathematical curve or a small tool with only two or three widgets ),
), We can write all the code manually. If the UI is complex, we use C # To develop the UI class library for F # to call, you can also only use F # To write code outside the UI.

Next we will discuss the specific implementation of these three methods.

 

Use F # compile all UI code manually

In the previous article, we used f # To draw the Mandelbrot set. Because there are no controls on the form, we need to process the painting event to complete the form painting. Here is a simple example to use the webbrowser control to create a simple browser:

F # code-Simple Web Browser

# Light

openSystem
openSystem.Drawing
openSystem.Windows.Forms
//Createtheprogressbar.
letstatusProgress=
  newToolStripProgressBar(Size=newSize(200,16),
              Style=ProgressBarStyle.Marquee,
              Visible=false)
letstatus=newStatusStrip(Dock=DockStyle.Bottom)
status.Items.Add(statusProgress)|>ignore
//Createcontrols
lettoolbar=newToolStrip(Dock=DockStyle.Top)
letaddress=newToolStripTextBox(Size=newSize(400,25))
letbrowser=newWebBrowser(Dock=DockStyle.Fill)
letgo=newToolStripButton(DisplayStyle=ToolStripItemDisplayStyle.Text,Text="Go")
address.KeyPress.Add(fune->
          if(e.KeyChar='r')then
            browser.Url<-newUri(address.Text))

go.Click.Add(fune->browser.Url<-newUri(address.Text))
toolbar.Items.Add(newToolStripLabel("Address:"))|>ignore
toolbar.Items.Add(address)|>ignore
toolbar.Items.Add(go)|>ignore
//Browsercontroleventhandlers.
browser.Navigating.Add(fune->
           statusProgress.Visible<-true)
browser.DocumentCompleted.Add(fune->
               statusProgress.Visible<-false
               address.Text<-browser.Url.AbsoluteUri)

letform=newForm(Text="WebBrowserSample",Size=newSize(800,600))
form.Controls.Add(toolbar)
form.Controls.Add(status)
form.Controls.Add(browser)
form.PerformLayout()
form.Show()
[<STAThread>]
Application.EnableVisualStyles()
Application.Run(form)

There is no need to become a mentor for a small tool like this. It's hard to drive C. Try to browse the blog site:

Note: Let's see how the event processing function in the above example is written, such as the Click Event of the Go button. We know that the event processing function of the control in C # generally has two parameters: sender (: Object) and E (: eventargs), but there is only one here, because of the standard.. Net event. F # ignores the first parameter.

Use the UI class library developed by C # In F #

Some time ago, I saw an example of how to use C # To obtain the process information of the local machine. I felt it was difficult and moderate. Let's use it as an example. In the original article, there are only two main files. One is the tool class.
Processvalidation. cs. The first is form class form1.cs. Now we want to divide form1 into two parts, and put the relevant parts of the uidesigner in the C # class library,
The background code is written in F #, And the processvalidation class is also changed to F # code.

But here I rename them according to my habits. The form is processchecker, the tool class is processhelper, and then the form and control in processfinder are used, finally, the entry function (fssamples. other files in the UI can be skipped ).

This process is relatively simple, but there is a small problem. In the form class defined in C #, each control is private by default. How to Use
What about it? There are several ways to expose them. One is to change private to public, which is simple, but not a good practice; the other is to encapsulate the control
Public attributes. If the number of controls is small, it is easier to do it. If the number of controls is large, it is also troublesome. Here I use the third method (from Allen
Lee's suggestion), that is, to change the private modifier of the control to protected, and then inherit the form, so there is still good encapsulation, the operation is relatively simple, as long as you select to modify
And set their modifiers attribute to protected. (Download Sample Code) <

It is worth mentioning that a line of code should be added before the application. Run method:

Application.EnableVisualStyles()

In this way, you can use the visual style of XP/Vista. The Running Effect of the program is:

Only use F # To develop code other than the UI

Benefit from F # and others. net Language is highly interoperable. we can develop the UI part in C # And then call the F # class library. This process is no different from calling the C # class library, I will not go into details here.

Others

The topics for winform development include creating user controls and custom controls. There is a lot of information in this regard. I believe that using this method will not be difficult to develop :)

Summary

This article mainly discusses how to use F # To develop Windows applications. Through analysis, three main methods are identified, focusing on how to use the Form Designer in Vs in F #.
The development of the winform program is much simpler. One example is processfinder, which can obtain information about processes and applications on the current machine. <

Source code

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.