ArcGIS Pro 2.8 API Reference Guide
CreateLegend Method (LayoutElementFactory)
Example 

ArcGIS.Desktop.Layouts Namespace > LayoutElementFactory Class : CreateLegend Method
Layout or GroupElement
Envelope
MapFrame
Creates a legend element on a layout from an envelope geometry. This method must be called on the MCT. Use QueuedTask.Run.
Syntax

Parameters

elementContainer
Layout or GroupElement
envelope
Envelope
mapFrame
MapFrame

Return Value

Returns a Legend.
Exceptions
ExceptionDescription
This method or property must be called within the lambda passed to QueuedTask.Run.
Map has reached maximum graphics count limit of 4000 elements. One or more elements cannot be created.
Map has reached maximum graphics size limit of 10 MB. One or more elements cannot be created.
Remarks
If the element container is a Layout then the element gets added to the root level of the layout TOC at the top most position. If the element container is a GroupElement then it gets added to the group at the topmost position.
Example
//Create a legend for an associated map frame.

//Construct on the worker thread
await QueuedTask.Run(() =>
{
  //Build 2D envelope geometry
  Coordinate2D leg_ll = new Coordinate2D(6, 2.5);
  Coordinate2D leg_ur = new Coordinate2D(8, 4.5);
  Envelope leg_env = EnvelopeBuilder.CreateEnvelope(leg_ll, leg_ur);

  //Reference MF, create legend and add to layout
  MapFrame mapFrame = layout.FindElement("New Map Frame") as MapFrame;
  if (mapFrame == null)
  {
    ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Map frame not found", "WARNING");
    return;
  }
  Legend legendElm = LayoutElementFactory.Instance.CreateLegend(layout, leg_env, mapFrame);
  legendElm.SetName("New Legend");
});
Requirements

Target Platforms: Windows 10, Windows 8.1

See Also

Reference

LayoutElementFactory Class
LayoutElementFactory Members