24 JULY 2019

c#basics one-way-to-source event-trigger inotify-property-change invoke-required dispatcher-invoke

mvvm2.png

You can use the binding mode option to determine the directions in which data passes between the binding source and the target control.  Five options are available: TwoWay. Configures the binding to be bi-directional. Changes made by the user are passed back to the data source and changes in the source update the control. This option is generally used for user input controls. OneWay. Sets the binding so that changes made in the data source are copied into the bound property of the target control. Updates made by the user are not passed to the data source. This binding mode is generally used for read-only controls, such as TextBlocks. OneWayToSource. Configures the binding so that changes made by manipulating the control are passed back to the data source. Changes in the data source are not copied into the control. OneTime. A one-time data binding means that the control’s property is set when control is created or when the data context is changed. Further changes to either the property or the data source are not transmitted. This type of binding is generally used for static data or when you wish to display a snapshot of the data at a point in time. Default. Uses the default binding mode for the property. This value varies according to the control. For example, a TextBlock’s Text property defaults to being one-way but a TextBox’s Text property uses a two-way binding as standard. We saw another type of trigger that is available called- the event trigger. As the name suggests, this trigger causes actions in response to events being raised. You can detect any routed event and respond with an action or group of actions. the event trigger is mostly used for animation effects (i.e. enlarging fonts).

We learned about the MVVM pattern (Model View View-Model). MVVM facilitates a separation of development of the graphical user interface (XAML and c# code behind) – from development of the business logic or back-end logic (the data model). when we follow the MVVM architecture we need to use the INotifyPropertyChanged. INotifyPropertyChanged is an interface used to notify the Control that the property value has changed. so if we modify a property inside an object which is Bind to the XAML, it will be updated in the view. in order to inform the UI we need to fire the PropertyChanged event (which we “get for free” when we implement this interface).

The last topic was the UI thread in WPF. when we developed a Winform application we used the async and BeginInvoke mechanism in order to call the UI thread. but before we called the UI thread we needed to check if the current thread is the UI thread or not. we can call the InvokeRequired method. if the method returns true, it means that the current thread is the UI thread. in WPF we can do the same: Dispatcher.CheckAccess()  returns true if the current thread is the UI thread (and false if not). in order to ask the UI to invoke a method we use the Dispatcher.Invoke( Action ) method

More topics covered:

  • SafeInvoke pattern
  • MVVM – for testing purposes
  • Tools like Selenium – for full end to end testing
  • Model class (like person) implements the INotifyPropertyChanged
  • MVVM class could be used as the DataContext of the window

Links:

07 JULY 2019

c#basics async-await ui-thread begin-invoke ui-freeze

ui.PNG

We learned the Async Await pattern. The await keyword provides a non-blocking way to start a task, then continue execution when that task completes. whenever we want to use the await– we must add async modifier to the method signature. the async await pattern should be used when we are running operations such as: file access (read/ write), web operation, etc. the advantage of the async await is that we do not consume a thread to wait for the operation to complete. also need to take into consideration that the operations after the await are running in a background thread (so it is not guarantee to be completed). the next topic was UI thread in WinForm application. since blocking the UI thread causes the entire window to “freeze” we should always avoid making long operations on the UI thread. one solution could be to use the async await pattern. another solution could be to run long operation on a separate thread (Task) and call the UI thread using the BeginInvoke method on the specific control we want to redraw. why should we use the BeginInvoke? answer: since redrawing a component not from the UI thread causes the following exception: “…accessed from a thread other than the thread it was created on”

More topics covered:

  • ILSpy extraction tool
  • Async builds a state machine around the function
  • Async key word does not break interface
  • Async in signature return Task – automatically
  • BeginInvoke on the main form
  • Changing control color does not require the UI thread
  • After the await the same thread may continue running the operations if it is available. if not, a different thread will continue

Links:

02 January 2019

c#basics File-Explorer-App Menu
MessageBox-Result Open-File-Dialog Color-Dialog

shot.png

We developed together in class a File Explorer which could open images, open text files and color labels
We learned how to create MessaegBox which shows buttons –  and handle the option the user has selected in the message box

More topics covered:

  • Menu-Strip
  • Color-Dialog
  • Open-File-Dialog
  • Loading a file into a picture Bitmap
  • Loading text file into a list of strings (very advanced, not required in this module)

Links:

26 December 2018

c#basics Simon-game MessageBox UI-Thread new-Thread().Start Thread.Sleep const Mono

simon

We developed together in class a Simon game – where the computer plays random colors and you have to repeat them (be careful: one wrong color and your out!)
We learned about the UI thread and creating a new worker thread
We learned about global scope of variables
We learned about Thread.Sleep
We learned how to create MessaegBox using: MessageBox.Show(…);
We learned about WinForm theory, and Mono project

More topics covered:

  • Using const, i.e. const int MAX = 4
  • Setting Colors using Color., i.e. Color.Red
  • Setting Text/Colors from the Form1.Designer.cs
  •  new Thread(() =>
    {
         // put your code here
    }).Start();
  • WinForm Framework theory – .NET versions, GDI, popularity, usage, …, read here: Winform PDF
  • Mono cross-platform, read more about it here: Mono Project
    mono

Links:

19 December 2018

c#basics Whastapp ListBox KeyPress KeyDown Leave DateTime.Now KeyValue

We built a chat Application (like Whatsapp, is typing …)
We learned how to add ListBox and more events into our WinForm application.

More topics covered:

  • ListBox (Add item, get count, set focus)
  • KeyPress event: whenever the user clicked on keyboard inside TextBox
  • KeyDown: here we can detect ENTER key [KeyValue == 13]
  • Leave: whenever the user left the TextBox
  • How to print the current Date-Time
  • How to convert Integer into String , i.e. x.ToString()

Links:

12 December 2018

c#basics Calculator Button TextBox Click-Event Debug.WriteLine

We built our first calculator in Windows Form Application
We learned how to add buttons and click events into our WinForm application.

More topics covered:

  • Text boxes
  • Add Debug messages (to output window)
  • Rename the components in the window (i.e. button, text box etc.)
  • Insert text into Label
  • Add our own function into our WinForm code

Links:

09 December 2018

c#basics Functions advanced, WinForm application, String, boolean

We learned the String and Boolean types and some basic usage.

We saw functions advanced features:
(See C# 5.0 IN A NUTSHELL: page 42)
– Arrays as parameter
– int (primitive) as parameter
– ref
– out
– default values
– params
– destructuring parameters

We created our first Window Forms application
– Hello world!
– Colored in Green 🙂

Links:

Design a site like this with WordPress.com
Get started