Exemple #1
0
        public void OnResize(int width, int height)
        {
            if (mRealTexture != null)
            {
                mRealTexture.Dispose();
            }
            if (mTmpTexture != null)
            {
                mTmpTexture.Dispose();
            }

            mRealTexture = new SharpDX.Direct3D11.Texture2D(mDevice.Device, new SharpDX.Direct3D11.Texture2DDescription
            {
                ArraySize         = 1,
                BindFlags         = SharpDX.Direct3D11.BindFlags.RenderTarget | SharpDX.Direct3D11.BindFlags.ShaderResource,
                CpuAccessFlags    = SharpDX.Direct3D11.CpuAccessFlags.None,
                Format            = Format.B8G8R8A8_UNorm,
                Height            = height,
                Width             = width,
                MipLevels         = 1,
                OptionFlags       = SharpDX.Direct3D11.ResourceOptionFlags.SharedKeyedmutex,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = SharpDX.Direct3D11.ResourceUsage.Default
            });

            using (var resource = mRealTexture.QueryInterface <SharpDX.DXGI.Resource>())
                mTmpTexture = D2DDevice.OpenSharedResource <Texture2D>(resource.SharedHandle);

            if (NativeView != null)
            {
                NativeView.Dispose();
            }
            NativeView = new SharpDX.Direct3D11.ShaderResourceView(mDevice.Device, mRealTexture,
                                                                   new SharpDX.Direct3D11.ShaderResourceViewDescription
            {
                Format    = Format.B8G8R8A8_UNorm,
                Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D,
                Texture2D = new SharpDX.Direct3D11.ShaderResourceViewDescription.Texture2DResource
                {
                    MipLevels       = 1,
                    MostDetailedMip = 0
                }
            });

            if (RenderTarget != null)
            {
                RenderTarget.Dispose();
            }
            using (var surface = mTmpTexture.QueryInterface <Surface>())
                RenderTarget = new RenderTarget(Direct2DFactory, surface, new RenderTargetProperties()
                {
                    DpiX        = 0.0f,
                    DpiY        = 0.0f,
                    MinLevel    = SharpDX.Direct2D1.FeatureLevel.Level_DEFAULT,
                    PixelFormat = new PixelFormat()
                    {
                        AlphaMode = AlphaMode.Premultiplied, Format = Format.Unknown
                    },
                    Type  = RenderTargetType.Hardware,
                    Usage = RenderTargetUsage.None
                });

            if (mMutex10 != null)
            {
                mMutex10.Dispose();
            }
            if (mMutex11 != null)
            {
                mMutex11.Dispose();
            }

            mMutex10 = mTmpTexture.QueryInterface <KeyedMutex>();
            mMutex11 = mRealTexture.QueryInterface <KeyedMutex>();

            Brushes.Initialize(RenderTarget);
            Fonts.Initialize(DirectWriteFactory);

            Button.Initialize();
            Frame.Initialize();

            // right now the texture is unowned and only a key of 0 will succeed.
            // after releasing it with a specific key said key then can be used for
            // further locking.
            mMutex10.Acquire(0, -1);
            mMutex10.Release(Key11);
        }
        /// ------------------------------------------------------------------------------------------------
        ///
        protected override void OnElementChanged(Xamarin.Forms.Platform.iOS.ElementChangedEventArgs <View> e)
        {
            try
            {
                base.OnElementChanged(e);
                if (e.OldElement != null)
                {
                    if (formsMap == null)
                    {
                        formsMap = (CustomMap)e.NewElement;
                    }
                    this.MapViewInstance().GetViewForAnnotation = null;
                    customPins.Clear();
                }
                if (e.NewElement != null)
                {
                    if (formsMap == null)
                    {
                        formsMap = (CustomMap)e.NewElement;
                    }
                    var customView = Control as CustomMKAnnotationView;

                    if (Control != null)
                    {
                        Control.RemoveGestureRecognizer(_tapRecogniser);
                    }
                    base.OnElementChanged(e);
                    if (Control != null)
                    {
                        Control.AddGestureRecognizer(_tapRecogniser);
                    }

                    if (customPins == null)
                    {
                        customPins = new List <CustomPin>();
                    }
                    else if (customPins != null && customPins.Count != 0)
                    {
                        customPins.Clear();
                    }

                    var singlePin = MapViewModel.GetCurrentPin();
                    if (singlePin != null)
                    {
                        formsMap.Pins.Add(singlePin.Pin);
                        customPins.Add(singlePin);
                    }
                    formsMap.ClearPins = () =>
                    {
                        if (customPins != null)
                        {
                            formsMap?.Pins.Clear();
                            customPins.Clear();
                        }
                    };
                    formsMap.LoadPins = Pins =>
                    {
                        if (Pins != null)
                        {
                            customPins.Clear();
                            formsMap.Pins.Clear();
                            foreach (var pin in Pins)
                            {
                                formsMap.CustomPins = Pins;
                                formsMap.Pins.Add(pin.Pin);
                                customPins.Add(pin);
                            }
                        }
                    };
                    this.MapViewInstance().GetViewForAnnotation = GetViewForAnnotation;
                    formsMap.DisposeMapControl = () =>
                    {
                        try
                        {
                            //
                            annotationView?.RemoveFromSuperview();
                            annotationView?.Dispose();
                            //
                            _nativeMap?.RemoveFromSuperview();
                            _nativeMap?.Delegate?.Dispose();
                            _nativeMap.Delegate = null;
                            _nativeMap?.Dispose();
                            //
                            NativeView?.RemoveFromSuperview();
                            NativeView?.Dispose();
                            //
                            formsMap   = null;
                            customPins = null;
                            singlePin  = null;
                            //
                            customView?.RemoveFromSuperview();
                            customView?.Dispose();
                            //
                            Control?.RemoveFromSuperview();
                            Control?.Dispose();
                            //
                        }
                        catch (ObjectDisposedException ex)
                        {
                        }
                        catch (Exception ex)
                        {
                        }
                    };
                }
            }
            catch (Exception ex)
            {
            }
        }