Cross-version cost: Question about loading runtime of XAML from WPF 3 to WPF 4

Source: Internet
Author: User

Many of the software I wrote can be found in. from. Net 3.0. net 4.5 Is run at the same time, first of all, because the program must reference a lower version in writing.. Net class library environment, so Visual Studio won't let you use a later version.. net, which is not only applied in C # code, but also in XAML. For example. use bindingbase In the. NET 3.0 environment. stringformat attribute. During debugging and running, I will use a later version of the. NET environment to run the program, because it is more efficient to run, especially when the new version of the CLR is greatly improved.

In this case, no major problems have occurred when running the static compiled C # code in different. Net versions. However, for the execution of XAML, because the XAML is dynamically loaded, some hidden problems may occur. Once it appears, the program crashes directly. These problems are not because you reference the type of a higher version, but because of different versions.. NET environment for differences in the processing of XAML loading, these differences are often due to a large number of restrictions on the lower version of the environment, and because your program runs in a high version environment, so these problems are not noticed, once the program runs in a lower version, the problem may occur.

Therefore, we strongly recommend that you use a lower version of. Net during debugging when writing this type of cross-. Net version WPF program! (Of course, I need to endure the 4.5 version of CLR, which is much slower than the 2.0 version of CLR ).

 

The following are some problems. All problems are found. net 3, but not in.. Net 4.5, but I have no other versions. net, so I cannot tell which version of the problem is fixed.

To enable the program to run normally in. NET 3 (WPF 3), you must solve these problems in a lower version environment instead of using a more convenient high version.

 

Directory

  • 1. Attributes of inputbinding
  • 2. Icon problem of menuitem
  • 3. Use style to set contextmenu

 

Returned directory
1. Attributes of inputbinding

Note: inputbinding is a commonly used base class of the keybinding and mousebinding types.

In WPF 4, inputbinding inherits from freezable. Its three important attributes: Command, commandparameter, and commandtarget both have dependency attributes.

 

In WPF 3:

Although inputbinding is also dependencyobject, the three attributes of command, commandparameter, and commandtarget are not dependency attributes.

Therefore, in WPF 4, you can use binding on these attributes:

<Window. inputbindings>

<Keybinding key = "F5" command = "{binding runcommand}"/>

<Keybinding key = "F7" command = "{binding compilecommand}"/>

</Window. inputbindings>

 

However, it is not possible in WPF 3. Users can choose to use additional attributes to solve such problems. However, since it is a simple assignment, you can manually assign values to the target audience (if you are not an mvvm enthusiast ).

Note that you need to add a name to the keybinding, similar to this:

<Window. inputbindings>

<Keybinding X: Name = "kbrun" Key = "F5"/>

<Keybinding X: Name = "kbcompile" Key = "F7"/>

</Window. inputbindings>

 

Returned directory
2. Icon problem of menuitem

In WPF 4, no matter what the Icon size is, the icon will always follow the menu size:

 

In WPF 3, running the same program may surprise you:

As you can see, WPF 3 does not automatically limit the size of the menuitem icon.

Therefore, to allow your program to run correctly in WPF 3, define a size for all icons. Of course, you can use styles, such:

<Menu. Resources>

<Style targettype = "image">

<Setter property = "width" value = "20"/>

<Setter property = "height" value = "20"/>

</Style>

</Menu. Resources>

 

Returned directory
3. Use style to set contextmenu

In WPF 4, you can do this (all of this also looks very reasonable ):

<Style targettype = "listboxitem">

<Setter property = "contextmenu">

<Setter. value>

<Contextmenu>

<! --... -->

</Contextmenu>

</Setter. value>

</Setter>

</Style>

 

In WPF 3, an exception is thrown. The type is "system. windows. markup. xamlparseexception, the message is "unable to" system. add "system. windows. controls. contextmenu content." Good language.

 

The solution is to define the contextmenu in the resource and reference the contextmenu through the setter of the style. (Reference)

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.