Extension for Visual Studio - XAML Styler is a visual studio extension, which formats XAML source code by sorting the attributes based on their importance. This tool can help you/your team maintain a better XAML coding style as well as a much better XAML readability. You're both right. Yes, it's WPF, and there are lots of parts written in C#. But the parent is right - Visual Studio is C using COM and Win32 at its core, going back all the way to the Visual Basic IDE that shipped as part of Visual Studio 6. It would be a very difficult port to Mac, Linux, etc. Visual Studio 2019 for Mac. Develop apps and games for iOS, Android and using.NET. Download Visual Studio for Mac. Create and deploy scalable, performant apps using.NET and C# on the Mac.
Windows Presentation Foundation (WPF) is a very powerful framework for building desktop applications. Unlike Windows Forms, WPF provides the capabilities of nested UIElements, animation, layered presentation, and more. The focus of this tutorial is to demonstrate the aspects of Routed Events.
Routed Events are a construct specific to WPF that supports event routing. It allows for the processing of events in a very flexible fashion to meet a particular capability.
User interface elements (UIElements) have the ability to nest elements. A panel can contain a button which can contain a panel. Within that panel could be an image and some text. The user then clicks the mouse button. What element is expected to handle the event? Is it the text or the image? It could even be the panel that holds them or the button itself.
A common practice is to add a mouse click handler for a specific UIElement. The code may look something like this:
In this case, the application will route the click event for the button directly to the
MyButton_Click
method. This type of event routing is called Direct. It is more exactly RoutingStrategy.Direct. In this implementation, the concept of Routed Events is invisible to the developer.Continuing with this example, if you wanted to handle the click for an image and a text block and the button with the same action, you could add handlers on the image, textblock, stackpanel, and the button, or you could use routed events.
Looking further at this example, the elements are organized into a hierarchy:
The tutorial in this post will guide you through creating a project that will visually demonstrate the flow, or routing, of events. It is interesting to see the flow of events, both up and down the visual tree of the window.
WPF provides several mechanisms for the handling of events:
- Direct events
- Tunneling events
- Bubbling events
Direct events are most common and are used when you specify a handler on a specific UIElement. An example is when you handle the
Click
event on a button. The event will be handled by that specific handler.Bubbling events are events that get routed up the element hierarchy from the source of the event to each successive parent element until the root element of the page, the base of the element hierarchy, is reached. If there is no handler event directly associated with an element the event “bubbles” upwards until it reaches a parent element with a handler method that can handle the event.
Tunneling events are those that start at the root of the element hierarchy and travel downwards through each successive child element until the element that invoked the event is reached. Tunneling events are also referred to as Preview events because of the naming convention used for handler methods, such as
PreviewMouseDown
and PreviewMouseUp
. The Preview prefix helps differentiate in which direction the event will be handled.Events are often created in pairs of bubbling and tunneling events, with the tunneling event having the
Preview
prefix on its method name. This is done to enable each input action to invoke both a bubbling and tunneling event. The tunneling event is invoked first, giving rise to its “preview” association, then the bubbling event is raised. For more information on how these pairs of events work together, see WPF Input Events in the WPF documentation on docs.microsoft.com.Prerequisites
You’ll need the following tools and resources to build and run this project:
Windows 10 – It puts the Windows in Windows Presentation Foundation.
.NET Core SDK 3.1 – The SDK includes the APIs, runtime, and CLI.
Visual Studio 2019 with the following workloads and individual components:
![Studio Studio](https://imag.malavida.com/mvimgbig/download-fs/visual-basic-873-1.jpg)
- .NET desktop development workload (Includes C#)
- GitHub Extension for Visual Studio (If you want to clone the companion repository.)
You should have a general knowledge of Visual Studio and the C# language syntax. You will be adding and editing files, and debugging code.
There is a companion repositoryfor this post available on GitHub. It contains the complete source code for the tutorial project.
Creating the project
Begin this tutorial by creating a WPF App (.NET Core) project for C# named RoutedEventsSpyGlass. You can put the solution and project folders wherever it’s most convenient for you.
In the MainWindow.xaml file, replace the content of the file with the following XAML markup:
The layout here is simple, as you can see from the rendered UI in the file’s preview pane: a grid with several rows, each containing a UIElement. At the bottom, a ScrollViewer is positioned that will be dynamically populated from code. Above that is the header for the data that will be in the scrollview. At the top are two buttons: one to clear the scrollview and one to trigger the demonstration.
In the MainWindow.xaml.cs file, which is nested under MainWindow.xaml in the Solution Explorer, add the following
using
statement to the existing list:Replace the existing contents of the
MainWindow
class with the following code:The code first iterates over a few of the selected elements and assigns various events to the
DoEverythingEventHandler
where most of the work is done. The purpose of the DoEverythingEventHandler
is to dynamically add rows of information to the ScrollView. The information will reveal what the event is (RoutedEvent), who is processing it (Sender), where it originated (Source) and what type of routing strategy it is implementing (Routing). Several helper functions reduce duplication and make the output more readable.Testing the completed application
Before building the application, note the
Background
color attribute of the btn
button. This is to assist you in differentiating the button from the elements that it contains: the image and the text.Build and run the application. You will see the large button at the top with the text “Routed Events SpyGlass”. Right-click on the text of the button and a stream of data will be added to the scrollview portion of the window. An example of the output is provided below. If your output looks different, you may have pressed the left mouse button, which will produce a different result which will be discussed later. Paragon driver for mac.
Visual Studio For Mac Review
The data presented is interesting in how it makes it clear to see the sequence of events generated by user actions such as mouse clicks and keyboard strokes. First, you will see the PreviewMouseDown event being processed by the Grid, which is at the top of the visual tree. It will “tunnel” down to the Button and then the TextBlock. These tunneling events are named with the prefix of “Preview”. The events then change direction and “Bubble” back up the visual tree through the button and then the Grid. The events also get routed to the Window, but for simplicity it has been ignored in the example.
Every element in the tree associated with the Source has the opportunity to intercept and take action on the event.
Try again. Clear the scrollview by clicking the Clear button at the top of the application window, and then left click on the button. The output will look similar to this:
The
PreviewMouseDown
event traverses the elements much as it did before when you right-clicked, but on the bubbling of MouseDown it stops. At the bottom you will notice the click events for the Button and the Grid but not for the TextBlock. The button processes the left mouse action differently and interprets it as a “Click”. It also marks the event as Handled, which stops the further routing of the event.With the button in focus, try hitting keys on the keyboard and the corresponding data generated for those events will display.
The visual presentation of the sequence of events here is exclusively found within WPF. It is a very powerful mechanism that you can leverage, but it will seem invisible to the casual observer.
Routed events allow you to create an event handler anywhere with the visual tree of that element.
Installing and implementing a handler like this is convenient in situations where the event could originate from any of several child elements.
Potential enhancements
Mouse and keyboard events are not the only events possible here. On tablets you could experiment with stylus events. Focus and LostFocus are two additional events you could add and explore.
The application also demonstrates how to programmatically add and manipulate elements. XAML is a powerful tool, but anything that can be done in XAML can be done in code. In this case, the ability to use foreach loops made implementing the event handlers for the selected elements less repetitive.
Summary
The data presented visually in the project lends to a deeper understanding of how events get routed through the visual tree. Tunneling and Bubbling of events come to life a bit showing you exactly what is happening and in what order.
Additional resources
Routed Events Overview – This docs.microsoft.com article describes how routed events work in Windows Presentation Foundation (WPF).
Events and routed events overview – For a view of how events work in Universal Windows Platform (UWP) see this article, which provides background on event-driven code as a concept.
Using Twilio Lookup in .NET Core WPF Applications – Check out this blog post for a practical example of using Twilio Lookup in a WPF application to obtain phone number information.
TwilioQuest – If you’d like to learn more about programming C# and other languages, try this action-adventure game inspired by the 16-bit golden era of computer gaming. Paste screen capture mac.
Jeffrey Rosenthal is a C/C++/C# developer and enjoys the architectural aspects of coding and software development. Jeff is a MCSD and has operated his own company, The Coding Pit since 2008. When not coding, Jeff enjoys his home projects, rescuing dogs, and flying his drone. Jeff is available for consulting on various technologies and can be reached via email, Twitter, or LinkedIn.
Sponsored By
Buckle up friends! Microsoft is open sourcing WPF, Windows Forms (winforms), and WinUI, so the three major Windows UX technologies are going open source! All this is happening on the same day as .NET Core 3.0 Preview 1 is announced. Madness! ;)
.NET Core 3 is a major update which adds support for building Windows desktop applications using Windows Presentation Foundation (WPF), Windows Forms, and Entity Framework 6 (EF6). Note that .NET Core 3 continues to be open source and runs on Windows, Linux, Mac, in containers, and in the cloud. In the case of WPF/WinForms/etc you'll be able to create apps for Windows that include (if you like) their own copy of .NET Core for a clean side-by-side install and even faster apps at run time. The Windows UI XAML Library (WinUI) is also being open sourced AND you can use these controls in any Windows UI framework.
Visual Studio Mac Python
That means your (or my!) WPF/WinForms/WinUI apps can all use the same controls if you like, using XAML Islands. I could take the now 10 year old BabySmash WPF app and add support for pens, improved touch, or whatever makes me happy!
WPF and Windows Forms projects are run under the .NET Foundation which also announced changes today and the community will guide foundation operations. The .NET Foundation is also changing its governance model by increasing the number of board members to 7, with just 1 appointed by Microsoft. The other board members will be voted on by the community! Anyone who has contributed to a .NET Foundation project can run, similar to how the Gnome Foundation works! Learn more about the .NET Foundation here.
On the runtime and versioning side, here's a really important point from the .NET blog that's worth emphasizing IMHO:
Know that if you have existing .NET Framework apps that there is not pressure to port them to .NET Core. We will be adding features to .NET Framework 4.8 to support new desktop scenarios. While we do recommend that new desktop apps should consider targeting .NET Core, the .NET Framework will keep the high compatibility bar and will provide support for your apps for a very long time to come.
I think of it this way. If you’ve got an existing app that you’re happy with, there is no reason to port this to .NET Core. Microsoft will support the .NET Framework for a very long time, given that it’s a part of Windows. But post .NET Framework 4.8. new features will usually only become available in .NET Core because Microsoft is drastically reducing the risk and thus rate of change for .NET Framework. So if you’re building a new app or you’re actively evolving an existing app you should really start looking at .NET Core. Porting to .NET Core certainly isn’t free, but it offers many benefits, such as better performance, XCOPY deployment for the framework itself, and feature set that is growing fast, thanks to open source. Choose the strategy that makes sense for your project and/or business.
I don't want to hear any of this 'this is dead, only use that' nonsense. We just open sourced WinForms and have already taken Pull Requests. WinForms has been updated for 4k+ displays! WPF is open source, y'all! Think about the .NET Standard and how you can run standard libraries on .NET Framework, .NET Core, and Mono - or any '.NET' that's out there. Mono is enabling running .NET Standard libraries via WebAssembly. To be clear - your browser is now .NET Standard capable! There are open source projects like https://platform.uno/ and Avalonia and Ooui taking .NET in new and interesting places. Blazor makes Web UIs in .NET with (preview/experimental) client support with Web Assembly and server support included in .NET 3.0 with Razor Components. Only good things are coming, my friends!
.NET Core runs on Raspberry Pi and ARM processors! .NET Core supports serial ports, IoT devices, and there's even a System.Device.GPIO (General Purpose I/O) package! Go explore https://github.com/dotnet/iot to really get your head around how much cool stuff is happening in the .NET space.
I want to encourage you to go check out Matt Warren's extremely well-researched post 'Open Source .NET - 4 years later' to get a real visceral sense of how far we've come as a community. You'll be amazed!
Now, go play!
- Download .NET Core 3 Preview 1 on Windows, Mac and Linux.
- You can see details of the release in the .NET Core 3 Preview 1 release notes
- Visual Studio 2019 will support building .NET Core 3 applications and the VS2019 preview can be installed side by side with existing versions of VS.
Enjoy.
Sponsor: Preview the latest JetBrains Rider with its Assembly Explorer, Git Submodules, SQL language injections, integrated performance profiler and more advanced Unity support.
Wpf Visual Studio Code
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.
AboutNewsletter