Browse Source
Add back Design.SetPreviewWith(AvaloniaObject, Control) overload (#21184)
* Add back Design.SetPreviewWith(AvaloniaObject, Control) overload
* Remove dead code
pull/21219/head
Julien Lebosquain
6 months ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with
38 additions and
8 deletions
-
src/Avalonia.Controls/Design.cs
-
tests/Avalonia.Markup.Xaml.UnitTests/Xaml/DesignModeTests.cs
|
|
@ -142,6 +142,40 @@ namespace Avalonia.Controls |
|
|
s_previewWith[target] = template; |
|
|
s_previewWith[target] = template; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Sets a preview control for the specified <see cref="AvaloniaObject"/> at design-time.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="target">The target object.</param>
|
|
|
|
|
|
/// <param name="control">The preview control.</param>
|
|
|
|
|
|
public static void SetPreviewWith(AvaloniaObject target, Control? control) |
|
|
|
|
|
{ |
|
|
|
|
|
if (control is null) |
|
|
|
|
|
{ |
|
|
|
|
|
s_previewWith[target] = null; |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
switch (target) |
|
|
|
|
|
{ |
|
|
|
|
|
case ResourceDictionary resourceDictionary: |
|
|
|
|
|
SetPreviewWith(resourceDictionary, control); |
|
|
|
|
|
break; |
|
|
|
|
|
case IDataTemplate dataTemplate: |
|
|
|
|
|
SetPreviewWith(dataTemplate, control); |
|
|
|
|
|
break; |
|
|
|
|
|
case IStyle style: |
|
|
|
|
|
SetPreviewWith(style, control); |
|
|
|
|
|
break; |
|
|
|
|
|
case Visual: |
|
|
|
|
|
// Not a supported scenario without templates; causes stack overflows.
|
|
|
|
|
|
s_previewWith[target] = null; |
|
|
|
|
|
break; |
|
|
|
|
|
default: |
|
|
|
|
|
SetPreviewWith(target, new FuncTemplate<Control>(() => control)); |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
/// Sets a preview template for the specified <see cref="ResourceDictionary"/> at design-time.
|
|
|
/// Sets a preview template for the specified <see cref="ResourceDictionary"/> at design-time.
|
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
|
|
|
@ -33,23 +33,19 @@ public class DesignModeTests : XamlTestBase |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
[Fact] |
|
|
[Fact] |
|
|
public void Design_Mode_PreviewWith_Works_With_Control_Template() |
|
|
public void Design_Mode_PreviewWith_Returns_Original_Control() |
|
|
{ |
|
|
{ |
|
|
using (UnitTestApplication.Start(TestServices.MockWindowingPlatform)) |
|
|
using (UnitTestApplication.Start(TestServices.MockWindowingPlatform)) |
|
|
{ |
|
|
{ |
|
|
var obj = (Control)AvaloniaRuntimeXamlLoader.Load(@"
|
|
|
var obj = (Control)AvaloniaRuntimeXamlLoader.Load(@"
|
|
|
<Button xmlns='https://github.com/avaloniaui'>
|
|
|
<Button xmlns='https://github.com/avaloniaui'>
|
|
|
<Design.PreviewWith> |
|
|
<Design.PreviewWith> |
|
|
<Template> |
|
|
<Border /> |
|
|
<Border> |
|
|
|
|
|
<Button /> |
|
|
|
|
|
</Border> |
|
|
|
|
|
</Template> |
|
|
|
|
|
</Design.PreviewWith> |
|
|
</Design.PreviewWith> |
|
|
</Button>", designMode: true);
|
|
|
</Button>", designMode: true);
|
|
|
var preview = Design.CreatePreviewWithControl(obj); |
|
|
var preview = Design.CreatePreviewWithControl(obj); |
|
|
var previewBorder = Assert.IsType<Border>(preview); |
|
|
// Should return the original control, not the preview, as this is not supported to avoid stack overflows.
|
|
|
Assert.IsType<Button>(previewBorder.Child); |
|
|
Assert.Same(obj, preview); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|