Browse Source

Added support for Design.PreviewWith in resource dictionaries.

pull/8263/head
Steven Kirk 4 years ago
parent
commit
8abeb76235
  1. 22
      src/Avalonia.Controls/Design.cs
  2. 22
      src/Avalonia.DesignerSupport/DesignWindowLoader.cs

22
src/Avalonia.Controls/Design.cs

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Avalonia.Styling;
@ -6,6 +7,8 @@ namespace Avalonia.Controls
{
public static class Design
{
private static Dictionary<object, Control?>? _previewWith;
public static bool IsDesignMode { get; internal set; }
public static readonly AttachedProperty<double> HeightProperty = AvaloniaProperty
@ -47,19 +50,30 @@ namespace Avalonia.Controls
return control.GetValue(DataContextProperty);
}
public static readonly AttachedProperty<Control> PreviewWithProperty = AvaloniaProperty
.RegisterAttached<AvaloniaObject, Control>("PreviewWith", typeof (Design));
public static readonly AttachedProperty<Control?> PreviewWithProperty = AvaloniaProperty
.RegisterAttached<AvaloniaObject, Control?>("PreviewWith", typeof (Design));
public static void SetPreviewWith(AvaloniaObject target, Control control)
public static void SetPreviewWith(AvaloniaObject target, Control? control)
{
target.SetValue(PreviewWithProperty, control);
}
public static Control GetPreviewWith(AvaloniaObject target)
public static void SetPreviewWith(ResourceDictionary target, Control? control)
{
_previewWith ??= new();
_previewWith[target] = control;
}
public static Control? GetPreviewWith(AvaloniaObject target)
{
return target.GetValue(PreviewWithProperty);
}
public static Control? GetPreviewWith(ResourceDictionary target)
{
return _previewWith?[target];
}
public static readonly AttachedProperty<IStyle> DesignStyleProperty = AvaloniaProperty
.RegisterAttached<Control, IStyle>("DesignStyle", typeof(Design));

22
src/Avalonia.DesignerSupport/DesignWindowLoader.cs

@ -37,6 +37,7 @@ namespace Avalonia.DesignerSupport
var localAsm = assemblyPath != null ? Assembly.LoadFile(Path.GetFullPath(assemblyPath)) : null;
var loaded = loader.Load(stream, localAsm, null, baseUri, true);
var style = loaded as IStyle;
var resources = loaded as ResourceDictionary;
if (style != null)
{
var substitute = Design.GetPreviewWith((AvaloniaObject)style);
@ -58,6 +59,27 @@ namespace Avalonia.DesignerSupport
}
};
}
else if (resources != null)
{
var substitute = Design.GetPreviewWith(resources);
if (substitute != null)
{
substitute.Resources.MergedDictionaries.Add(resources);
control = substitute;
}
else
control = new StackPanel
{
Children =
{
new TextBlock {Text = "ResourceDictionaries can't be previewed without Design.PreviewWith. Add"},
new TextBlock {Text = "<Design.PreviewWith>"},
new TextBlock {Text = " <Border Padding=20><!-- YOUR CONTROL FOR PREVIEW HERE --></Border>"},
new TextBlock {Text = "</Design.PreviewWith>"},
new TextBlock {Text = "in your resource dictionary"}
}
};
}
else if (loaded is Application)
control = new TextBlock {Text = "Application can't be previewed in design view"};
else

Loading…
Cancel
Save