Browse Source

Fix loading of UserControls.

Also added a UserControl test, though it turns out it was unrelated to
the problem at hand.
pull/464/head
Steven Kirk 10 years ago
parent
commit
6e337ec877
  1. 5
      src/Markup/Perspex.Markup.Xaml/PerspexXamlLoader.cs
  2. 22
      src/Markup/Perspex.Markup.Xaml/Styling/StyleInclude.cs
  3. 5
      src/Perspex.Controls/Control.cs
  4. 1
      tests/Perspex.Controls.UnitTests/Perspex.Controls.UnitTests.csproj
  5. 57
      tests/Perspex.Controls.UnitTests/UserControlTests.cs

5
src/Markup/Perspex.Markup.Xaml/PerspexXamlLoader.cs

@ -67,21 +67,26 @@ namespace Perspex.Markup.Xaml
// in certain situations, so we try to load .xaml and if that's not found we try .paml.
// Ideally we'd be able to use .xaml everywhere
var assetLocator = PerspexLocator.Current.GetService<IAssetLoader>();
if (assetLocator == null)
{
throw new InvalidOperationException(
"Could not create IAssetLoader : maybe Application.RegisterServices() wasn't called?");
}
foreach (var uri in GetUrisFor(type))
{
if (assetLocator.Exists(uri))
{
using (var stream = assetLocator.Open(uri))
{
var initialize = rootInstance as ISupportInitialize;
initialize?.BeginInit();
return Load(stream, rootInstance);
}
}
}
throw new FileNotFoundException("Unable to find view for " + type.FullName);
}

22
src/Markup/Perspex.Markup.Xaml/Styling/StyleInclude.cs

@ -11,6 +11,8 @@ namespace Perspex.Markup.Xaml.Styling
/// </summary>
public class StyleInclude : IStyle
{
private IStyle _loaded;
/// <summary>
/// Gets or sets the source URL.
/// </summary>
@ -19,19 +21,25 @@ namespace Perspex.Markup.Xaml.Styling
/// <summary>
/// Gets the loaded style.
/// </summary>
public IStyle Loaded { get; private set; }
/// <inheritdoc/>
public void Attach(IStyleable control, IStyleHost container)
public IStyle Loaded
{
if (Source != null)
get
{
if (Loaded == null)
if (_loaded == null)
{
var loader = new PerspexXamlLoader();
Loaded = (IStyle)loader.Load(Source);
_loaded = (IStyle)loader.Load(Source);
}
return _loaded;
}
}
/// <inheritdoc/>
public void Attach(IStyleable control, IStyleHost container)
{
if (Source != null)
{
Loaded.Attach(control, container);
}
}

5
src/Perspex.Controls/Control.cs

@ -323,11 +323,10 @@ namespace Perspex.Controls
{
if (_initCount == 0)
{
++_initCount;
//throw new InvalidOperationException("BeginInit was not called.");
throw new InvalidOperationException("BeginInit was not called.");
}
if (--_initCount == 0 && !_styled)
if (--_initCount == 0 && _isAttachedToLogicalTree && !_styled)
{
RegisterWithNameScope();
ApplyStyling();

1
tests/Perspex.Controls.UnitTests/Perspex.Controls.UnitTests.csproj

@ -90,6 +90,7 @@
</Choose>
<ItemGroup>
<Compile Include="ClassesTests.cs" />
<Compile Include="UserControlTests.cs" />
<Compile Include="DockPanelTests.cs" />
<Compile Include="EnumerableExtensions.cs" />
<Compile Include="GridSplitterTests.cs" />

57
tests/Perspex.Controls.UnitTests/UserControlTests.cs

@ -0,0 +1,57 @@
// Copyright (c) The Perspex Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using System.Linq;
using Perspex.Controls.Presenters;
using Perspex.Controls.Primitives;
using Perspex.Controls.Templates;
using Perspex.Styling;
using Perspex.UnitTests;
using Xunit;
namespace Perspex.Controls.UnitTests
{
public class UserControlTests
{
[Fact]
public void Should_Be_Styled_As_UserControl()
{
using (UnitTestApplication.Start(TestServices.RealStyler))
{
var target = new UserControl();
var root = new TestRoot
{
Styles = new Styles
{
new Style(x => x.OfType<ContentControl>())
{
Setters = new[]
{
new Setter(TemplatedControl.TemplateProperty, GetTemplate())
}
}
},
Child = target,
};
Assert.NotNull(target.Template);
}
}
private FuncControlTemplate GetTemplate()
{
return new FuncControlTemplate<ContentControl>(parent =>
{
return new Border
{
Background = new Media.SolidColorBrush(0xffffffff),
Child = new ContentPresenter
{
Name = "PART_ContentPresenter",
[~ContentPresenter.ContentProperty] = parent[~ContentControl.ContentProperty],
}
};
});
}
}
}
Loading…
Cancel
Save