The key is that when you create your code is that many of your commands are asyn. This means they need to be in a async method and the method has to be public to avoid CalledOnWrongThreadException. For example, you could have a call in your 'regular' synchronous code to LoadTemplateProject, that then calls your async method.
public static void LoadTemplateProject(string myTemplatePath)
//Load the requested map template
var projectFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments).ToString() + @"\ArcGIS\Projects";
CreateProjectSettings ps = new CreateProjectSettings()
{
CreateNewProjectFolder = true,
LocationPath = projectFolder,
TemplatePath = myTemplatePath,
TemplateType = TemplateType.LocalScene
};
LoadNewProject(ps);
}
public static async void LoadNewProject(CreateProjectSettings ps2)
{
//load the newly defined project; must be public to avoid threading errors!
try
{
Project thePrj = await Project.CreateAsync(ps2);
await Project.OpenAsync(thePrj.URI.ToString());
}
catch (Exception eX)
{
MessageBox.Show(eX.Message);
}
}