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

Misc bugs around Xaml resources & code gen #1112

Merged
merged 7 commits into from
Jun 21, 2019
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
5 changes: 4 additions & 1 deletion doc/ReleaseNotes/_ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
* Basic support for `Windows.Devices.Sensors.Barometer`

### Breaking changes
*
*

### Bug fixes
* [iOS] Area of view outside Clip rect now allows touch to pass through, this fixes NavigationView not allowing touches to children (#1018)
* `ComboBox` drop down is now placed following a logic which is closer to UWP and it longer flickers when it appears (especilly on WASM)
* #854 `BasedOn` on a `<Style>` in `App.Xaml` were not resolving properly
* #706 `x:Name` in `App.Xaml`'s resources were crashing the compilation.
* #846 `x:Name` on non-`DependencyObject` resources were crashing the compilation

## Release 1.45.0
### Features
Expand Down
38 changes: 38 additions & 0 deletions src/SamplesApp/SamplesApp.Shared/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,42 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
RequestedTheme="Light">

<Application.Resources>

<!-- ReSharper disable once InconsistentNaming -->
<SolidColorBrush x:Key="Pink_From_App_Xaml_Resources" Color="#FF54BA" />

<Style x:Key="Style_Bold_From_App_Xaml_Resources" TargetType="TextBlock">
<Setter Property="FontWeight" Value="ExtraBold" />
</Style>

<Style x:Key="Style_Pink_From_App_Xaml_Resources" TargetType="TextBlock" BasedOn="{StaticResource Style_Bold_From_App_Xaml_Resources}">
<Setter Property="Foreground" Value="{StaticResource Pink_From_App_Xaml_Resources}" />
</Style>

<Style x:Key="CustomTextBox" TargetType="TextBox" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Grid>
<Grid.Resources>
<Style x:Key="TestStyle" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<TextBlock Text="Sample" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

</Application.Resources>
</Application>
5 changes: 2 additions & 3 deletions src/SamplesApp/SamplesApp.Shared/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ public App()
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
var sw = Stopwatch.StartNew();
var n = Windows.UI.Xaml.Window.Current.Dispatcher.RunAsync(
CoreDispatcherPriority.Idle,
() => Console.WriteLine("Done loading " + sw.Elapsed));
var n = Windows.UI.Xaml.Window.Current.Dispatcher.RunIdleAsync(
_ => Console.WriteLine("Done loading " + sw.Elapsed));

#if DEBUG
if (System.Diagnostics.Debugger.IsAttached)
Expand Down
13 changes: 12 additions & 1 deletion src/SamplesApp/UITests.Shared/UITests.Shared.projitems
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
Expand Down Expand Up @@ -113,6 +113,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml\Resources\XamlGlobalResources.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml\UIElementTests\Arrange_Performance01.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down Expand Up @@ -2117,6 +2121,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml\TouchEventsTests\Touch.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml\TouchEventsTests\TouchRotated.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml\TouchEventsTests\TouchViewModel.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml\Resources\XamlGlobalResources.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml\UIElementTests\Arrange_Performance01.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml\UIElementTests\TransformToVisual_Simple.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml\VisualStateTests\VisualState_AdaptiveTrigger_Storyboard.xaml.cs" />
Expand Down Expand Up @@ -3706,6 +3711,12 @@
<Compile Update="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\ToggleSwitchControl\ToggleSwitch_IsOn.xaml.cs">
<DependentUpon>ToggleSwitch_IsOn.xaml</DependentUpon>
</Compile>
<Compile Update="C:\src\Uno\src\SamplesApp\UITests.Shared\Windows_UI_Xaml\Resources\XamlGlobalResources.xaml.cs">
<DependentUpon>XamlGlobalResources.xaml</DependentUpon>
</Compile>
<Compile Update="C:\src\Uno\src\SamplesApp\UITests.Shared\Windows_UI_Xaml\Resources\XamlLocalResources.xaml.cs">
<DependentUpon>XamlLocalResources.xaml</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<Content Include="$(MSBuildThisFileDirectory)Assets\cart.png" />
Expand Down
4 changes: 2 additions & 2 deletions src/SamplesApp/UITests.Shared/UITests.Shared.shproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="Globals">
<ProjectGuid>beb0ab3a-16a7-49cf-ad5e-f4a53b985afe</ProjectGuid>
Expand All @@ -10,4 +10,4 @@
<PropertyGroup />
<Import Project="UITests.Shared.projitems" Label="Shared" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" />
</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Page
x:Class="UITests.Shared.Windows_UI_Xaml.Resources.XamlGlobalResources"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
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
Background="White"
BorderBrush="{StaticResource Pink_From_App_Xaml_Resources}"
BorderThickness="4"
Padding="2"
Margin="20"
VerticalAlignment="Center"
HorizontalAlignment="Center">

<TextBlock
Style="{StaticResource Style_Pink_From_App_Xaml_Resources}"
TextWrapping="WrapWholeWords"
TextAlignment="Justify"
FontSize="55">This text should be rendered in PINK with a PINK border around it.</TextBlock>

<TextBox Style="{StaticResource CustomTextBox}" />
</Grid>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Documents;
using Uno.UI.Samples.Controls;

namespace UITests.Shared.Windows_UI_Xaml.Resources
{
[SampleControlInfo("XAML", "XamlGlobalResources", description: "Using a resource defined in Global.Xaml's Resources (Pink).")]
public sealed partial class XamlGlobalResources : Page
{
public XamlGlobalResources()
{
this.InitializeComponent();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Uno.UI.SourceGenerators.XamlGenerator.Utils;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -73,13 +74,15 @@ internal partial class XamlFileGenerator
private readonly INamedTypeSymbol _stringSymbol;
private readonly INamedTypeSymbol _objectSymbol;
private readonly INamedTypeSymbol _iFrameworkElementSymbol;
private readonly INamedTypeSymbol _dependencyObjectSymbol;

private readonly INamedTypeSymbol _iCollectionSymbol;
private readonly INamedTypeSymbol _iCollectionOfTSymbol;
private readonly INamedTypeSymbol _iListSymbol;
private readonly INamedTypeSymbol _iListOfTSymbol;
private readonly INamedTypeSymbol _iDictionaryOfTKeySymbol;
private readonly INamedTypeSymbol _dataBindingSymbol;
private readonly INamedTypeSymbol _styleSymbol;

private readonly bool _isWasm;

Expand Down Expand Up @@ -130,12 +133,14 @@ bool isDebug
_elementStubSymbol = GetType(XamlConstants.Types.ElementStub);
_contentPresenterSymbol = GetType(XamlConstants.Types.ContentPresenter);
_iFrameworkElementSymbol = GetType(XamlConstants.Types.IFrameworkElement);
_dependencyObjectSymbol = GetType(XamlConstants.Types.DependencyObject);
_iCollectionSymbol = GetType("System.Collections.ICollection");
_iCollectionOfTSymbol = GetType("System.Collections.Generic.ICollection`1");
_iListSymbol = GetType("System.Collections.IList");
_iListOfTSymbol = GetType("System.Collections.Generic.IList`1");
_iDictionaryOfTKeySymbol = GetType("System.Collections.Generic.IDictionary`2");
_dataBindingSymbol = GetType("Windows.UI.Xaml.Data.Binding");
_styleSymbol = GetType(XamlConstants.Types.Style);

_isWasm = isWasm;
}
Expand Down Expand Up @@ -855,22 +860,39 @@ IEnumerable<KeyValuePair<string, XamlObjectDefinition>> namedResources
writer.AppendLineInvariant(0, ";", namedResource.Value.Type);
}

if (namedResources.Any())
bool IsGenerateCompiledBindings(KeyValuePair<string, XamlObjectDefinition> nr)
{
var type = GetType(nr.Value.Type);

// Styles are handled differently for now, and there's no variable generated
// for those entries. Skip the ApplyCompiledBindings for those. See
// ImportResourceDictionary handling of x:Name for more details.
if (type.Equals(_styleSymbol))
{
return false;
}

if (type.AllInterfaces.Any(i => i.Equals(_dependencyObjectSymbol)))
{
return true;
}

return false;
}

var resourcesTogenerateApplyCompiledBindings = namedResources
.Where(IsGenerateCompiledBindings)
.ToArray();

if (resourcesTogenerateApplyCompiledBindings.Any())
{
using (writer.BlockInvariant("Loading += (s, e) =>"))
{
foreach (var namedResource in namedResources)
foreach (var namedResource in resourcesTogenerateApplyCompiledBindings)
{
if (namedResource.Value.Type.Name == "Style")
{
// Styles are handled differently for now, and there's no variable generated
// for those entries. Skip the ApplyCompiledBindings for those. See
// ImportResourceDictionary handling of x:Name for more details.
continue;
}

var type = GetType(namedResource.Value.Type);

writer.AppendFormatInvariant($"{namedResource.Key}.ApplyCompiledBindings();");
writer.AppendLineInvariant($"{namedResource.Key}.ApplyCompiledBindings();");
}
}
writer.AppendLineInvariant(0, ";");
Expand Down Expand Up @@ -964,7 +986,7 @@ IEnumerable<KeyValuePair<string, XamlObjectDefinition>> keyedResources
}
else
{
// If there is no method suffix,
// If there is no method suffix,
using (writer.BlockInvariant("public static object FindResource(string name)"))
{
BuildGetResources(writer, keyedResources);
Expand Down Expand Up @@ -1680,7 +1702,7 @@ private void RegisterResources(XamlObjectDefinition topLevelControl)

if (isExplicitResDictionary)
{
// To be able to have MergedDictionaries, the first node of the Resource node
// To be able to have MergedDictionaries, the first node of the Resource node
// must be an explicit resource dictionary.

resourcesMember = FindImplicitContentMember(resourcesMember.Objects.First());
Expand Down Expand Up @@ -1733,8 +1755,8 @@ private void TryExtractAutomationId(XamlMemberDefinition member, string[] target

var fullMemberName = $"{FindType(member.Member.DeclaringType)?.GetFullMetadataName()}.{member.Member.Name}";

// Try to match each potential candidate by comparing based only on the name of the member first.
// If that fails, try matching based on the full metadata name of the member
// Try to match each potential candidate by comparing based only on the name of the member first.
// If that fails, try matching based on the full metadata name of the member
var hasUiAutomationMapping = targetMembers
.Any(candidateMember =>
(!candidateMember.Contains(".") && candidateMember == member.Member.Name) ||
Expand All @@ -1754,7 +1776,7 @@ private void TryExtractAutomationId(XamlMemberDefinition member, string[] target

string bindingPath;

// Checks the first binding member, which can be used to implicitlty declare the binding path (i.e. without
// Checks the first binding member, which can be used to implicitlty declare the binding path (i.e. without
// declaring a "Path=" specifier). Otherwise, the we look for any explicit binding path declaration.
var firstBindingMember = bindingMembers?.FirstOrDefault();
if (firstBindingMember != null &&
Expand Down Expand Up @@ -2164,7 +2186,7 @@ private void TryValidateContentPresenterBinding(IIndentedStringBuilder writer, X
var hasRelativeSource = binding.Members
.Any(m =>
m.Member.Name == "RelativeSource"
// It can either be TemplatedParent or Self. In either cases, it does not use the inherited
// It can either be TemplatedParent or Self. In either cases, it does not use the inherited
// DataContext, which falls outside of the scenario we want to avoid.
);

Expand Down Expand Up @@ -2231,9 +2253,9 @@ private XamlLazyApplyBlockIIndentedStringBuilder CreateApplyBlock(IIndentedStrin
closureName = "c" + (_applyIndex++).ToString(CultureInfo.InvariantCulture);

//
// Since we're using strings to generate the code, we can't know ahead of time if
// Since we're using strings to generate the code, we can't know ahead of time if
// content will be generated only by looking at the Xaml object model.
// For now, we only observe if the inner code has generated code, and we create
// For now, we only observe if the inner code has generated code, and we create
// the apply block at that time.
//
string delegateType = null;
Expand Down Expand Up @@ -2606,7 +2628,7 @@ private string BuildLiteralValue(INamedTypeSymbol propertyType, string memberVal

throw new Exception("Unable to convert {0} for {1} with type {2}".InvariantCultureFormat(memberValue, memberName, propertyType));
}

private string BuildLocalizedResourceValue(XamlMemberDefinition owner, string memberName, string objectUid)
{
var uidParts = objectUid.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
Expand Down Expand Up @@ -2862,25 +2884,23 @@ private string GetGlobalStaticResource(string resourceName, INamedTypeSymbol tar
{
return $"global::{resource.Namespace}.GlobalStaticResources.{SanitizeResourceName(resourceName)}";
}
else if (_staticResources.ContainsKey(resourceName))

if (_staticResources.ContainsKey(resourceName))
{
return $"{GetCastString(targetType, _staticResources[resourceName])}StaticResources.{SanitizeResourceName(resourceName)}";
}

var valueString = $"(global::Windows.UI.Xaml.Application.Current.Resources[\"{resourceName}\"] ?? throw new InvalidOperationException(\"The resource {resourceName} cannot be found\"))";

if (targetType != null)
{
// We do not know the type of the source, and it must be converted first
return $"global::Windows.UI.Xaml.Markup.XamlBindingHelper.ConvertValue(typeof{GetCastString(targetType, null)}, {valueString})";
}
else
{
var validateString = _isDebug ? $" ?? throw new InvalidOperationException(\"The resource {resourceName} cannot be found\")" : "";
var valueString = $"(global::Windows.UI.Xaml.Application.Current.Resources[\"{resourceName}\"]{validateString})";

if (targetType != null)
{
// We do not know the type of the source, and it must be converted first
return $"global::Windows.UI.Xaml.Markup.XamlBindingHelper.ConvertValue(typeof{GetCastString(targetType, null)}, {valueString})";
}
else
{

return valueString;
}
return valueString;
}
}

Expand Down Expand Up @@ -3032,7 +3052,7 @@ private void BuildLiteralProperties(IIndentedStringBuilder writer, XamlObjectDef
}
else if (member.Member.DeclaringType == null && member.Member.Name == "Name")
{
// This is a special case, where the declaring type is from the x: namespace,
// This is a special case, where the declaring type is from the x: namespace,
// but is considered of an unknown type. This can happen when providing the
// name of a control using x:Name instead of Name.
var hasNameProperty = HasProperty(objectDefinition.Type, "Name");
Expand Down Expand Up @@ -3199,7 +3219,7 @@ private void BuildChild(IIndentedStringBuilder writer, XamlMemberDefinition owne

if (contentOwner != null)
{
// This case is to support the layout switching for the ListViewBaseLayout, which is not
// This case is to support the layout switching for the ListViewBaseLayout, which is not
// a FrameworkTemplate. Thsi will need to be removed when this custom list view is removed.
var returnType = typeName == "ListViewBaseLayoutTemplate" ? "Uno.UI.Controls.Legacy.ListViewBaseLayout" : "_View";

Expand Down Expand Up @@ -3298,7 +3318,7 @@ private void BuildChild(IIndentedStringBuilder writer, XamlMemberDefinition owne
}
}

// Attached properties need to be expanded using the namespace, otherwise the resolution will be
// Attached properties need to be expanded using the namespace, otherwise the resolution will be
// performed at runtime at a higher cost.
propertyName = RewriteAttachedPropertyPath(propertyName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@
<None Update="Localization_Implicit.xaml">
<Generator>MSBuild:Compile</Generator>
</None>
<None Update="XamlLocalResources.xaml">
<Generator>MSBuild:Compile</Generator>
</None>
</ItemGroup>


Expand Down
Loading