Pass parameters to IBackgroundTask

徘徊边缘 提交于 2019-12-12 05:59:46

问题


I am implementing a windows store application for windows rt. It uses a backgroundtask to update its tile. I want to be able to configure the backgroundtask and the application to use the same urls when performing work, so i want to use a unified or centralized configuration. Right now i am using some *.resw files to configure certain aspects in my Windows Store application. How can i pass this configuration to the background task?


回答1:


The easiest way is using ApplicationData.Current.LocalSettings Here is an example of wrapper which I use in my app:

  public class SettingsService
    {

        private readonly ApplicationDataContainer _container;

        public SettingsService()
        {
            var localSettings = ApplicationData.Current.LocalSettings;

            if (!localSettings.Containers.ContainsKey("AppSettings"))
            {
                _container = localSettings.CreateContainer("AppSettings", ApplicationDataCreateDisposition.Always);
            }
            else
            {
                _container = localSettings.Containers["AppSettings"];
            }
        }

     private T GetValue<T>(string key, T @default)
        {

            if (_container.Values.ContainsKey(key))
            {
                return (T)_container.Values[key];
            }

            return @default;
        }

        private void SetValue(string key, object value)
        {
            if (!_container.Values.ContainsKey(key))
            {
                _container.Values.Add(key, value);
            }
            else
            {
                _container.Values[key] = value;
            }

        }

       //Any setting
   public bool IsFirstLaunch
    {
        get { return GetValue("IsFirstLaunch", true); }
        set { SetValue("IsFirstLaunch", value); }
    }
}



回答2:


Weirdly enough i answered my own question in a different thread

Using app resw file from background task



来源:https://stackoverflow.com/questions/13363214/pass-parameters-to-ibackgroundtask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!