protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo) { // if we're not in design mode, recreate the image source for the new size if (DesignerProperties.GetIsInDesignMode(this) == false && graphicsService != null) { // recreate the image source imageSource.Dispose(); imageSource = new RenderTargetImageSource(GraphicsDevice, (int)ActualWidth, (int)ActualHeight); rootImage.Source = imageSource.WriteableBitmap; } base.OnRenderSizeChanged(sizeInfo); }
void UserControl_Loaded(object sender, RoutedEventArgs e) { // if we're not in design mode, initialize the graphics device if (DesignerProperties.GetIsInDesignMode(this) == false && graphicsService == null) { // add a reference to the graphics device graphicsService = GraphicsDeviceService.AddRef((PresentationSource.FromVisual(this) as HwndSource).Handle); // create the image source imageSource = new RenderTargetImageSource(GraphicsDevice, (int)ActualWidth, (int)ActualHeight); rootImage.Source = imageSource.WriteableBitmap; // hook the rendering event CompositionTarget.Rendering += CompositionTarget_Rendering; // Invoke the ControlLoaded event if (ControlLoaded != null) { ControlLoaded(this, new GraphicsDeviceEventArgs(graphicsService.GraphicsDevice)); } } }