Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UserControl fixes #3279

Merged
merged 2 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/SamplesApp/SamplesApp.Droid/SamplesApp.Droid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
<ResourcesDirectory>..\SamplesApp.Shared\Strings</ResourcesDirectory>
<!-- AndroidUseAapt2 is disabled until https://github.com/unoplatform/uno/issues/1382 is resolved -->
<AndroidUseAapt2>false</AndroidUseAapt2>
<!-- https://github.com/unoplatform/uno/issues/61 -->
<UnoSkipUserControlsInVisualTree>false</UnoSkipUserControlsInVisualTree>
</PropertyGroup>
<PropertyGroup>
<IsUiAutomationMappingEnabled>true</IsUiAutomationMappingEnabled>
Expand Down
3 changes: 0 additions & 3 deletions src/SamplesApp/SamplesApp.Wasm/SamplesApp.Wasm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
<DefineConstants>$(DefineConstants);__WASM__;HAS_UNO</DefineConstants>
<NoWarn>NU1701,CS1998</NoWarn>
<LangVersion>7.3</LangVersion>
<!-- https://github.com/unoplatform/uno/issues/61 -->
<UnoSkipUserControlsInVisualTree>false</UnoSkipUserControlsInVisualTree>

<IsUiAutomationMappingEnabled>true</IsUiAutomationMappingEnabled>
<UseUnoXamlParser>true</UseUnoXamlParser>
</PropertyGroup>
Expand Down
4 changes: 1 addition & 3 deletions src/SamplesApp/SamplesApp.iOS/SamplesApp.iOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
</NuGetPackageImportStamp>
<IsUiAutomationMappingEnabled>true</IsUiAutomationMappingEnabled>
<ResourcesDirectory>..\SamplesApp.Shared\Strings</ResourcesDirectory>
<!-- https://github.com/unoplatform/uno/issues/61 -->
<UnoSkipUserControlsInVisualTree>false</UnoSkipUserControlsInVisualTree>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|iPhoneSimulator' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -179,4 +177,4 @@
<Import Project="..\SamplesApp.UnitTests.Shared\SamplesApp.UnitTests.Shared.projitems" Label="Shared" />
<Import Project="..\UITests.Shared\UITests.Shared.projitems" Label="Shared" />
<Import Project="$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.iOS.CSharp.targets" />
</Project>
</Project>
1 change: 0 additions & 1 deletion src/SamplesApp/SamplesApp.macOS/SamplesApp.macOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<TargetFrameworkIdentifier>Xamarin.Mac</TargetFrameworkIdentifier>
<MonoMacResourcePrefix>Resources</MonoMacResourcePrefix>
<UnoSkipUserControlsInVisualTree>false</UnoSkipUserControlsInVisualTree>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|iPhoneSimulator' ">
<DebugSymbols>true</DebugSymbols>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,30 @@
<SolidColorBrush x:Key="myStaticResource" Color="Purple" />
</UserControl.Resources>

<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="DefaultState"/>
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="DefaultState"/>

<VisualState x:Name="State01">
<VisualState.Setters>
<!-- Element name binding -->
<Setter Target="border01_bound.Background" Value="{Binding Background, ElementName=border01}"/>
<VisualState x:Name="State01">
<VisualState.Setters>
<!-- Element name binding -->
<Setter Target="border01_bound.Background" Value="{Binding Background, ElementName=border01}"/>

<!-- Static resource -->
<Setter Target="border02.Background" Value="{StaticResource myStaticResource}"/>
<!-- Static resource -->
<Setter Target="border02.Background" Value="{StaticResource myStaticResource}"/>

<Setter Target="border03.Background">
<Setter.Value>
<SolidColorBrush Color="Orange" />
</Setter.Value>
</Setter>
<Setter Target="border03.Background">
<Setter.Value>
<SolidColorBrush Color="Orange" />
</Setter.Value>
</Setter>

</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>

<Grid>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>

<StackPanel>
<Button x:Name="changeState" Content="Change State" Click="OnClick" />
<Border x:Name="border01" Background="Red" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:xamarin="http://platform.uno/xamarin"
xmlns:not_xamarin="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
mc:Ignorable="d xamarin"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

Expand Down Expand Up @@ -52,6 +51,4 @@
<TextBlock x:Name="_output" />
</ScrollViewer>
</xamarin:StackPanel>

<not_xamarin:TextBlock Text="Updates mode is specific to Uno" />
</Page>
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ internal partial class XamlCodeGeneration

// Determines if the source generator will skip the inclusion of UseControls in the
// visual tree. See https://github.com/unoplatform/uno/issues/61
private bool _skipUserControlsInVisualTree = true;
private bool _skipUserControlsInVisualTree = false;
davidjohnoliver marked this conversation as resolved.
Show resolved Hide resolved

#pragma warning disable 649 // Unused member
private readonly bool _forceGeneration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ private string InnerGenerateFile()

using (writer.BlockInvariant("public partial class {0} : {1}", _className.className, controlBaseType.ToDisplayString()))
{
var isDirectUserControlChild = IsUserControl(topLevelControl.Type, checkInheritance: false);
var isDirectUserControlChild = _skipUserControlsInVisualTree && IsUserControl(topLevelControl.Type, checkInheritance: false);

using (Scope("{0}{1}".InvariantCultureFormat(_className.ns.Replace(".", ""), _className.className)))
{
Expand Down Expand Up @@ -701,7 +701,7 @@ private string BuildControlInitializerDeclaration(string className, XamlObjectDe
{
return "protected override void InitializeComponent()";
}
else if (IsUserControl(topLevelControl.Type, checkInheritance: false))
else if (_skipUserControlsInVisualTree && IsUserControl(topLevelControl.Type, checkInheritance: false))
{
string contentTypeDisplayString = GetImplicitChildTypeDisplayString(topLevelControl);

Expand Down Expand Up @@ -1706,7 +1706,7 @@ private bool BuildProperties(IIndentedStringBuilder writer, XamlObjectDefinition
}
}
}
else if (IsUserControl(topLevelControl.Type))
else if (_skipUserControlsInVisualTree && IsUserControl(topLevelControl.Type))
{
if (implicitContentChild.Objects.Any())
{
Expand Down Expand Up @@ -1929,7 +1929,7 @@ implicitContentChild.Value is string implicitValue

return true;
}
else if (returnsContent && IsUserControl(topLevelControl.Type))
else if (returnsContent && _skipUserControlsInVisualTree && IsUserControl(topLevelControl.Type))
{
writer.AppendFormatInvariant(XamlConstants.Types.IFrameworkElement + " content = null");
}
Expand Down
5 changes: 3 additions & 2 deletions src/Uno.UI.Tests/Uno.UI.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="MSTest.TestAdapter"/>
<PackageReference Include="MSTest.TestFramework"/>
<PackageReference Include="MSTest.TestAdapter" />
<PackageReference Include="MSTest.TestFramework" />
<PackageReference Include="FluentAssertions">
<Version>5.10.3</Version>
</PackageReference>
Expand Down Expand Up @@ -94,6 +94,7 @@
</ItemGroup>
<ItemGroup Condition="'$(BuildingInsideUnoSourceGenerator)'!=''">
<Page Include="Windows_UI_Xaml_Data\**\*.xaml" />
<Page Include="Windows_UI_Xaml_Controls\**\*.xaml" />
<Page Include="App\App.xaml" />
<Page Include="App\Xaml\Test_CreateFromString.xaml" />
<Page Include="App\Xaml\Test_Dictionary.xaml" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Uno.UI.Tests.Windows_UI_XAML_Controls.UserControlTests;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;

namespace Uno.UI.Tests.Windows_UI_Xaml_Controls.xLoad
{
[TestClass]
public class Given_UserControl
{
[TestMethod]
public void When_UserControl_TopLevel_Binding()
{
var sut = new UserControl_TopLevelBinding();
sut.ForceLoaded();

var uc01 = sut.FindName("uc01");

Assert.AreEqual(0, UserControl_TopLevelBinding_AttachedProperty.MyPropertyChangedCount);
Assert.AreEqual(0, UserControl_TopLevelBinding_AttachedProperty.GetMyProperty(uc01));

sut.DataContext = 42;

Assert.AreEqual(1, UserControl_TopLevelBinding_AttachedProperty.MyPropertyChangedCount);
Assert.AreEqual(42, UserControl_TopLevelBinding_AttachedProperty.GetMyProperty(uc01));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Page
x:Class="Uno.UI.Tests.Windows_UI_XAML_Controls.UserControlTests.UserControl_TopLevelBinding"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Uno.UI.Tests.Windows_UI_XAML_Controls.UserControlTests"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid x:Name="test01">
<local:UserControl_TopLevelBinding_UserControl x:Name="uc01" />
</Grid>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238

namespace Uno.UI.Tests.Windows_UI_XAML_Controls.UserControlTests
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class UserControl_TopLevelBinding : Page
{
public UserControl_TopLevelBinding()
{
this.InitializeComponent();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<UserControl
x:Class="Uno.UI.Tests.Windows_UI_XAML_Controls.UserControlTests.UserControl_TopLevelBinding_UserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Uno.UI.Tests.Windows_UI_XAML_Controls.UserControlTests"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
local:UserControl_TopLevelBinding_AttachedProperty.MyProperty="{Binding}"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid>

</Grid>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238

namespace Uno.UI.Tests.Windows_UI_XAML_Controls.UserControlTests
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class UserControl_TopLevelBinding_UserControl : UserControl
{
public UserControl_TopLevelBinding_UserControl()
{
this.InitializeComponent();
}
}

public static class UserControl_TopLevelBinding_AttachedProperty
{
public static int GetMyProperty(DependencyObject obj)
{
return (int)obj.GetValue(MyPropertyProperty);
jeromelaban marked this conversation as resolved.
Show resolved Hide resolved
}

public static void SetMyProperty(DependencyObject obj, int value)
{
obj.SetValue(MyPropertyProperty, value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.RegisterAttached("MyProperty", typeof(int), typeof(UserControl_TopLevelBinding_AttachedProperty), new PropertyMetadata(0, OnMyPropertyChanged));

public static int MyPropertyChangedCount { get; private set; }

private static void OnMyPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
{
MyPropertyChangedCount++;
}
}
}