Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a parameterized constructor from XAML

Tags:

wpf

xaml

While using WPF I noticed that when I add a control to a XAML file, the default constructor is called.

Is there a way to call a parameterized constructor?

like image 352
Hani Avatar asked Jul 04 '09 23:07

Hani


People also ask

How do you call a parameterized constructor?

Example of Parameterized Constructor For example, when we create the object like this MyClass obj = new MyClass(123, "Hi"); then the new keyword invokes the Parameterized constructor with int and string parameters (MyClass(int, String)) after object creation.

How do you pass a parameterized constructor?

It becomes a possible situation to pass arguments to that object. To create a parameterized constructor, it is needed to just add parameters as a value to the object as the way we pass a value to a function. Somewhat similar scenario we do by passing the parametrized values to the object created with the class.

What are parameterized constructor How are they invoked?

Parameterized Constructor – A constructor is called Parameterized Constructor when it accepts a specific number of parameters. To initialize data members of a class with distinct values. In the above example, we are passing a string and an integer to the object.

What is default parameterized constructor?

A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values. If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameterless constructor A::A() .


2 Answers

.NET 4.0 brings a new feature that challenges the answer - but apparently only for UWP applications (not WPF).

x:Arguments Directive

<object ...>     <x:Arguments>         oneOrMoreObjectElements     </x:Arguments> </object> 
like image 101
Raskal Avatar answered Oct 25 '22 23:10

Raskal


One of the guiding principles of XAML-friendly objects is that they should be completely usable with a default constructor, i.e., there is no behavior that is only accessible when using a non-default constructor. To fit with the declarative nature of XAML, object parameters are specified via property setters. There is also a convention that says that the order in which properties are set in XAML should not be important.

You may, however, have some special considerations that are important to your implementation but at odds with convention:

  1. You may have one or more properties which must be set before the object can be used.
  2. Two or more properties may be mutually exclusive with each other, e.g., it makes no sense to set both the StreamSource and UriSource of an image.
  3. You may want to ensure that a property is only set during initialization.
  4. One property may depend on another, which can be tricky due to the aforementioned convention of order independence when setting properties.

To make it easier to handle these cases, the ISupportInitialize interface is provided. When an object is read and created from XAML (i.e., parsed), objects implementing ISupportInitialize will be handled specially:

  1. The default constructor will be called.
  2. BeginInit() will be called.
  3. Properties will be set in the order they appeared in the XAML declaration.
  4. EndInit() is called.

By tracking calls to BeginInit() and EndInit(), you can handle whatever rules you need to impose, including the requirement that certain properties be set. This is how you should handle creation parameters; not by requiring constructor arguments.

Note that ISupportInitializeNotification is also provided, which extends the above interface by adding an IsInitialized property and Initialized event. I recommend using the extended version.

like image 21
Mike Strobel Avatar answered Oct 26 '22 00:10

Mike Strobel