WPF Learning Notes

Source: Internet
Author: User
Tags exit thread in domain

Like WinForm, WPF also needs a application to govern some global behavior and operations, and only one application instance exists in each Domain. Unlike WinForm, the WPF application default consists of two parts: App.xaml and App.xaml.cs, which is somewhat similar to the Delphi Form, separating the definition from the behavior code. Of course, WebForm also used a similar approach. XAML is strictly not a pure XML format file, it is more like a DSL, all its definitions are mapped directly to some code, but the specific translation work is done by the compiler.

Here's a simple App definition.

public partial class App : Application
{
}

When you see paritial in the auto-generated Project code, you should subconsciously look for "This code is generated by a tool." This time, however, the automatically generated code is more erratic and--obj\debug\app.g.cs.

public partial class App : System.Windows.Application
{
 [DebuggerNonUserCode]
 public void InitializeComponent()
 {
  this.StartupUri = new System.Uri("Window1.xaml", System.UriKind.Relative);
 }
 [STAThread]
 [DebuggerNonUserCode]
 public static void Main()
 {
  App app = new App();
  app.InitializeComponent();
  app.Run();
 }
}

App.startupuri is used to set the Mainwindow,app.run () Start message loop. Of course, there's the stathread, which means that WPF still uses a UI Thread to process the UI message.

We can simply discard the auto-generated code and write an App by hand.

public class App : Application
{
 [STAThread]
 private static void Main()
 {
  var app = new App();
  var window = new Window { Title = "WPF" };
  app.Run(window);
 }
}

Application provides a number of useful properties and methods.

Current: Gets the default application instance in Domain.

MainWindow: Gets the main window instance.

Windows: Gets all instances of the Window that are instantiated.

Shutdownmode: Specifies the Application.shutdown mode, including the main form closes, the last window closes, and the Shutdown () is called manually.

Properties: A thread-safe global dictionary that can be used to store a public information.

Shutdown: This method terminates the application Process and can return an exit code to the operating system.

We can still use mutexes to prevent multiple instances from running.

private void Application_Startup(object sender, StartupEventArgs e)
{
 var createdNew = false;
 var name = Assembly.GetEntryAssembly().FullName;
 new Mutex(true, name, out createdNew);
 
 if (!createdNew)
 {
  MessageBox.Show("There is already an instance running, Exit!");
  Application.Current.Shutdown();
 }
}

Of course, you can also use Windows properties to determine if a form already exists.

private void button1_Click(object sender, RoutedEventArgs e)
{
 var window2 = Application.Current.Windows.OfType<Window>().FirstOrDefault(w => w is Window2);
 if (window2 == null) window2 = new Window2();
 window2.Show();
 window2.Activate();
}

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.