Answered by:
Is there any optimized way to implement the value converters without using a static or singleton reference ????

Question
-
i have created some of the converters class in my application and which is going to use in so many places. So Im using the singleton instance to implement the convert operation and im seeking the other better way to implement the converters without using static and singleton instance.
Sudhesh. G <br/> <a href="http://gurucoders.blogspot.com" target="_blank">http://gurucoders.blogspot.com</a><br/>
Answers
-
If you add your converter as a resource to your window, then only a single instance of it will be created and used for all conversions where you use it inside this window. Why would you want to make a class that is used from many places as a singleton anyway? For example, the System.String (string) and System.Int32 (int) classes are often used heavily in an application but these classes are not singletons or static...
You should only use the singleton desing pattern when you want to ensure that a class can only have one instance and when you want to provide a global point of access to it. Converters don't hold their state for the lifetime of the application in general though.
- Marked as answer by Franklin ChenMicrosoft employee, Moderator Monday, January 13, 2014 12:13 PM
All replies
-
-
If you add your converter as a resource to your window, then only a single instance of it will be created and used for all conversions where you use it inside this window. Why would you want to make a class that is used from many places as a singleton anyway? For example, the System.String (string) and System.Int32 (int) classes are often used heavily in an application but these classes are not singletons or static...
You should only use the singleton desing pattern when you want to ensure that a class can only have one instance and when you want to provide a global point of access to it. Converters don't hold their state for the lifetime of the application in general though.
- Marked as answer by Franklin ChenMicrosoft employee, Moderator Monday, January 13, 2014 12:13 PM
-
Hi,
you can call your converter from code behind. Thus, you only have one implementation.
Even in MVVM it make sense to implement converters and as well, the converter itself can be used in MVVM functions and methods.
You dont need a static on, just create an instance and call e.g. "Convert".
May look like "var s = new BooleanToVisibility().Convert(...);".Greetings,
Chris
- Edited by cpehonk Monday, January 6, 2014 11:59 AM