We introduced and demonstrated two approaches to show a WPF window in a Revit Addin (Class Library type) project and each has its pros and cons. Is not there really a better and easier way to do such a common task?
I betted there does be so looked into it further.
As pointed out before, for a regular Class Library project like our Revit Addin ones, the Visual Studio IDE, specifically the Add New Item dialog, only allows a WPF User Control to be added to the project:
But for the Class Library project converted from a WPF Application one, all WPF templates will show up in the Add New Item dialog:
Surely it means we can add all these items into the Class Library project.
So what is the magic here?
It was found out the magic resides in the project file, like *.csproj for C#, through digging here and there.
In the our two test cases, the beginning of the WpfAppToRevitClass.csproj looks like:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{B9A6CBBD-71D9-4CE3-B57D-9811B97B091B}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>WpfAppToRevitClass</RootNamespace>
<AssemblyName>WpfAppToRevitClass</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<WarningLevel>4</WarningLevel>
<StartupObject>
</StartupObject>
</PropertyGroup>
And the RevitAddinCSWPF.csproj:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{20C08958-4FF2-4DDE-AD9E-C69D595414A1}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>RevitAddinCSWPF</RootNamespace>
<AssemblyName>RevitAddinCSWPF</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
Keen readers, have you observed anything different between the two .csproj c# project files?
Right, the ProjectTypeGuids node is it.
After the following node being put into the PropertyGroup of the above project,
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
The Add New Item dialog looks exactly the same as in the other project.
This time, we can add a WPF window (with the default name Windows1.xmal for example) to the RevitAddin project, and use the same code, as we introduced in the Converting WPF Application to Revit Class Library, to launch it inside Revit:
//TODO: add your code below.
Window1 wpfWin = new Window1();
if (wpfWin.ShowDialog() == false)
return Result.Cancelled;
So, the unofficial way proves the best since what we need to do is to add a small little tiny thing into the text-format project file itself rather than do all those tedious and error prone stuffs.
More articles about WPF and Revit Addin/API can be expected soon. Please stay tuned.
RevitAddinWizard may provide a few coders to address some such common situations in the new future. Please also check on its updates once in a while.
Recent Comments