public void Initialize(CommonDX.DeviceManager deviceManager)
        {

            _deviceManager = deviceManager;
            _d2dContext = deviceManager.ContextDirect2D;

            _generalGrayColor = new SharpDX.Direct2D1.SolidColorBrush(_deviceManager.ContextDirect2D, Color.Gray);
            _generalRedColor = new SharpDX.Direct2D1.SolidColorBrush(_deviceManager.ContextDirect2D, Color.Red);
            _generalLightGrayColor = new SharpDX.Direct2D1.SolidColorBrush(_deviceManager.ContextDirect2D, Color.LightGray);
            _generalLightWhiteColor = new SharpDX.Direct2D1.SolidColorBrush(_deviceManager.ContextDirect2D, Color.White);

            _generalTextFormat = new SharpDX.DirectWrite.TextFormat(
                _deviceManager.FactoryDirectWrite,
                "Segoe UI",
                SharpDX.DirectWrite.FontWeight.Light,
                SharpDX.DirectWrite.FontStyle.Normal,
                SharpDX.DirectWrite.FontStretch.Normal,
                16f);

            _debugTextFormat = new SharpDX.DirectWrite.TextFormat(
                _deviceManager.FactoryDirectWrite,
                "Segoe UI",
                SharpDX.DirectWrite.FontWeight.Light,
                SharpDX.DirectWrite.FontStyle.Normal,
                SharpDX.DirectWrite.FontStretch.Normal,
                20f);

            _layoutDetail = new LayoutDetail() { Width = this.State.DrawingSurfaceWidth, Height = this.State.DrawingSurfaceHeight };
            _layoutDeviceScreenSize = new RectangleF(0, 0, (float)_layoutDetail.Width, (float)_layoutDetail.Height);
            _appWidth = (float)_layoutDetail.Width + 5;
            _appHeight = (float)_layoutDetail.Height;

            _updateScaleTranslate(1.0f);


            ////_drawTiles(0);
            if (State.DefaultBackgroundUri != string.Empty) { 
                _clearRenderTree();
                _changeBackgroundImpl(1, _appWidth, _appHeight, 0, 0, State.DefaultBackgroundFolder, State.DefaultBackgroundUri, "", Color.White, 0.7f, "", false);
            }

            //GestureService.OnGestureRaised += (o,a) => {
            //    CustomGestureArgs gestureArgs = (CustomGestureArgs)a;
            //    //NumberFramesToRender += 3;
            //    if (gestureArgs.ManipulationStartedArgs != null)
            //    {
            //        _isInertialTranslationStaging = false;
            //        _globalCameraTranslationStaging = _globalCameraTranslation;
            //    }
            //    else if (gestureArgs.ManipulationInertiaStartingArgs != null)
            //    {
            //        _isInertialTranslationStaging = true;
            //        _globalCameraTranslationStaging = _globalCameraTranslation;
            //    }
            //    else if (gestureArgs.ManipulationUpdatedArgs != null)
            //    {
            //        if(_isInertialTranslationStaging)
            //            _updateCameraTranslationStaging((float)gestureArgs.ManipulationUpdatedArgs.Velocities.Linear.X);
            //        else
            //            _updateCameraTranslationStaging((float)gestureArgs.ManipulationUpdatedArgs.Cumulative.Translation.X);
            //    }
            //    else if (gestureArgs.ManipulationCompletedArgs != null)
            //    {
            //        if (gestureArgs.ManipulationCompletedArgs.Cumulative.Scale < 1)
            //        {
            //            if (_globalScale.X != 0.9f) { _updateBackgroundTweener(1.0f, 0.9f, 1.2f); SendInformationNotification("zoom level at 90%", 3); }
            //            //_updateScaleTranslate(0.9f);
            //        }
            //        else if (gestureArgs.ManipulationCompletedArgs.Cumulative.Scale > 1)
            //        {
            //            if (_globalScale.X != 1.0f) { _updateBackgroundTweener(0.9f, 1.0f, 1.2f); SendInformationNotification("zoom level at 100%", 3); }
            //            //_updateScaleTranslate(1.0f);
            //        }

            //        _globalCameraTranslation = _globalCameraTranslation + _globalCameraTranslationStaging;
            //        _globalCameraTranslationStaging = Vector3.Zero;
            //        _isInertialTranslationStaging = false;

            //    }
            //    else if (gestureArgs.TappedEventArgs != null)
            //    {
            //        var x = gestureArgs.TappedEventArgs.Position.X - _globalCameraTranslation.X;
            //        var y = gestureArgs.TappedEventArgs.Position.Y - _globalCameraTranslation.Y;

            //        var found = _doTilesHitTest((float)x, (float)y);
            //        if (found != null && found.Count > 0)
            //        {
            //            _selectedRect = found[0];
            //        }
            //        else _selectedRect = null;
            //    }
            //};


            WindowLayoutService.OnWindowLayoutRaised += WindowLayoutService_OnWindowLayoutRaised;


        }
        ///// <summary>
        ///// Loads bitmap asynchronously and injects into global variables. I need to work out how to NOT make them global
        ///// </summary>
        ///// <param name="assetNativeUri"></param>
        ///// <returns></returns>
        //public async Task<bool> LoadAssetAsync(string assetNativeUri)
        //{
        //    var path = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;

        //    var storageFile = await Windows.Storage.StorageFile.GetFileFromPathAsync(assetNativeUri);

        //    Stream ms = await storageFile.OpenStreamForReadAsync();  //ras.GetResults().AsStreamForRead())
        //    //var data = SharpDX.IO.NativeFile.ReadAllBytes(assetNativeUri);
        //    //using (System.IO.MemoryStream ms = new System.IO.MemoryStream(data))
        //    {
        //        if (ms != null)
        //        {

        //            SharpDX.WIC.BitmapDecoder bitmapDecoder = new SharpDX.WIC.BitmapDecoder(
        //                                                                                        _deviceManager.WICFactory,
        //                                                                                        ms,
        //                                                                                        SharpDX.WIC.DecodeOptions.CacheOnDemand
        //                                                                                    );
        //            {


        //                SharpDX.WIC.BitmapFrameDecode bitmapFrameDecode = bitmapDecoder.GetFrame(0);
        //                {

        //                    SharpDX.WIC.BitmapSource bitmapSource = new SharpDX.WIC.BitmapSource(bitmapFrameDecode.NativePointer);
        //                    {

        //                        SharpDX.WIC.FormatConverter formatConverter = new SharpDX.WIC.FormatConverter(_deviceManager.WICFactory);
        //                        //formatConverter.Initialize( bitmapSource, SharpDX.WIC.PixelFormat.Format32bppBGRA);
        //                        formatConverter.Initialize(
        //                            bitmapSource,
        //                            SharpDX.WIC.PixelFormat.Format32bppBGRA,
        //                            SharpDX.WIC.BitmapDitherType.None,
        //                            null,
        //                            0.0f,
        //                            SharpDX.WIC.BitmapPaletteType.Custom
        //                            );

        //                        _backgroundImageSize = formatConverter.Size;

        //                        _backgroundImageFormatConverter = formatConverter;

        //                        return true;
        //                    }

        //                }

        //            }

        //        }
        //    }

        //    //ras.Close();

        //    _backgroundImageFormatConverter = null;
        //    _backgroundImageSize = new Size2(0, 0);

        //    return false;
        //}



        /// <summary>
        /// Used to update an existing bitmap effect with a new image
        /// </summary>
        /// <param name="assetUri"></param>
        /// <param name="aggregateId"></param>
        private async void UpdateImageByUri(UIElementState uistate, LayoutDetail layoutDetail, bool resetTransforms = true)
        {
            try
            {
                //string assetUri, string aggregateId
                var found = _renderTree.Where(x => x.Type == eRenderType.Effect && x.EffectDTO.AggregateId == uistate.AggregateId).FirstOrDefault();
                if (found != null)
                {
                    var ret = await LoadAssetAsync(_deviceManager.WICFactory, uistate.udfString1, uistate.AggregateId);
                    //LoadLocalNativeAsset(assetUri, out backgroundImageFormatConverter, out backgroundImageSize);

                    if (ret!=null)
                    {
                        found.EffectDTO.Effect.SetValueByName("WicBitmapSource", _backgroundImageFormatConverter);
                        _backgroundImageFormatConverter.Dispose();
                        _backgroundImageFormatConverter = null;


                        //double newRatio = (_backgroundImageSize.Width > Window.Current.Bounds.Width) ? (Window.Current.Bounds.Width/_backgroundImageSize.Width) : 1d;
                        double newRatio = (_backgroundImageSize.Width > layoutDetail.Width) ? (layoutDetail.Width / _backgroundImageSize.Width) : 1d;

                        AppDatabase.Current.UpdateUIElementStateField(uistate.AggregateId, "udfBool1", false, sendAggregateUpdateMessage: false);

                        if (resetTransforms) AppDatabase.Current.AddUpdateUIElementState(uistate.AggregateId, State.SelectedScene.AggregateId, 0, 0, _backgroundImageSize.Width, _backgroundImageSize.Height, newRatio, true, null, null, false);
                        else AppDatabase.Current.AddUpdateUIElementState(uistate.AggregateId, State.SelectedScene.AggregateId, uistate.Left, uistate.Top, _backgroundImageSize.Width, _backgroundImageSize.Height, uistate.Scale, true, null, null, false);


                        NumberFramesToRender = 3;

                    }
                }

                //TurnOnRenderingBecauseThereAreRenderableEffects();
            }
            catch (Exception ex) { 
            
            }
        }