xamarinpropertiesbindable

Global bindable property in Xamarin


I would like to create a bunch of bindable properties to be used by views from all over the app. Default font size/color in a label - that sort of thing. When a property changes, I expect all views to reflect this change. Is this possible? How do I refer to such a property in xaml?


Solution

  • So I think you can go about this two ways.

    1) Use Dynamic Resources. In your xaml file you specify the resource much like you would StaticResource, but use DynamicResource instead. Then in code behind you can modify the resource dictionary and the view should update. You can find examples of this in chapter 10 of the Petzold book: (https://developer.xamarin.com/guides/xamarin-forms/creating-mobile-apps-xamarin-forms/)

    2) Use value Converters. You can bind the property in the xaml control to a property in your view model and specify a value converter to convert from some general value or enum to a UI view value (such is Visibilty, or color etc.) You can find examples of value converters in the Petzold book in chapter 16.

    Personally, I think you should use option 1 above unless the properties are somehow related to workflow and not view customization. If you are presenting controls to the user to change things like font size or colors of things then perhaps option 2 is better.