0
votes

Question about binding in XAML with WP8.

In my App.cs I declare a public property for class Setting. In other xaml pages I need to access that propery and pass that property to a ConverterParameter. I can't say I've found a clean way of doing this. Below is my current method of how I accomplish this, but it just feels dirty. Any other ways out there?

So what's happening with code below? In app the settings data gets loaded. Any time the settings gets loaded or the a setting changes it Removes/Adds App.Current.Resource. This then allows me to data bind it {StaticResource {resourceName}}

Again, this works 100%...but is there a better/another way to accomplish this?

App.cs

private static Settings _settings = null;
public static Settings Settings
{
    get { return _settings; }
    private set { _settings = value; }
}

private async void Application_Launching(object sender, LaunchingEventArgs e)
{
    if (Settings == null)
        Settings = await FlightPath.Core.Data.LoadSettingsAsync();

    App.Current.Resources.Add("Settings", App.Settings);
    Settings.SettingsChanged += Settings_SettingsChanged;
}

private void Settings_SettingsChanged(object sender, EventArgs e)
{
    if (App.Current.Resources["Settings"] == null)
        App.Current.Resources.Add("Settings", App.Settings);
    else
    {
        App.Current.Resources.Remove("Settings");
        App.Current.Resources.Add("Settings", App.Settings);
    }
}

Application Page XAML using Converter / ConverterParameter

<TextBlock Text="{Binding observation_time, 
    Converter={StaticResource ZuluToLocalTimeConverter}, 
    ConverterParameter={StaticResource Settings}}"
    Style="{StaticResource PhoneTextNormalStyle}" 
    Margin="-4,0,0,0"/>
1
If it's common, why not have the converters just fetch the settings from a common location (within the converter code)? Or, build a more complex converter that could itself accept the reference to the settings as a property. - WiredPrairie
The issue isn't with the converter accepting the property. It can accept the "Settings" property. I'm just wondering is there a way to reference the Settings property in the App.cs file other than always settings a App.Current.Resource.Add which then makes that property accessible via StaticResource in xaml. - Clarke76
Yeah -- I understood that. That's why I made two alternative suggestions. <local:MyConverter Settings="{StaticResource TheSettings}" /> or just include logic in your Converter to retrieve the settings from a global location. - WiredPrairie
The only way the converter would be able to retrieve the settings would be to read the data from disk each time. Don't want to do that. It cannot access the global location because the Converter class is in a separate dll which can be referenced by RT and/or WP8. So it has no concept of what App : Application is. - Clarke76
Not quite sure what you mean with the xaml code you posted. You are referencing a StaticResource, which I'm already doing and works, but have to set that static resource via code, and reset each time object is updated. That's what I'm hoping to avoid. Or I'm just confused, sorry. :/ On a side note, I'm looking at passing in the App.Setting property to the ViewController of Page. That way I can assign that to property of VC and just do a normal {Binding Settings}. Not sure why I didn't think of that before. - Clarke76

1 Answers

0
votes

if you are using MVVM you can Create a SettingManager class which having a Singleton instance. Then declare its propert in ViewModelBase class. Finally use it into your xaml code

XAML

C#

class ViewModelBaseClass: InotifyPropertyChanged
{
    public SettingManager Settings{get{return SettingManager.Instance;}}
}


class SettingManager
{
   public static Instance{get{...}}

   public string this[string sName]
   {
      return "whatever you need";
   }
}

  class MYViewModel: ViewModelBase
{

}