示例#1
0
        public void Attached_Property_With_Namespace_Is_Set()
        {
            var xaml =
                @"<ContentControl xmlns='https://github.com/avaloniaui' 
                    xmlns:test='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'
                    test:BasicTestsAttachedPropertyHolder.Foo='Bar'/>";

            var target = AvaloniaXamlLoader.Parse <ContentControl>(xaml);

            Assert.NotNull(target);
            Assert.Equal("Bar", BasicTestsAttachedPropertyHolder.GetFoo(target));
        }
示例#2
0
        public void Named_Control_Is_Added_To_NameScope_Simple()
        {
            var xaml = @"
<UserControl xmlns='https://github.com/avaloniaui'>
    <Button Name='button'>Foo</Button>
</UserControl>";

            var control = AvaloniaXamlLoader.Parse <UserControl>(xaml);
            var button  = control.FindControl <Button>("button");

            Assert.Equal("Foo", button.Content);
        }
        public void Attached_Property_In_Panel_Is_Set()
        {
            var xaml = @"
<Panel xmlns='https://github.com/avaloniaui'>
    <ToolTip.Tip>Foo</ToolTip.Tip>
</Panel>";

            var target = AvaloniaXamlLoader.Parse <Panel>(xaml);

            Assert.Empty(target.Children);

            Assert.Equal("Foo", ToolTip.GetTip(target));
        }
示例#4
0
        /// <summary>
        /// Loads a theme from an XAML file.
        /// </summary>
        /// <param name="file">The XAML file containing the style definition.</param>
        /// <returns>Returns the loaded theme.</returns>
        public static ITheme Load(FileInfo file)
        {
            if (file is null)
            {
                throw new ArgumentNullException(nameof(file));
            }

            var name  = Path.GetFileNameWithoutExtension(file.Name);
            var xaml  = File.ReadAllText(file.FullName);
            var style = AvaloniaXamlLoader.Parse <IStyle>(xaml);

            return(new Theme(name, style));
        }
示例#5
0
        public void Non_Attached_Property_With_Attached_Property_Syntax_Throws()
        {
            // 1) It has been allowed in AvaloniaObject.SetValue for ages
            // 2) There is no way to know if AddOwner was called in compile-time
            if (!AvaloniaXamlLoader.UseLegacyXamlLoader)
            {
                return;
            }
            var xaml =
                @"<ContentControl xmlns='https://github.com/avaloniaui' TextBlock.Text='foo'/>";

            XamlTestHelpers.AssertThrowsXamlException(() => AvaloniaXamlLoader.Parse <ContentControl>(xaml));
        }
        public void Attached_Property_Is_Set_On_Control_Outside_Avalonia_Namspace()
        {
            // Test for issue #1548
            var xaml =
                @"<UserControl xmlns='https://github.com/avaloniaui'
    xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
  <local:TestControl Grid.Column='2' />
</UserControl>";

            var target = AvaloniaXamlLoader.Parse <UserControl>(xaml);

            Assert.Equal(2, Grid.GetColumn((TestControl)target.Content));
        }
        public void Attached_Property_Supports_Binding()
        {
            using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
            {
                var xaml =
                    @"<Window xmlns='https://github.com/avaloniaui' TextBlock.FontSize='{Binding}'/>";

                var target = AvaloniaXamlLoader.Parse <ContentControl>(xaml);

                target.DataContext = 21.0;

                Assert.Equal(21.0, TextBlock.GetFontSize(target));
            }
        }
示例#8
0
        public void Transitions_Should_Be_Properly_Parsed()
        {
            var parsed = (Grid)AvaloniaXamlLoader.Parse(@"
<Grid xmlns='https://github.com/avaloniaui' >
  <Grid.Transitions>
    <DoubleTransition Property='Opacity'
       Easing='CircularEaseIn'
       Duration='0:0:0.5' />
  </Grid.Transitions>
</Grid>");

            Assert.Equal(1, parsed.Transitions.Count);
            Assert.Equal(Visual.OpacityProperty, parsed.Transitions[0].Property);
        }
示例#9
0
        public void ControlTemplate_With_TargetType_Is_Operational()
        {
            var xaml     = @"
<ControlTemplate xmlns='https://github.com/avaloniaui' 
                 xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
                 TargetType='{x:Type ContentControl}'>
    <ContentPresenter Content='{TemplateBinding Content}' />
</ControlTemplate>
";
            var template = AvaloniaXamlLoader.Parse <ControlTemplate>(xaml);

            Assert.Equal(typeof(ContentControl), template.TargetType);

            Assert.IsType(typeof(ContentPresenter), template.Build(new ContentControl()).Control);
        }
示例#10
0
        public void AvaloniaProperty_With_Getter_And_No_Setter_Is_Set()
        {
            if (!AvaloniaXamlLoader.UseLegacyXamlLoader)
            {
                return;
            }
            var xaml =
                @"<local:NonControl xmlns='https://github.com/avaloniaui' 
    xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'
    Bar='bar' />";

            var target = AvaloniaXamlLoader.Parse <NonControl>(xaml);

            Assert.Equal("bar", target.Bar);
        }
示例#11
0
        public void Custom_Properties_Should_Work_With_XClass()
        {
            var precompiled = new XamlIlClassWithCustomProperty();

            Assert.Equal("123", precompiled.Test);
            var loaded = (XamlIlClassWithCustomProperty)AvaloniaXamlLoader.Parse(@"
<UserControl xmlns='https://github.com/avaloniaui'
             xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
             x:Class='Avalonia.Markup.Xaml.UnitTests.XamlIlClassWithCustomProperty'
             Test='321'>

</UserControl>");

            Assert.Equal("321", loaded.Test);
        }
示例#12
0
        public void AvaloniaProperty_Without_Getter_And_Setter_Is_Set()
        {
            // It's not possible to know in compile time if a read-only property has a magic way of being not read-only
            if (!AvaloniaXamlLoader.UseLegacyXamlLoader)
            {
                return;
            }
            var xaml =
                @"<local:NonControl xmlns='https://github.com/avaloniaui' 
    xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'
    Foo='55' />";

            var target = AvaloniaXamlLoader.Parse <NonControl>(xaml);

            Assert.Equal(55, target.GetValue(NonControl.FooProperty));
        }
示例#13
0
        public void Named_Control_Is_Added_To_NameScope()
        {
            using (UnitTestApplication.Start(TestServices.StyledWindow))
            {
                var xaml = @"
<Window xmlns='https://github.com/avaloniaui'
             xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
    <Button Name='button'>Foo</Button>
</Window>";

                var window = AvaloniaXamlLoader.Parse <Window>(xaml);
                var button = window.FindControl <Button>("button");

                Assert.Equal("Foo", button.Content);
            }
        }
示例#14
0
        public void Slider_Properties_Can_Be_Set_In_Any_Order()
        {
            using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
            {
                var xaml = @"
<Window xmlns='https://github.com/avaloniaui'>
    <Slider Width='400' Value='500' Minimum='0' Maximum='1000'/>
</Window>";

                var window = AvaloniaXamlLoader.Parse <Window>(xaml);
                var slider = (Slider)window.Content;

                Assert.Equal(0, slider.Minimum);
                Assert.Equal(1000, slider.Maximum);
                Assert.Equal(500, slider.Value);
            }
        }
示例#15
0
        public void BeginInit_Matches_EndInit()
        {
            using (UnitTestApplication.Start(TestServices.StyledWindow))
            {
                var xaml = @"
<Window xmlns='https://github.com/avaloniaui'
             xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
             xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
    <local:InitializationOrderTracker />
</Window>";

                var window  = AvaloniaXamlLoader.Parse <Window>(xaml);
                var tracker = (InitializationOrderTracker)window.Content;

                Assert.Equal(0, tracker.InitState);
            }
        }
示例#16
0
        public void Double_Xaml_Binding_Is_Operational()
        {
            using (UnitTestApplication.Start(TestServices.MockPlatformWrapper
                                             .With(windowingPlatform: new MockWindowingPlatform())))
            {
                var xaml =
                    @"<Window xmlns='https://github.com/avaloniaui' Width='{Binding}'/>";

                var target = AvaloniaXamlLoader.Parse <ContentControl>(xaml);

                Assert.Null(target.Content);

                target.DataContext = 55.0;

                Assert.Equal(55.0, target.Width);
            }
        }
示例#17
0
        public void Element_Whitespace_Should_Be_Trimmed()
        {
            using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
            {
                var xaml = @"
<Window xmlns='https://github.com/avaloniaui'>
    <TextBlock>
        Hello World!
    </TextBlock>
</Window>";

                var window    = AvaloniaXamlLoader.Parse <Window>(xaml);
                var textBlock = (TextBlock)window.Content;

                Assert.Equal("Hello World!", textBlock.Text);
            }
        }
示例#18
0
        public void Parser_Should_Override_Precompiled_Xaml()
        {
            var precompiled = new XamlIlClassWithPrecompiledXaml();

            Assert.Equal(Brushes.Red, precompiled.Background);
            Assert.Equal(1, precompiled.Opacity);
            var loaded = (XamlIlClassWithPrecompiledXaml)AvaloniaXamlLoader.Parse(@"
<UserControl xmlns='https://github.com/avaloniaui'
             xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
             x:Class='Avalonia.Markup.Xaml.UnitTests.XamlIlClassWithPrecompiledXaml'
             Opacity='0'>
    
</UserControl>");

            Assert.Equal(loaded.Opacity, 0);
            Assert.Null(loaded.Background);
        }
示例#19
0
        public void Binding_To_Self_Works()
        {
            using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
            {
                var xaml = @"
<Window xmlns='https://github.com/avaloniaui'
        xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
    <TextBlock Name='textblock' Text='{Binding Tag, RelativeSource={RelativeSource Self}}'/>
</Window>";

                var window    = AvaloniaXamlLoader.Parse <ContentControl>(xaml);
                var textBlock = (TextBlock)window.Content;

                textBlock.Tag = "foo";

                Assert.Equal("foo", textBlock.Text);
            }
        }
示例#20
0
        static void Main(string[] args)
        {
            var appBuilder = BuildAvaloniaApp().SetupWithoutStarting();

            Console.WriteLine("Hello World!");

            var str = Repro();

            Console.WriteLine(str);

            Window w = AvaloniaXamlLoader.Parse <Window>(str);

            w.DataContext = new { Person = "PersonName" };

            appBuilder.Instance.Run(w);

            w.ShowDialog();
        }
示例#21
0
        public void ContentControl_ContentTemplate_Is_Functional()
        {
            var xaml =
                @"<ContentControl xmlns='https://github.com/avaloniaui'>
    <ContentControl.ContentTemplate>
        <DataTemplate>
            <TextBlock Text='Foo' />
        </DataTemplate>
    </ContentControl.ContentTemplate>
</ContentControl>";

            var contentControl = AvaloniaXamlLoader.Parse <ContentControl>(xaml);
            var target         = contentControl.ContentTemplate;

            Assert.NotNull(target);

            var txt = (TextBlock)target.Build(null);

            Assert.Equal("Foo", txt.Text);
        }
示例#22
0
        public void Xaml_Binding_Is_Delayed()
        {
            using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
            {
                var xaml =
                    @"<ContentControl xmlns='https://github.com/avaloniaui' Content='{Binding}'/>";

                var target = AvaloniaXamlLoader.Parse <ContentControl>(xaml);

                Assert.Null(target.Content);

                target.DataContext = "Foo";

                Assert.Null(target.Content);

                DelayedBinding.ApplyBindings(target);

                Assert.Equal("Foo", target.Content);
            }
        }
示例#23
0
        public void Multi_Xaml_Binding_Is_Parsed()
        {
            var xaml =
                @"<MultiBinding xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
    Converter ='{x:Static BoolConverters.And}'>
     <Binding Path='Foo' />
     <Binding Path='Bar' />
</MultiBinding>";

            var target = AvaloniaXamlLoader.Parse <MultiBinding>(xaml);

            Assert.Equal(2, target.Bindings.Count);

            Assert.Equal(BoolConverters.And, target.Converter);

            var bindings = target.Bindings.Cast <Binding>().ToArray();

            Assert.Equal("Foo", bindings[0].Path);
            Assert.Equal("Bar", bindings[1].Path);
        }
示例#24
0
        public void DeferedXamlLoader_Should_Preserve_NamespacesContext()
        {
            var xaml =
                @"<ContentControl xmlns='https://github.com/avaloniaui'
            xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
    <ContentControl.ContentTemplate>
        <DataTemplate>
            <TextBlock  Tag='{Static local:NonControl.StringProperty}'/>
        </DataTemplate>
    </ContentControl.ContentTemplate>
</ContentControl>";

            var contentControl = AvaloniaXamlLoader.Parse <ContentControl>(xaml);
            var template       = contentControl.ContentTemplate;

            Assert.NotNull(template);

            var txt = (TextBlock)template.Build(null);

            Assert.Equal((object)NonControl.StringProperty, txt.Tag);
        }
示例#25
0
        public void ControlTemplate_With_Nested_Child_Is_Operational()
        {
            var xaml     = @"
<ControlTemplate xmlns='https://github.com/avaloniaui'>
    <ContentControl Name='parent'>
        <ContentControl Name='child' />
    </ContentControl>
</ControlTemplate>
";
            var template = AvaloniaXamlLoader.Parse <ControlTemplate>(xaml);

            var parent = (ContentControl)template.Build(new ContentControl()).Control;

            Assert.Equal("parent", parent.Name);

            var child = parent.Content as ContentControl;

            Assert.NotNull(child);

            Assert.Equal("child", child.Name);
        }
示例#26
0
        public void Panel_Children_Are_Added()
        {
            var xaml = @"
<UserControl xmlns='https://github.com/avaloniaui'>
    <Panel Name='panel'>
        <ContentControl Name='Foo' />
        <ContentControl Name='Bar' />
    </Panel>
</UserControl>";

            var control = AvaloniaXamlLoader.Parse <UserControl>(xaml);

            var panel = control.FindControl <Panel>("panel");

            Assert.Equal(2, panel.Children.Count);

            var foo = control.FindControl <ContentControl>("Foo");
            var bar = control.FindControl <ContentControl>("Bar");

            Assert.Contains(foo, panel.Children);
            Assert.Contains(bar, panel.Children);
        }
示例#27
0
        public void All_Properties_Are_Set_Before_Final_EndInit()
        {
            using (UnitTestApplication.Start(TestServices.StyledWindow))
            {
                var xaml = @"
<Window xmlns='https://github.com/avaloniaui'
                xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
                xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
    <local:InitializationOrderTracker Width='100' Height='100'
        Tag='{Binding Height, RelativeSource={RelativeSource Self}}' />
</Window>";


                var window  = AvaloniaXamlLoader.Parse <Window>(xaml);
                var tracker = (InitializationOrderTracker)window.Content;

                //ensure binding is set and operational first
                Assert.Equal(100.0, tracker.Tag);

                Assert.Equal("EndInit 0", tracker.Order.Last());
            }
        }
示例#28
0
        public void Control_Is_Added_To_Parent_Before_Final_EndInit()
        {
            using (UnitTestApplication.Start(TestServices.StyledWindow))
            {
                var xaml = @"
<Window xmlns='https://github.com/avaloniaui'
             xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
             xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
    <local:InitializationOrderTracker Width='100'/>
</Window>";

                var window  = AvaloniaXamlLoader.Parse <Window>(xaml);
                var tracker = (InitializationOrderTracker)window.Content;

                var attached = tracker.Order.IndexOf("AttachedToLogicalTree");
                var endInit  = tracker.Order.IndexOf("EndInit 0");

                Assert.NotEqual(-1, attached);
                Assert.NotEqual(-1, endInit);
                Assert.True(attached < endInit);
            }
        }
示例#29
0
        public void ControlTemplate_With_Panel_Children_Are_Added()
        {
            var xaml     = @"
<ControlTemplate xmlns='https://github.com/avaloniaui'>
    <Panel Name='panel'>
        <ContentControl Name='Foo' />
        <ContentControl Name='Bar' />
    </Panel>
</ControlTemplate>
";
            var template = AvaloniaXamlLoader.Parse <ControlTemplate>(xaml);

            var panel = (Panel)template.Build(new ContentControl()).Control;

            Assert.Equal(2, panel.Children.Count);

            var foo = panel.Children[0];
            var bar = panel.Children[1];

            Assert.Equal("Foo", foo.Name);
            Assert.Equal("Bar", bar.Name);
        }
示例#30
0
        public void Grid_Row_Col_Definitions_Are_Built()
        {
            var xaml = @"
<Grid xmlns='https://github.com/avaloniaui'>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width='100' />
        <ColumnDefinition Width='Auto' />
        <ColumnDefinition Width='*' />
        <ColumnDefinition Width='100*' />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height='100' />
        <RowDefinition Height='Auto' />
        <RowDefinition Height='*' />
        <RowDefinition Height='100*' />
    </Grid.RowDefinitions>
</Grid>";

            var grid = AvaloniaXamlLoader.Parse <Grid>(xaml);

            Assert.Equal(4, grid.ColumnDefinitions.Count);
            Assert.Equal(4, grid.RowDefinitions.Count);

            var expected1 = new GridLength(100);
            var expected2 = GridLength.Auto;
            var expected3 = new GridLength(1, GridUnitType.Star);
            var expected4 = new GridLength(100, GridUnitType.Star);

            Assert.Equal(expected1, grid.ColumnDefinitions[0].Width);
            Assert.Equal(expected2, grid.ColumnDefinitions[1].Width);
            Assert.Equal(expected3, grid.ColumnDefinitions[2].Width);
            Assert.Equal(expected4, grid.ColumnDefinitions[3].Width);

            Assert.Equal(expected1, grid.RowDefinitions[0].Height);
            Assert.Equal(expected2, grid.RowDefinitions[1].Height);
            Assert.Equal(expected3, grid.RowDefinitions[2].Height);
            Assert.Equal(expected4, grid.RowDefinitions[3].Height);
        }