We have already done most of the development work in the [PreviewHandlerCommon](./common/cominterop/IPreviewHandler.cs) common project. To add a preview for the file type of .xyz:
- Add a new .NET project in the preview pane folder.
- Add a reference to the `PreviewHandlerCommon` common project.
- Create your preview handler class and extend the FileBasedPreviewHandler class. See an example below:
```csharp
using System;
using System.Runtime.InteropServices;
using Common;
namespace XYZPreviewHandler
{
/// <summary>
/// Implementation of preview handler for .xyz files.
/// GUID = CLSID / CLASS ID.
/// </summary>
[Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx")]
[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
public class XYZPreviewHandler : FileBasedPreviewHandler
this.xyzPreviewHandlerControl = new xyzPreviewHandlerControl();
return this.xyzPreviewHandlerControl;
}
}
}
```
Create a separate Preview Handler Control class and extend the `FormHandlerControl` Class.
```csharp
using Common;
namespace XYZPreviewHandler
{
public class XYZPreviewHandlerControl : FormHandlerControl
{
public XYZPreviewHandlerControl()
{
// ... do your initializations here.
}
public override void DoPreview<T>(T dataSource)
{
// ... add your preview rendering code here.
}
}
}
```
#### Integrate the Preview Handler into PowerToys Settings:
Navigate to the [powerpreview](../previewpane/powerpreview/powerpreview.h) project and edit the `powerpreview.h` file. Add the following Settings Object instance to `m_previewHandlers` settings objects array in the constructor initialization:
```cpp
// XYZ Preview Handler Settings Object.
FileExplorerPreviewSettings(
false,
L"<--YOUR_TOGGLE_CONTROL_ID-->",
L"<--Adescriptionofyourpreviewhandler-->",
L"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx", // your preview handler CLSID.
To add a new Previewer update the `Product.wxs` file in `PowerToysSetup` similar to existing Previewer to register the Preview Handler. More details about registration of Preview Handlers can be [found here.](https://docs.microsoft.com/en-us/windows/win32/shell/how-to-register-a-preview-handler)
**[Important] This method of registering Preview Handler DLL's is not recommended. It could lead to registry corruption.**
#### Registering Preview Handler
1. Restart Visual studio as administrator.
2. Sign `XYZPreviewHandler` and it's dependencies. To sign an assembly in VS, follow steps given [here](https://docs.microsoft.com/en-us/dotnet/standard/assembly/sign-strong-name#create-and-sign-an-assembly-with-a-strong-name-by-using-visual-studio).
3. Build `XYZPreviewHandler` project.
4. Open developer command prompt from `Tools > Command Line > Developer Command Prompt`.
5. Run following command for each nuget and project dependency to add them to Global Assembly Cache(GAC).
```
gacutil -i <pathtodependency>
```
6. Run following commands to register preview handler.
Since in-process preview handlers run under a surrogate hosting process (prevhost.exe by default), to debug a preview handler, you need to attach the debugger to the host process.
1. Click on a file with registered extension to start host process.
2. Attach debugger in Visual studio from `Debug->Attach to Process` and select `prevhost.exe` with type `Managed(version), x64`.
After successful integration, your preview handler should appear in the PowerToys settings UI under the `File Explorer Preview` Tab. In here you should be able to enable and disable all the preview handles.