I am new to AddOn Development. I am trying to add a button on the chartTrader panel. I followed the instructions on the documentation page for AddOns to do this but it doesn't work perfectly. When I recompile Ninjascript, there is some undefined behaviour (I see two buttons sometimes, sometimes Ninja throws an Error "The calling thread cannot access this object because a different thread owns it"). What is the right way to create and destroy a button / or any resource for that matter when developing addons. I have attached my code.
public class DataSenderAddon : NinjaTrader.NinjaScript.AddOnBase
{
private Gui.Chart.Chart currentChart;
private Gui.Chart.ChartTrader currentChartTrader;
private Grid currentChartTraderGrid, myGrid;
private RowDefinition rowDefinitionsMyGrid;
private Button sampleButton;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Add on here.";
Name = "DataSenderAddon";
}
else if (State == State.Configure)
{
}
}
protected override void OnWindowCreated(Window window)
{
try
{
currentChart = window as Chart;
if (currentChart == null)
return;
// currentChartTrader = currentChart.ChartTrader;
currentChartTrader = Window.GetWindow(currentChart.ActiveChartControl.Parent).FindFirst("ChartWindowChartTraderControl") as ChartTrader;
if (currentChartTrader == null)
return;
// Get ChartTrader Grid
currentChartTraderGrid = currentChartTrader.FindName("grdMain") as Grid;
if (currentChartTraderGrid == null)
return;
Output.Process($"{currentChartTraderGrid.RowDefinitions.Count}", PrintTo.OutputTab1);
// Add row to Chart Trader Grid
rowDefinitionsMyGrid = new RowDefinition();
currentChartTraderGrid.RowDefinitions.Add(rowDefinitionsMyGrid);
Output.Process($"{currentChartTraderGrid.RowDefinitions.Count}", PrintTo.OutputTab1);
// Make Grid
myGrid = new Grid
{
Background = Brushes.Transparent,
Name = "ButtonsGrid",
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
};
myGrid.RowDefinitions.Add(new RowDefinition());
myGrid.ColumnDefinitions.Add(new ColumnDefinition());
sampleButton = new Button
{
Name = "enableBotButton",
Content = "Click Me",
ToolTip = "Click Me Button",
Foreground = Brushes.White,
Background = Brushes.Green,
Margin = new System.Windows.Thickness(5, 10, 5, 10),
FontSize = 12,
Opacity = 0.7,
Cursor = System.Windows.Input.Cursors.Hand
};
sampleButton.Click += OnClick;
myGrid.Children.Add(sampleButton);
Grid.SetRow(sampleButton, 1);
Grid.SetRow(sampleButton, 1);
currentChartTraderGrid.Children.Add(myGrid);
Grid.SetRow(myGrid, currentChartTraderGrid.RowDefinitions.Count);
}
catch(Exception e)
{
Output.Process($"{e.Message}", PrintTo.OutputTab1);
}
}
protected void OnClick(object sender, RoutedEventArgs rea)
{
if (sender == sampleButton)
Output.Process($"Clicked", PrintTo.OutputTab1);
}
protected override void OnWindowDestroyed(Window window)
{
if(!(window is Chart))
{
return;
}
myGrid.Children.Remove(sampleButton);
sampleButton.Click -= OnClick;
sampleButton = null;
currentChartTraderGrid.Children.Remove(myGrid);
myGrid = null;
Output.Process($"Here", PrintTo.OutputTab1);
currentChartTraderGrid.RowDefinitions.Remove(rowDefinitionsMyGrid);
Output.Process($"{currentChartTraderGrid.RowDefinitions.Count}", PrintTo.OutputTab1);
}
}

Comment