MvvmCross将配置传递给可配置的插件加载器

时间:2013-08-17 17:27:42

标签: plugins mvvmcross portable-class-library

我正在创建一个可移植的MockGeoLocationWatcher,可以替代IMvxGeoLocationWatcher的具体实现,直到有一个实际的设备。这应该有助于开发和测试需要地理位置的应用程序。

此插件的PluginLoader类目前如下所示:

namespace Pidac.MvvmCross.Plugins.Location
{
    public class PluginLoader : IMvxConfigurablePluginLoader
    {
        private bool _loaded;
        public static readonly PluginLoader Instance = new PluginLoader();

        public void EnsureLoaded()
        {
            if (_loaded)
                return;

            _loaded = true;


            var locationWatcher = new MockGeoLocationWatcher();

            var data = @"<?xml version='1.0' encoding='utf-8'?>
<WindowsPhoneEmulator xmlns='http://schemas.microsoft.com/WindowsPhoneEmulator/2009/08/SensorData'>
    <SensorData>
        <Header version='1' />
        <GpsData latitude='48.619934106826' longitude='-84.5247359841114' />
        <GpsData latitude='48.6852544862377' longitude='-83.9864059059864' />
        <GpsData latitude='48.8445703681025' longitude='-83.7337203591114' />
        <GpsData latitude='48.8662561090809' longitude='-83.2393355934864' />
        <GpsData latitude='49.0825970371386' longitude='-83.0415816872364' />
        <GpsData latitude='49.2621642999055' longitude='-82.7229781716114' />
        <GpsData latitude='49.2621642999055' longitude='-82.6021285622364' />
        <GpsData latitude='49.2047736379815' longitude='-82.3054977028614' />
    </SensorData>
</WindowsPhoneEmulator>";

            locationWatcher.SensorLocationData = data;
            Mvx.RegisterSingleton(typeof(IMvxGeoLocationWatcher), locationWatcher);
        }

        public void Configure(IMvxPluginConfiguration configuration)
        {

        }
    }

    public class MockLocationWatcherConfiguration : IMvxPluginConfiguration
    {
        public static readonly MockLocationWatcherConfiguration Default = new MockLocationWatcherConfiguration();

        // ideally, we should use this property to point to a file or string containing location data
        // this should be configurable outside of code base.
        public string SensorLocationData { get; set; }
    }
}

我想传递传感器数据,当前通过MockLocationWatcherConfiguration实例硬编码到名为“data”的变量中,但是在IMvxConfigurablePluginLoader.Configure(配置)之前不知道MvvmCross框架期望在何处加载此插件的配置被调用。理想情况下,我应该通过配置来指定它。

我查看了 Json plugin's implementation of PluginLoaded,但在IMvxConfigurablePluginLoader.Configure中尝试强制转换之前仍然无法确定配置的检索位置。

任何想法或指示将不胜感激。

TIA。

1 个答案:

答案 0 :(得分:0)

wiki页面https://github.com/slodge/MvvmCross/wiki/MvvmCross-plugins草案中对此进行了介绍 - 请参阅“编写可配置插件”