WPF work notes: Localization support, main thread notifications,

Source: Internet
Author: User

1. Localization support

(1) Overriding the control's default dependency property Languageproperty

FrameworkElement.LanguageProperty.OverrideMetadata (                typeofnew  Frameworkpropertymetadata (                    xmllanguage.getlanguage (CULTUREINFO.CURRENTCULTURE.IETFLANGUAGETAG)));

(2) Add Resources.resx,resources.zh-cn.resx in the project resource file, etc. localized string resources.

In the file, enter the Key-value key value pair, key the same, enter the localized language string in value, as shown below

(3) referencing namespaces in the interface. Such as:

Xmlns:resc= "Clr-namespace:wpfapplication.resources"

(4) Reference resource string resource. Such as:

<button content= "{x:static resc:Resources.Open}"/>

2. Master Process Notification

In multi-file support and audio/video playback interface applications, it is often necessary to notify the main process externally. For example, you have opened the Cool dog music playback interface, in the external folder, double-click the. mp3 music file, does not start the second cool dog music playback interface, but instead notifies the main window to receive external requests. Here is a way to implement:

First, there is only one entry for the system developed by WPF, which is the application class, the App.xaml

(1) App.xaml do not need StartupUri, in the background to start the main window. The event handle is first defined in App.xaml.cs to inform the main window

public static EventWaitHandle programstarted;private bool creatednew;

(2) App.xaml.cs rewrite Onstartup to determine if a new main window is required

programstarted = new EventWaitHandle (False, Eventresetmode.autoreset, "Mystartevent", out creatednew);

The creatednew value obtained by the above statement determines whether the main window needs to be created, and creatednew equals True when the main window is declared as a general rule:

if (creatednew) {      MainWindow mainwin = new MainWindow ();      Mainwin.show ();}

(3) Below is the focus, in the case of the main window has been started, how to transfer external files to the main window to

Since the event handle programstarted has already been defined earlier, we first need to write the external file to a local file or to the registry (named Global), and when the window process that has been started is notified, it reads what the new app process writes.

Then call Programstarted.set (); Thread.Sleep (100); The main window will be notified.

(4) A wait request needs to be registered in the thread pool of the main window:

In the MainWindow.xaml.cs constructor:

ThreadPool.RegisterWaitForSingleObject (app.programstarted, onprogramstarted, NULL,-1, false);

Event Callback handler function:

        <summary>///        main process callback function after receiving other process notifications///</summary>//        <param name= "state" ></ Param>        //<param name= "Timeout" ></param>        private void onprogramstarted (object state, BOOL Timeout)        {            thread thread = new Thread (new ThreadStart () + =            {this                . Dispatcher.begininvoke (System.Windows.Threading.DispatcherPriority.Normal, (System.Threading.ThreadStart) Delegate ()                {                    //process Global                }))            ;            Because the thread pool notifies the main process, the thread must be executed in the single-threaded apartment ApartmentState.STA            . Setapartmentstate (ApartmentState.STA);            Thread. IsBackground = true;            Thread. Start ();        }

Note that dispatcher is used in the code above, and only WPF is able to access the main window control because the main window control is in the main window and cannot be called in a new thread, and if you want to use it, you need to use dispatcher.

The default dispatcher execution environment is also ApartmentState.STA, so the above code can be as simple as (can only be used in WPF):

        private void Onprogramstarted (object state, bool timeout)        {this                . Dispatcher.begininvoke (System.Windows.Threading.DispatcherPriority.Normal, (System.Threading.ThreadStart) Delegate ()                {                   //processing Global                });        }

  

WPF work notes: Localization support, main thread notifications,

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.