Browse Source

Derive transitioning control from ContentControl

artyom 6 năm trước cách đây
mục cha
commit
6360de1921

+ 1 - 1
src/Avalonia.ReactiveUI/RoutedViewHost.cs

@@ -53,7 +53,7 @@ namespace Avalonia.ReactiveUI
     /// ReactiveUI routing documentation website</see> for more info.
     /// </para>
     /// </remarks>
-    public class RoutedViewHost : TransitioningUserControl, IActivatable, IEnableLogger
+    public class RoutedViewHost : TransitioningContentControl, IActivatable, IEnableLogger
     {
         /// <summary>
         /// <see cref="AvaloniaProperty"/> for the <see cref="Router"/> property.

+ 10 - 4
src/Avalonia.ReactiveUI/TransitioningUserControl.cs → src/Avalonia.ReactiveUI/TransitioningContentControl.cs

@@ -11,27 +11,27 @@ namespace Avalonia.ReactiveUI
     /// <summary>
     /// A ContentControl that animates the transition when its content is changed.
     /// </summary>
-    public class TransitioningUserControl : UserControl
+    public class TransitioningContentControl : ContentControl, IStyleable
     {
         /// <summary>
         /// <see cref="AvaloniaProperty"/> for the <see cref="FadeInAnimation"/> property.
         /// </summary>
         public static readonly AvaloniaProperty<IAnimation> FadeInAnimationProperty =
-            AvaloniaProperty.Register<TransitioningUserControl, IAnimation>(nameof(DefaultContent),
+            AvaloniaProperty.Register<TransitioningContentControl, IAnimation>(nameof(DefaultContent),
                 CreateOpacityAnimation(0d, 1d, TimeSpan.FromSeconds(0.25)));
 
         /// <summary>
         /// <see cref="AvaloniaProperty"/> for the <see cref="FadeOutAnimation"/> property.
         /// </summary>
         public static readonly AvaloniaProperty<IAnimation> FadeOutAnimationProperty =
-            AvaloniaProperty.Register<TransitioningUserControl, IAnimation>(nameof(DefaultContent),
+            AvaloniaProperty.Register<TransitioningContentControl, IAnimation>(nameof(DefaultContent),
                 CreateOpacityAnimation(1d, 0d, TimeSpan.FromSeconds(0.25)));
 
         /// <summary>
         /// <see cref="AvaloniaProperty"/> for the <see cref="DefaultContent"/> property.
         /// </summary>
         public static readonly AvaloniaProperty<object> DefaultContentProperty =
-            AvaloniaProperty.Register<TransitioningUserControl, object>(nameof(DefaultContent));
+            AvaloniaProperty.Register<TransitioningContentControl, object>(nameof(DefaultContent));
         
         /// <summary>
         /// Gets or sets the animation played when content appears.
@@ -68,6 +68,12 @@ namespace Avalonia.ReactiveUI
             get => base.Content;
             set => UpdateContentWithTransition(value);
         }
+        
+        /// <summary>
+        /// TransitioningContentControl uses the default ContentControl 
+        /// template from Avalonia default theme.
+        /// </summary>
+        Type IStyleable.StyleKey => typeof(ContentControl);
 
         /// <summary>
         /// Updates the content with transitions.

+ 1 - 1
src/Avalonia.ReactiveUI/ViewModelViewHost.cs

@@ -13,7 +13,7 @@ namespace Avalonia.ReactiveUI
     /// the ViewModel property and display it. This control is very useful
     /// inside a DataTemplate to display the View associated with a ViewModel.
     /// </summary>
-    public class ViewModelViewHost : TransitioningUserControl, IViewFor, IEnableLogger
+    public class ViewModelViewHost : TransitioningContentControl, IViewFor, IEnableLogger
     {
         /// <summary>
         /// <see cref="AvaloniaProperty"/> for the <see cref="ViewModel"/> property.

+ 64 - 0
tests/Avalonia.ReactiveUI.UnitTests/TransitioningContentControlTest.cs

@@ -0,0 +1,64 @@
+// Copyright (c) The Avalonia 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 Avalonia.Controls;
+using Avalonia.Controls.Presenters;
+using Avalonia.Controls.Templates;
+using Avalonia.UnitTests;
+using Avalonia.VisualTree;
+using ReactiveUI;
+using Splat;
+using Xunit;
+
+namespace Avalonia.ReactiveUI.UnitTests
+{
+    public class TransitioningContentControlTest
+    {
+        [Fact]
+        public void Transitioning_Control_Should_Derive_Template_From_Content_Control()
+        {
+            var target = new TransitioningContentControl();
+            var stylable = (IStyledElement)target;
+            Assert.Equal(typeof(ContentControl),stylable.StyleKey);
+        }
+
+        [Fact]
+        public void Transitioning_Control_Template_Should_Be_Instantiated() 
+        {
+            var target = new TransitioningContentControl
+            {
+                FadeInAnimation = null,
+                FadeOutAnimation = null,
+                Template = GetTemplate(),
+                Content = "Foo"
+            };
+            target.ApplyTemplate();
+            ((ContentPresenter)target.Presenter).UpdateChild();
+
+            var child = ((IVisual)target).VisualChildren.Single();
+            Assert.IsType<Border>(child);
+            child = child.VisualChildren.Single();
+            Assert.IsType<ContentPresenter>(child);
+            child = child.VisualChildren.Single();
+            Assert.IsType<TextBlock>(child);
+        }
+
+        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],
+                        [~ContentPresenter.ContentTemplateProperty] = parent[~ContentControl.ContentTemplateProperty],
+                    }
+                };
+            });
+        }
+    }
+}