Overview
The CodeBehind tab serves as a platform for defining functions linked to a display, written in either standard VB.Net or C# for WPF Clients and JavaScript for Web Clients. CodeBehind functions operate when opening, closing, or while the display is open, based on your code configuration.
CodeBehind enables you to script custom functionalities and responses to user inputs or system changes.
On this page:
Table of Contents | ||||
---|---|---|---|---|
|
Understanding CodeBehind
You can switch between VB.Net and C#, and the system automatically converts existing code to the chosen language. For your own assembly references, use Scripts → References.
The ability to switch between VB.Net and C# is a powerful feature that allows developers to leverage the strengths of both languages. Remember to thoroughly test your code after switching languages, as subtle differences between the two can lead to unexpected behaviors.
Pre-defined Methods and Custom .NET Variables
Pre-defined Methods
The CodeBehind of each display includes a set of pre-defined methods, designed to streamline common display events:
- DisplayOpening(): Triggered when a display is in the process of opening.
- DisplayIsOpen(): Regularly called throughout the duration the display remains open.
- DisplayClosing(): Activated when a display is closing.
- DialogOnOK(): Invoked when the OK button on a dialog display is pressed. Returning 1 permits the dialog to close, while returning 0 prevents its closure.
- .
Custom .NET Variables and Methods
In addition to pre-defined methods, CodeBehind allows for the incorporation of custom .NET variables and methods. These custom elements can significantly enhance the control over display behavior, accommodating more complex and tailored functionality. Therefore, their utilization should be considered for advanced or specific user interactions, for example as UI handles for Mouse Actions.
Custom Properties Extension
Alongside these methods, displays also support the use of Custom Properties Extensions. These properties serve as local variables exclusive to a particular display and can be accessed both within the display itself and in CodeBehind. Custom Properties can also be linked to tags to increase their functionality. One significant advantage of employing custom properties is the potential reduction of communication points while preserving project functionality.
Several runtime methods are available for managing Custom Properties, including methods for retrieving and setting property values, obtaining all properties as a string, and eliminating all custom properties. Note, however, that the logic programmed with Custom Properties is strictly valid for the specific display they're attached to, indicating that data cannot be preserved from one page for use on another.
For comprehensive information about Custom Properties CustomProperties and their associated methods, please refer to Custom Properties.Effectively understanding and using these pre-defined methods and Custom Properties will help to ensure your displays operate consistently and as anticipated across different situations. The addition of custom .NET variables and methods allows for even greater flexibility and customization in your application, further enhancing its robustness and adaptabilitysee Custom Properties.
Code Execution
ConsiderationsPerformance
When the CodeBehind runs on the client side, the display locks while the code is running (This can be circumvented by using Async functions). For lengthy procedures, opt for Scripts (tasks and classes) to prevent displays from locking for too long.
Code execution efficiency is crucial in HMI SCADA systems. Long running code can block the user interface, leading to poor user experience. Always strive to optimize your CodeBehind routines and consider offloading heavy processing tasks to the server or background processes.
Managing Multiple Popups
FactoryStudio provides diverse methods to manage and interact with displays and popups, including Display.Open
, Display.NewPopup
, Client.OpenDisplay
, and Client.NewPopup
, each with its own unique syntax and behavior. These methods provide flexibility in how new displays or popups are opened and handled in your project.
One key consideration is the difference between @Display
and @Client
methods. For instance, the software automatically updates the method in @Display
if the display name changes, but not in @Client
methods, which require manual updates.
Moreover, while utilizing labels as parameters for symbols can streamline your project setup, be aware of potential limitations when combining NewPopup input labels and Symbols. A common workaround is to use individual Popups for each symbol.
Online Configuration is another powerful tool for real-time project updates directly from the Engineering Environment.
For an in-depth understanding and examples of managing multiple popups in FactoryStudio, please visit Multiple Popups.
Knowledge of the available methods and their distinct behaviors can greatly enhance the flexibility and interactivity of your project.
Working with the Display Namespace
The Display Namespace contains information on how to manage displays and popups in FactoryStudio. It lists the displays and layouts with their properties and open and close methods, as well as contains the properties of the client environment on each connected device.
There are several ways to open a new display or popup, such as Display.DisplayName.Open
, Display.DisplayName.NewPopup
, Client.OpenDisplay
, and Client.NewPopup
. Each method has specific characteristics and behaviors that should be considered when using them.
In addition, the namespace includes a series of useful methods like Close()
, GetCustomPropertiesAsString()
, SetCustomProperties()
, and SetCustomPropertyValue()
, which allow you to manage and customize display properties.
For more information about the Display Namespace, please visit The Display Namespace.
Using CodeBehind in Different Display Types
CodeBehind can be used to define the mouse and input command handling methods to be executed on a specific display.
Mouse and input command handling are key for interactive displays. Consider using this feature to customize your user's interaction with each display, such as providing contextual menus on right-click, or keyboard shortcuts for frequent actions.
CodeBehind for Dialog Displays
In Dialog displays, use the built-in DialogOnOK method, which is called when the built-in OK button on the dialog is pressed. If "True" is returned on that method, the dialog is closed; if "False" is returned, the dialog remains open. This method is commonly used to ensure data validation on the dialog and prompts the user to correct incorrect entries before closing the dialog.
Dialog displays are a powerful tool for data input and user interaction. Leverage the DialogOnOK method to perform data validation and user guidance. Remember that a good user experience often involves providing clear feedback on required corrections.
CodeBehind for Standard Displays
The functionality of the CodeBehind is equally significant for standard displays. Code can be executed at various points during the display's lifecycle, enabling dynamic behavior and interaction.
Remember that code execution can affect performance. In standard displays, consider the trade-offs between interactivity and performance. For complex or computationally heavy operations, it may be better to handle these server-side or in background tasks.
CodeBehind Toolbars
The button in the image below has the functionality to enable/disable colorizing special words.
Example -
, see Objects and Namespaces
Example: Mouse Wheel Event in CurrentDisplay
To capture the mouse wheel event of a display and configure the Display.ZoomLevel of a page from it, use the following code in the CodeBehind to zoom in and out using the mouse wheel: Translate to English in a professional manner.
Code Block |
---|
public void DisplayIsOpen() { this.CurrentDisplay.GetThis().PreviewMouseWheel += this.PreviewMouseWheelChanged; } private void PreviewMouseWheelChanged(object sender, MouseWheelEventArgs e) { e.Handled = true; @Info.Trace("PreviewMouseWheelChanged || Delta: " + e.Delta); // If the mouse wheel delta is positive if (e.Delta > 0) { @Display.MainPage.ZoomLevel += 0.1; } // If the mouse wheel delta is negative? if (e.Delta < 0) { @Display.MainPage.ZoomLevel -= 0.1; } } |
References and Language Code Selection
To reference your own assemblies, use Scripts→ References. Language selection allows you to choose between VB.Net and C#, with the system automatically converting the existing code to the chosen language.
When referencing assemblies, ensure they are compatible with your selected language and that they are correctly versioned. When changing languages, always review the converted code for potential translation issues as some constructs in VB.Net might not have direct equivalents in C#, and vice versa.
Anteriormente a página estva no formato abaixo!
Overview
The CodeBehind tab is used to define a set of functions connected to a display. You can write code in either standard VB.Net or CSharp. You can also switch between both. If you change your language selection, the system automatically converts existing code to the selected language.
If you need references to your own assemblies, use Run → Build → References.
CodeBehind functions can be executed when opening or closing, or while the display is open, depending on how you configure the code. You can use CodeBehind to define the mouse and input command handling methods to be executed on a specific display.
For Dialog displays, use the built-in DialogOnOK method, which is called when the built-in OK button on the dialog is pressed. If "True" is returned on that method, the dialog is closed; if "False" is returned, the dialog remains open. This method is commonly used to ensure data validation on the dialog and prompts the user to correct incorrect entries before closing the dialog.
The Code Behind of the Displays has the following pre-defined methods:
- DisplayOpening()Executed when a display is opening.
- DisplayIsOpen()Called in regular intervals while the display is open.
- DisplayClosing()Executed when a display is closing.
- DialogOnOK()Called when the OK button on a dialog display is pressed. Returning 1 allows the dialog to close. Returning 0 prevents the dialog from closing.
You can add your own .NET variables and methods to this page.
Info |
---|
Since the client displays are designed to run on distributed environments as well as web environments, we recommend to avoid using functions that do not allow partial trust execution or that refer to physical file paths. |
.
In this section:
Page Tree | ||||
---|---|---|---|---|
|