Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash in XAML design mode #7

Merged
merged 6 commits into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion OpenTkControl/OpenTkControl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="OpenTK, Version=2.0.0.0, Culture=neutral, PublicKeyToken=bad199fe84eb3df4, processorArchitecture=MSIL">
<HintPath>..\packages\OpenTK.2.0.0\lib\net20\OpenTK.dll</HintPath>
<HintPath>..\..\packages\OpenTK.2.0.0\lib\net20\OpenTK.dll</HintPath>
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
Expand Down
12 changes: 12 additions & 0 deletions OpenTkControl/OpenTkControlBase.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
Expand Down Expand Up @@ -305,6 +306,12 @@ public Task RequestRepaint()
return tcs.Task;
}

/// <summary>
/// Check if it is run in designer mode.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected bool IsDesignMode() => DesignerProperties.GetIsInDesignMode(this);

/// <summary>
/// Renders a screenshot of the frame with the specified dimensions. It will be in bgra format with
/// [0,0] at the bottom left corner. Note that this is not meant for taking screenshots of what is
Expand Down Expand Up @@ -334,6 +341,11 @@ public Task<uint[,]> Screenshot(int width = 0, int height = 0)
/// <param name="args">Information about the event</param>
protected virtual void OnLoaded(object sender, RoutedEventArgs args)
{
#if DEBUG
if (IsDesignMode())
return;
#endif

_windowInfo = Utilities.CreateWindowsWindowInfo(
new WindowInteropHelper(Window.GetWindow(this)).Handle);
}
Expand Down
7 changes: 7 additions & 0 deletions OpenTkControl/ThreadOpenTkControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ protected override void OnLoaded(object sender, RoutedEventArgs args)
base.OnLoaded(sender, args);

_endThreadCts = new CancellationTokenSource();

_renderThread = new Thread(RenderThread)
{
IsBackground = true,
Expand Down Expand Up @@ -89,6 +90,12 @@ private void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArg
/// <param name="boxedToken"></param>
private void RenderThread(object boxedToken)
{
#if DEBUG
// Don't render in design mode to prevent errors from calling OpenGL API methods.
if (Dispatcher.Invoke(() => IsDesignMode()))
return;
#endif

CancellationToken token = (CancellationToken) boxedToken;

InitOpenGl();
Expand Down
7 changes: 7 additions & 0 deletions OpenTkControl/UiOpenTkControl.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
Expand Down Expand Up @@ -47,6 +48,12 @@ protected override void OnUnloaded(object sender, RoutedEventArgs routedEventArg
/// <param name="args">The event arguments about this event</param>
private void CompositionTargetOnRendering(object sender, EventArgs args)
{
#if DEBUG
//We needn't call render() for avoiding crash by calling OpenGL API methods.
if (IsDesignMode())
return;
#endif

DateTime now = DateTime.Now;
if ((_continuous && now > _nextRenderTime) || ManualRepaintEvent.WaitOne(0))
{
Expand Down