Skip to content

Instantly share code, notes, and snippets.

@EricEzaM
Created July 19, 2022 09:41
Show Gist options
  • Save EricEzaM/5797be1f4b28f15e9be53287a02d3d67 to your computer and use it in GitHub Desktop.
Save EricEzaM/5797be1f4b28f15e9be53287a02d3d67 to your computer and use it in GitHub Desktop.
Stride3D embed in WPF xaml control
<UserControl x:Class="Namespace.StrideView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Namespace"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<ContentPresenter x:Name="SceneView">
</ContentPresenter>
</Grid>
</UserControl>
public partial class StrideView : UserControl
{
private Thread gameThread;
private readonly TaskCompletionSource<bool> gameStartedTaskSource = new TaskCompletionSource<bool>();
private IntPtr windowHandle;
public StrideView()
{
InitializeComponent();
gameThread = new Thread(SafeAction.Wrap(GameRunThread))
{
IsBackground = true,
Name = "Game Thread"
};
gameThread.SetApartmentState(ApartmentState.STA);
Loaded += (sender, args) =>
{
StartGame();
};
}
private async Task StartGame()
{
gameThread.Start();
await gameStartedTaskSource.Task;
SceneView.Content = new GameEngineHost(windowHandle);
}
private void GameRunThread()
{
// Create the form from this thread
// EmbeddedGameForm is in Stride.Editor. You may need to copy this class to your own project.
var form = new EmbeddedGameForm()
{
TopLevel = false,
Visible = false
};
windowHandle = form.Handle;
var context = new GameContextWinforms(form);
gameStartedTaskSource.SetResult(true);
var game = new Game();
game.Run(context, scene =>
{
game.Window.IsBorderLess = true;
game.SetupBase3DScene();
});
}
}
@blair-ahlquist
Copy link

blair-ahlquist commented Aug 29, 2023

This code has an implicit dependency on extension methods in this repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment