Skip to content

Commit f498205

Browse files
authored
Fix crash in XAML design mode (#7)
* 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
1 parent 488ee89 commit f498205

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

OpenTkControl/OpenTkControl.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
</PropertyGroup>
3535
<ItemGroup>
3636
<Reference Include="OpenTK, Version=2.0.0.0, Culture=neutral, PublicKeyToken=bad199fe84eb3df4, processorArchitecture=MSIL">
37-
<HintPath>..\packages\OpenTK.2.0.0\lib\net20\OpenTK.dll</HintPath>
37+
<HintPath>..\..\packages\OpenTK.2.0.0\lib\net20\OpenTK.dll</HintPath>
3838
</Reference>
3939
<Reference Include="PresentationCore" />
4040
<Reference Include="PresentationFramework" />

OpenTkControl/OpenTkControlBase.xaml.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
44
using System.ComponentModel;
5+
using System.Runtime.CompilerServices;
56
using System.Threading;
67
using System.Threading.Tasks;
78
using System.Windows;
@@ -305,6 +306,12 @@ public Task RequestRepaint()
305306
return tcs.Task;
306307
}
307308

309+
/// <summary>
310+
/// Check if it is run in designer mode.
311+
/// </summary>
312+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
313+
protected bool IsDesignMode() => DesignerProperties.GetIsInDesignMode(this);
314+
308315
/// <summary>
309316
/// Renders a screenshot of the frame with the specified dimensions. It will be in bgra format with
310317
/// [0,0] at the bottom left corner. Note that this is not meant for taking screenshots of what is
@@ -334,6 +341,11 @@ public Task<uint[,]> Screenshot(int width = 0, int height = 0)
334341
/// <param name="args">Information about the event</param>
335342
protected virtual void OnLoaded(object sender, RoutedEventArgs args)
336343
{
344+
#if DEBUG
345+
if (IsDesignMode())
346+
return;
347+
#endif
348+
337349
_windowInfo = Utilities.CreateWindowsWindowInfo(
338350
new WindowInteropHelper(Window.GetWindow(this)).Handle);
339351
}

OpenTkControl/ThreadOpenTkControl.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ protected override void OnLoaded(object sender, RoutedEventArgs args)
5353
base.OnLoaded(sender, args);
5454

5555
_endThreadCts = new CancellationTokenSource();
56+
5657
_renderThread = new Thread(RenderThread)
5758
{
5859
IsBackground = true,
@@ -89,6 +90,12 @@ private void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArg
8990
/// <param name="boxedToken"></param>
9091
private void RenderThread(object boxedToken)
9192
{
93+
#if DEBUG
94+
// Don't render in design mode to prevent errors from calling OpenGL API methods.
95+
if (Dispatcher.Invoke(() => IsDesignMode()))
96+
return;
97+
#endif
98+
9299
CancellationToken token = (CancellationToken) boxedToken;
93100

94101
InitOpenGl();

OpenTkControl/UiOpenTkControl.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.ComponentModel;
23
using System.Threading.Tasks;
34
using System.Windows;
45
using System.Windows.Media;
@@ -47,6 +48,12 @@ protected override void OnUnloaded(object sender, RoutedEventArgs routedEventArg
4748
/// <param name="args">The event arguments about this event</param>
4849
private void CompositionTargetOnRendering(object sender, EventArgs args)
4950
{
51+
#if DEBUG
52+
//We needn't call render() for avoiding crash by calling OpenGL API methods.
53+
if (IsDesignMode())
54+
return;
55+
#endif
56+
5057
DateTime now = DateTime.Now;
5158
if ((_continuous && now > _nextRenderTime) || ManualRepaintEvent.WaitOne(0))
5259
{

0 commit comments

Comments
 (0)