Before starting the text, we need to introduce Sean Sexton. Software Engineer from Minnesota dual city. He maintains two blogs: 2,000 Things You shoshould Know About C # And 2,000 Things You shoshould Know About WPF. He updates an important and easy-to-Forget knowledge of WPF and C # Every day in a 150-word short language similar to Weibo. Follow's blog has been around for a while and I hope to share it with you.
In this series, I will not only translate every tip, but also add my own opinions and opinions in development. In this series, I hope that I can stick to it as well, and every day's progress can make great achievements.
This series is based on mr. Sean Sexton's english blog. Sean Sexton has all rights to copyright and Revoke rights.
Previous Article: <1-7>, <8-14>, <15-21>, <22-27>, <28-33>
[Xiao JIU's school is dedicated to describing extraordinary technologies in ordinary languages. If you want to reprint it, please indicate the source: xiaojiu's school. Cnblogs.com/xfuture]
#34 handle the WPF program exit event
When the WPF program is closed or exited, you can add the Exit logic in the Application. Exit event.
<Application x:Class="WpfApplication4.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml" Exit="Application_Exit"></Application> private void Application_Exit(object sender, ExitEventArgs e){ // Perform tasks at application exit}
The Exit event is triggered when the application exits and Windows exits, followed by the Windows SessionEnding event.
#35 Unhandled Exceptions
When a WPF application encounters an Exception, if the Code does not process it, it will throw, causing the application to shut down unexpectedly. For example:
When an exception is thrown, the WPF application immediately closes. The user will lose all the operations and will not get any additional information. In fact, it is the most undesirable situation for programmers to encounter. The programs they write have collapsed.
You can handle all thrown exceptions in Application. DispatcherUnhandledException. Here, we can display the exception information and set the Handled attribute to prevent program crashes. For example:
private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e){ string friendlyMsg = string.Format("SO sorry that something went wrong. The error was: [{0}]", e.Exception.Message); string caption = "Error"; MessageBox.Show(friendlyMsg, caption, MessageBoxButton.OK, MessageBoxImage.Error); // Signal that we handled things--prevents Application from exiting e.Handled = true;}
#36 Application-Scoped Properties
The Application class contains a property of Properties, which is the dictionary IDictionary of the property set and stores Key/Value. All the attributes you need to store in the Application are in this dictionary.
Properties can be read and written in any thread. It is thread-safe.
private void Application_Startup(object sender, StartupEventArgs e){ this.Properties.Add("Debug", false); this.Properties.Add("Logger", null); // Set properties based on command line parameters foreach (string a in e.Args) { if (a.ToLower() == "/debug") this.Properties["Debug"] = true; else if (a.ToLower() == "/logging") this.Properties["Logger"] = new MyAppLogger("Logfile.txt"); }}
#37 Resource
WPF and Silverlight, Resource is a. net object or value that can be used in multiple places. Resource is a reusable Resource of WPF and Silverlight.
Resources is stored in the resource Dictionary of the application. The stored objects are in the Key/Value format. Resources are usually stored in styles, templates, brushes and colors, storyboards, transforms, or 3D matrices.
#38 define and use a Resource
You can associate the Resource with the MainApplication so that you can use the Resource anywhere.
You can define resources in App. xaml:
<Application x:Class="WpfApplication.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml" Startup="Application_Startup" > <Application.Resources> <SolidColorBrush x:Key="greenBrush" Color="Green"/> </Application.Resources></Application>
In any UserControl or Window, you can reference this resource by referencing StaticResource.
<Window x:Class="WpfApplication.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="224" Width="334"> <Grid> <Button Content="Button" Background="{StaticResource greenBrush}" Height="23" HorizontalAlignment="Left" Margin="60,57,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> </Grid></Window>
#39 c # code use resource
Objects in the key/value format stored by Resource Dictionary are of the DictionaryEntry type. You can define this resource in Xaml and use it in C # code.
The specific implementation is as follows:
<Application.Resources> <SolidColorBrush x:Key="greenBrush" Color="Green"/></Application.Resources>
Key is greenBrush Value is a SolidColorBrush, and the Color attribute is Green.
C # code to get the resource:
SolidColorBrush br = (SolidColorBrush)Application.Current.Resources["greenBrush"];
Later, we will continue to explore the WPF internal mechanism. Stay tuned!
If it is helpful, click here in the lower right corner ~ (**)