DirectX Error on Workspace Switch – D2DERR_WRONG_RESOURCE_DOMAIN from OnRender, despite SharpDX best practices

Create SimpleFont public property to be used for TextFormat.
TextFormat can be declared and assigned once, then re-used.
TextLayout not so much. I would recommend using() statement.

While you do not need a TextLayout to just render a DrawText.
Easier to grab the height and length of string from Metrics.

	public class HTBlank : Indicator
	{
		private bool 	isConfigSet	= false;
		private string 	myString 	= "Test String";
		private SharpDX.DirectWrite.TextFormat myTextFormat;
		
		protected override void OnStateChange()
		{
			if (State == State.SetDefaults)
			{
				Description					= @"";
				Name						= "HTBlank";
				Calculate					= Calculate.OnBarClose;
				IsOverlay					= true;
				PaintPriceMarkers			= false;
				ScaleJustification			= NinjaTrader.Gui.Chart.ScaleJustification.Right;
				IsSuspendedWhileInactive	= true;
				
				MyStroke					= new Stroke(System.Windows.Media.Brushes.Red, NinjaTrader.Gui.DashStyleHelper.Dash, 2f, 100);
				MySimpleFont		 		= new NinjaTrader.Gui.Tools.SimpleFont("Arial", 16) { Size = 16, Bold = true };
			}
			else if (State == State.Configure)
			{
				isConfigSet = true;
				myTextFormat = MySimpleFont.ToDirectWriteTextFormat();
				myTextFormat.TextAlignment = SharpDX.DirectWrite.TextAlignment.Leading;
			}
			else if (State == State.Terminated)
			{
				if(!isConfigSet) return;
				
				if(myTextFormat != null)
				{
					myTextFormat.Dispose();
					myTextFormat = null;
				}
			}
		}
		
		protected override void OnBarUpdate() { }
		
		
		public override void OnRenderTargetChanged()
		{
		   if (RenderTarget != null) MyStroke.RenderTarget = RenderTarget;
		}
		
		
	   	protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
        {
			if(IsInHitTest || RenderTarget == null || ChartPanel == null || myTextFormat == null || MyStroke == null) return; 
	        base.OnRender(chartControl, chartScale);
			
			//DrawLine
			float y = (ChartPanel.H - ChartPanel.Y) / 2f;
			RenderTarget.DrawLine(new SharpDX.Vector2(ChartPanel.X, y), new SharpDX.Vector2(ChartPanel.W, y), MyStroke.BrushDX, MyStroke.Width, MyStroke.StrokeStyle);
			
			//DrawText
			float x = (ChartPanel.W - ChartPanel.X) / 2f;
			RenderTarget.DrawText(myString, myTextFormat, new SharpDX.RectangleF(x, y, 250f, 50f ), MyStroke.BrushDX);
			
			//DrawTextLayout
			using(SharpDX.DirectWrite.TextLayout myTextLayout = new SharpDX.DirectWrite.TextLayout(Core.Globals.DirectWriteFactory, myString, myTextFormat, 250f, 50f))
			{
				RenderTarget.DrawTextLayout(new SharpDX.Vector2(x - myTextLayout.Metrics.Width/2, y - myTextLayout.Metrics.Height), myTextLayout, MyStroke.BrushDX);
			}
		}		
		
		
		[Display(Name="MyStoke", Description=@"MyStoke", Order=0, GroupName="Settings")]
		public Stroke MyStroke { get; set; }
	
		[Display(Name="MySimpleFont", Description=@"MySimpleFont", Order=1, GroupName="Settings")]
		public SimpleFont MySimpleFont { get; set; }
		
		
	}

Be Safe in this Crazy World!
:smiling_face_with_sunglasses:

1 Like