Skip to content

Commit df2fb52

Browse files
committed
Completed Automate Project Information
1 parent 51132b1 commit df2fb52

19 files changed

+233
-1
lines changed

Docs/Guidance/CreateProject/AutomateProjectInformation.md

Lines changed: 233 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,20 +247,252 @@ projectFiles = files.Count();
247247
```
248248
Updated code will look like this.
249249
![](Images/ExecuteCommandAsyncFileCount.png)
250-
250+
___
251251
## Build Visual Studio Dialog
252+
Now that we have the project data that we need, we will then focus on building a dialog to display this information in Visual Studio.
253+
254+
1. From solution explorer we are going to do the following.
255+
256+
![](Images/DialogFolderAddNewItem.png)
257+
- Right click on the Dialog folder under the project folder
258+
- From the context menu select Add
259+
- From the sub context menu select New Item...
260+
___
261+
2. From the Add New Item dialog do the following
262+
263+
![](Images/NewProjectInformationUserControl.png)
264+
265+
- Under Visual C# Items select CodeFactory
266+
- Select Visual Studio User Control
267+
- In the name field enter ProjectInformationUserControl.xaml
268+
- Click Add
269+
___
270+
3. Now that the user control has been created it will open the user control xaml file.
271+
You will see the default markup for the user control itself.
272+
It will look like this.
273+
274+
![](Images/DefaultXamlDialog.png)
275+
___
276+
4. We are going to set the window title and set the target size of the dialog.
277+
We do this by updating the market up for the window title.
278+
We also replace the Design height and width.
279+
We set the height to 175 pixels and the width to 400 pixels.
280+
281+
```
282+
WindowTitle="Project Information"
283+
Height="175"
284+
Width="400">
285+
```
286+
The updated markup will look like this.
287+
![](Images/DialogTitleAndSize.png)
288+
___
289+
5. After we have setup our dialog and given it a title we are going to use a grid layout to format the placement of our data.
290+
Since we are working with 3 elements of data we will create a grid with 5 rows and a total 3 columns.
291+
292+
```
293+
<Grid.ColumnDefinitions>
294+
<ColumnDefinition Width="20"/>
295+
<ColumnDefinition Width="150"/>
296+
<ColumnDefinition/>
297+
</Grid.ColumnDefinitions>
298+
<Grid.RowDefinitions>
299+
<RowDefinition Height="20"/>
300+
<RowDefinition Height="30"/>
301+
<RowDefinition Height="30"/>
302+
<RowDefinition Height="30"/>
303+
<RowDefinition />
304+
</Grid.RowDefinitions>
305+
```
306+
The updated markup will look like this.
307+
![](Images/DialogGridFormatting.png)
308+
___
309+
6. Once the layout has been setup we will add label and text box controls for each piece of project data that is being added to the project.
310+
311+
```
312+
<Label Name="LabelProjectName" Grid.Row="1" Grid.Column="1" Content="Project Name:" HorizontalAlignment="Right" Margin="0,0,5,0"/>
313+
<TextBox Name="TextBoxProjectName" Grid.Row="1" Grid.Column="2" Margin="5" IsEnabled="False"/>
314+
<Label Name="LabelProjectReferences" Grid.Row="2" Grid.Column="1" Content="Project Reference Count:" HorizontalAlignment="Right" Margin="0,0,5,0"/>
315+
<TextBox Name="TextBoxProjectReferences" Grid.Row="2" Grid.Column="2" Margin="5" Width="40" HorizontalAlignment="Left" IsEnabled="False"/>
316+
<Label Name="LabelProjectFiles" Grid.Row="3" Grid.Column="1" Content="Project File Count:" HorizontalAlignment="Right" Margin="0,0,5,0"/>
317+
<TextBox Name="TextBoxProjectFiles" Grid.Row="3" Grid.Column="2" Margin="5" Width="40" HorizontalAlignment="Left" IsEnabled="False"/>
318+
```
319+
The updated markup will look like this.
320+
![](images/DialogDataControls.png)
321+
___
322+
7. Next we wire up the button and register a method to handle the click event.
323+
```
324+
<Button Name="ButtonOk" Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="3" Margin="125,15" Content="Ok" Click="ButtonOk_OnClick"/>
325+
```
326+
Note in the IDE when you enter the click attribute it will prompt you to create a new method let it create the method for you.
252327

328+
The updated markup will look like this.
253329

330+
![](Images/DialogButton.png)
331+
332+
The final layout of the user control will look like this.
333+
334+
![](Images/DialogLayout.png)
335+
___
336+
8. Now that the layout is complete we will access the code behind file for the user control and make some changes.
337+
To do this do the following.
338+
339+
![](Images/DialogSelectCodeBehind.png)
340+
341+
- Click the expander next to the xaml document of the user control it self.
342+
- Select the project file with the same name of the user control ending in .cs
343+
- This will will open the code file for edit.
344+
___
345+
9. After the code behind file has been opened we are going update the ok button click event method to close the dialog itself.
346+
Those familar with WPF will know there is no close method for a user control.
347+
This close event is part of the CodeFactory framework and will inform Visual Studio to close the dialog window.
348+
The following code will be added to close the dialog when clicked.
349+
350+
Before:
351+
```
352+
throw new NotImplementedException();
353+
```
354+
355+
After:
356+
```
357+
this.Close();
358+
```
359+
360+
The updated source code will look like this.
361+
362+
![](Images/ButtonClose.png)
363+
___
364+
10. The final thing that is needed on the dialog is to be able to pass the project data to the user control.
365+
We will create a public facing method that will take in the three pieces of information and directly set each control individually.
366+
367+
Those that are familar with WPF would probably used dependency properties.
368+
For this example we are keeping it simple for people that are not familar with WPF.
369+
370+
The following method is added to the code behind to set the data.
371+
```
372+
/// <summary>
373+
/// Sets the project information to be displayed in the dialog
374+
/// </summary>
375+
/// <param name="projectName">The name of the project hosted in the solution.</param>
376+
/// <param name="fileCount">The number of files hosted in the project.</param>
377+
/// <param name="referenceCount">The number of references in the project.</param>
378+
public void SetProjectInformation(string projectName, int fileCount, int referenceCount)
379+
{
380+
this.TextBoxProjectName.Text = projectName;
381+
this.TextBoxProjectFiles.Text = fileCount.ToString();
382+
this.TextBoxProjectReferences.Text = referenceCount.ToString();
383+
}
384+
385+
```
386+
___
254387
## Create and Display Dialog
388+
Once we have created a dialog and formatted it. Next we will need to create an instance of the dialog user control.
389+
Then pass the data that has been collected on the project and display it.
390+
391+
1. We navigate back to the ProjectInformationCommand code file.
392+
In the ExecuteCommandAsync method we are going to create an instance of the user control.
393+
394+
When working with the user interface we have to make a CodeFactory API call to make the instance of the user control.
395+
Behind the covers CodeFactory wraps the defined user control in a WPF dialog window used by Visual Studio.
396+
397+
In order to access the user interface you have to use a Visual Studio Action.
398+
All commands have direct access to all Visual Studio Actions managed by CodeFactory via a property on the command base class.
399+
400+
In the following we call ther user interface actions and create a instance of our user control and return it to the var dialog.
401+
402+
```
403+
var dialog = await VisualStudioActions.UserInterfaceActions
404+
.CreateVsUserControlAsync<ProjectInformationUserControl>();
405+
```
406+
The updated code looks like the following.
407+
408+
![](Images/ExecuteCommandAsyncCreateUserControl.png)
409+
___
410+
2. Once we have created an instance of dialog we will need to pass the project information to the dialog.
411+
We will call the public method we created and pass our project data to the dialog.
412+
413+
```
414+
dialog.SetProjectInformation(projectName,projectFiles,projectReferences);
415+
```
416+
The updated code looks like the following.
417+
418+
![](Images/ExecuteCommandAsyncSetDataOnDialog.png)
419+
___
420+
3. Now that our data has been set we are ready to display the dialog itself.
421+
Once again we will make a CodeFactory API call and display the dialog in Visual Studio.
422+
423+
```
424+
await VisualStudioActions.UserInterfaceActions.ShowDialogWindowAsync(dialog);
425+
```
426+
The updated code looks like the following.
427+
![](Images/ExecuteCommandAsyncShowDialog.png)
428+
___
429+
430+
## Test Completed Command
431+
Now the the project information command has been completed, we will use the debugger to test the logic.
432+
433+
1. Click Start Debugging this will be the play button icon on your tool bar with star next to it.
434+
435+
![](Images/StartDebugging.png)
436+
437+
You will notice that will start a new instance of Visual Studio.
438+
This is by design CodeFactory automation runs inside of Visual Studio so we debug it from another instance.
439+
___
440+
2. One the debugger version of Visual Studio loads do the following.
441+
442+
![](Images/OpenCodeFactoryTesting.png)
443+
444+
- Click File
445+
- From the menu select Recent Projects and Solutions
446+
- From the sub menu select CodeFactoryTesting.sln
447+
This will load our testing solution.
448+
449+
During the loading of the solution a number of services are loading.
450+
When in debugger mode this can be slow.
451+
You will know when your code factory logic is loaded by see this message in the lower left hand corner.
452+
453+
![](Images/CommandsLoaded.png)
454+
___
455+
3. Next we execute the project information command and test our logic by doing the following.
456+
457+
![](Images/RunShowProjectInformation.png)
458+
459+
- Right click on the project in the solution
460+
- From the context menu click Show Project Infomation
461+
462+
Visual Studio will then display the dialog with the information about the project.
463+
464+
![](Images/ProjectInfoDialogWithData.png)
465+
466+
- Click Ok to close the dialog
467+
___
468+
4. Play with the project
469+
Make changes to the project itself, add code files.
470+
Maybe add a project reference and then rerun the command the dialog will show the updated information.
471+
472+
Since this is in debug mode, you can go back to the hosting copy of Visual Studio and set break points.
473+
You can then step through the code and see how everthing executes.
474+
475+
Once you are done close the debug version of Visual Studio.
476+
___
477+
## Deploying the Automation to Your Solutions
478+
Every time you build your project the CodeFactory SDK calls an external executable called CFXPackager.
479+
This packages up your automation for using in solutions.
480+
481+
In the bin folder of your project is a file with the name of your project with a cfx file extension.
482+
![](Images/cfxLocation.png)
483+
484+
You copy the cfx file into the solution folder of your target solution you want to use with automation and CodeFactory will load it when the solution opens.
255485

256486

257487
## Return to Guidance
258488
This concludes the complete training for how to create a CodeFactory project. The link below will take you back to the guidance page.
259489

260490
[CodeFactory Guidance](../Overview.md)
261491

492+
262493
## Return To Create Project Guidance
263494
The link below will take you back to the create project guidance.
264495

265496
[Create Project Guidance](Overview.md)
266497

498+
7.12 KB
Loading
2.11 KB
Loading
33 KB
Loading
107 KB
Loading
102 KB
Loading
63.2 KB
Loading
57.8 KB
Loading
5.67 KB
Loading
8.26 KB
Loading

0 commit comments

Comments
 (0)