Where should constants (Label Text etc) come in WP

2019-04-14 06:21发布

问题:

Where should I create constants for Label Text, Button Text etc in a WPF MVVM application?

Is the best practice to:-

Creating static properties in ViewModel

Creating an nested class in the Model class and exposing it through a property?

Any other way.

Please advise. I am new to MVVM.

回答1:

Well I really liked and used this approach in my application. It uses standard Resx file for storing strings and uses Markup Extension for connecting view and the strings in resx file. You won't have to do any dirty trick in View Model or Model. I think resx file is best place for storing constant strings which end user will see. I used this solution for also changing the language of application at runtime. Even if your requirements are simple and you don't want fancy stuff you can use approach specified in post. It is really neat approach and places strings where they should be place (in resx). In next step you can directly bind text in resx with your label/button or anything you like.



标签: c# .net wpf mvvm