Skip to content

Commit

Permalink
Fix crash in XAML design mode (#7)
Browse files Browse the repository at this point in the history
* Add xaml design mode check

* updated packages

* Add IsDesignMode property for all controls

* make IsDesignMode as inline method.

* recovery xaml file to preview commit

* modify grammar
  • Loading branch information
MikiraSora authored Sep 2, 2020
1 parent 488ee89 commit f498205
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
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

0 comments on commit f498205

Please sign in to comment.