Skip to content

Commit

Permalink
fix: Cast to FrameworkElement/Window when accessing Loading/Activated…
Browse files Browse the repository at this point in the history
… to avoid issues when shadowing
  • Loading branch information
Youssef1313 committed Aug 29, 2024
1 parent 98c2aec commit 5648bea
Show file tree
Hide file tree
Showing 27 changed files with 541 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,103 @@ public MainPage()

await test.RunAsync();
}

[TestMethod]
public async Task TestMarkupExtensionWithShadowingFrameworkElementLoading()
{
var xamlFile = new XamlFile("MainPage.xaml", """
<local:BasePage
x:Class="TestRepro.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestRepro"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">
</local:BasePage>
""");

var test = new Verify.Test(xamlFile)
{
TestState =
{
Sources =
{
"""
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
namespace TestRepro
{
public partial class BasePage : Page
{
public string Loading => "";
}
public sealed partial class MainPage : BasePage
{
public MainPage()
{
this.InitializeComponent();
}
}
}
"""
}
}
}.AddGeneratedSources();

await test.RunAsync();
}

[TestMethod]
public async Task TestMarkupExtensionWithShadowingWindowActivated()
{
var xamlFile = new XamlFile("MainWindow.xaml", """
<local:BaseWindow
x:Class="TestRepro.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestRepro"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" />
</local:BaseWindow>
""");

var test = new Verify.Test(xamlFile)
{
TestState =
{
Sources =
{
"""
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
namespace TestRepro
{
public partial class BaseWindow : Window
{
public string Activated => "";
}
public sealed partial class MainWindow : BaseWindow
{
public MainWindow()
{
this.InitializeComponent();
}
}
}
"""
}
}
}.AddGeneratedSources();

await test.RunAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ private void InitializeComponent()

Bindings = ((IMainPage_Bindings)global::Uno.UI.Helpers.TypeMappings.CreateInstance<MainPage_Bindings>(this));
global::Uno.UI.Helpers.MarkupHelper.SetElementProperty(__that, "owner", __that);
Loading += (s, e) =>
((global::Microsoft.UI.Xaml.FrameworkElement)this).Loading += (s, e) =>
{
var __that = global::Uno.UI.Helpers.MarkupHelper.GetElementProperty<global::TestRepro.MainPage>(s, "owner");
__that.Bindings.UpdateResources();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private void InitializeComponent()

Bindings = ((IMainPage_Bindings)global::Uno.UI.Helpers.TypeMappings.CreateInstance<MainPage_Bindings>(this));
global::Uno.UI.Helpers.MarkupHelper.SetElementProperty(__that, "owner", __that);
Loading += (s, e) =>
((global::Microsoft.UI.Xaml.FrameworkElement)this).Loading += (s, e) =>
{
var __that = global::Uno.UI.Helpers.MarkupHelper.GetElementProperty<global::TestRepro.MainPage>(s, "owner");
__that.Bindings.UpdateResources();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private void InitializeComponent()

Bindings = ((IMainPage_Bindings)global::Uno.UI.Helpers.TypeMappings.CreateInstance<MainPage_Bindings>(this));
global::Uno.UI.Helpers.MarkupHelper.SetElementProperty(__that, "owner", __that);
Loading += (s, e) =>
((global::Microsoft.UI.Xaml.FrameworkElement)this).Loading += (s, e) =>
{
var __that = global::Uno.UI.Helpers.MarkupHelper.GetElementProperty<global::TestRepro.MainPage>(s, "owner");
__that.Bindings.UpdateResources();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private void InitializeComponent()

Bindings = ((IMainPage_Bindings)global::Uno.UI.Helpers.TypeMappings.CreateInstance<MainPage_Bindings>(this));
global::Uno.UI.Helpers.MarkupHelper.SetElementProperty(__that, "owner", __that);
Loading += (s, e) =>
((global::Microsoft.UI.Xaml.FrameworkElement)this).Loading += (s, e) =>
{
var __that = global::Uno.UI.Helpers.MarkupHelper.GetElementProperty<global::TestRepro.MainPage>(s, "owner");
__that.Bindings.UpdateResources();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private void InitializeComponent()
OnInitializeCompleted();

Bindings = new MainPage_Bindings(this);
Loading += (s, e) =>
((global::Microsoft.UI.Xaml.FrameworkElement)this).Loading += (s, e) =>
{
__that.Bindings.Update();
__that.Bindings.UpdateResources();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private void InitializeComponent()
OnInitializeCompleted();

Bindings = new MainPage_Bindings(this);
Loading += (s, e) =>
((global::Microsoft.UI.Xaml.FrameworkElement)this).Loading += (s, e) =>
{
__that.Bindings.UpdateResources();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private void InitializeComponent()
OnInitializeCompleted();

Bindings = new MainPage_Bindings(this);
Loading += (s, e) =>
((global::Microsoft.UI.Xaml.FrameworkElement)this).Loading += (s, e) =>
{
__that.Bindings.Update();
__that.Bindings.UpdateResources();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private void InitializeComponent()
OnInitializeCompleted();

Bindings = new MainPage_Bindings(this);
Loading += (s, e) =>
((global::Microsoft.UI.Xaml.FrameworkElement)this).Loading += (s, e) =>
{
__that.Bindings.Update();
__that.Bindings.UpdateResources();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// <autogenerated />
namespace MyProject
{
/// <summary>
/// Contains all the static resources defined for the application
/// </summary>
public sealed partial class GlobalStaticResources
{
static bool _initialized;
private static bool _stylesRegistered;
private static bool _dictionariesRegistered;
internal static global::Uno.UI.Xaml.XamlParseContext __ParseContext_ { get; } = new global::Uno.UI.Xaml.XamlParseContext()
{
AssemblyName = "TestProject",
}
;

static GlobalStaticResources()
{
Initialize();
}
public static void Initialize()
{
if (!_initialized)
{
_initialized = true;
global::Uno.UI.GlobalStaticResources.Initialize();
global::Uno.UI.GlobalStaticResources.RegisterDefaultStyles();
global::Uno.UI.GlobalStaticResources.RegisterResourceDictionariesBySource();
}
}
public static void RegisterDefaultStyles()
{
if(!_stylesRegistered)
{
_stylesRegistered = true;
RegisterDefaultStyles_MainPage_d6cd66944958ced0c513e0a04797b51d();
}
}
// Register ResourceDictionaries using ms-appx:/// syntax, this is called for external resources
public static void RegisterResourceDictionariesBySource()
{
if(!_dictionariesRegistered)
{
_dictionariesRegistered = true;
}
}
// Register ResourceDictionaries using ms-resource:/// syntax, this is called for local resources
internal static void RegisterResourceDictionariesBySourceLocal()
{
}
static partial void RegisterDefaultStyles_MainPage_d6cd66944958ced0c513e0a04797b51d();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// <auto-generated />
[assembly: global::System.Reflection.AssemblyMetadata("UnoHasLocalizationResources", "False")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// <autogenerated />
#pragma warning disable CS0114
#pragma warning disable CS0108
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Uno.UI;
using Uno.UI.Xaml;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Documents;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Animation;
using Microsoft.UI.Xaml.Shapes;
using Windows.UI.Text;
using Uno.Extensions;
using Uno;
using Uno.UI.Helpers;
using Uno.UI.Helpers.Xaml;
using MyProject;

#if __ANDROID__
using _View = Android.Views.View;
#elif __IOS__
using _View = UIKit.UIView;
#elif __MACOS__
using _View = AppKit.NSView;
#else
using _View = Microsoft.UI.Xaml.UIElement;
#endif

namespace TestRepro
{
partial class MainPage : global::TestRepro.BasePage
{
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
private const string __baseUri_prefix_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/";
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
private const string __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/";
private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope();
private void InitializeComponent()
{
NameScope.SetNameScope(this, __nameScope);
var __that = this;
base.IsParsing = true;
// Source 0\MainPage.xaml (Line 1:2)
;

this
.GenericApply(((c0) =>
{
// Source 0\MainPage.xaml (Line 1:2)

// WARNING Property c0.base does not exist on {using:TestRepro}BasePage, the namespace is http://www.w3.org/XML/1998/namespace. This error was considered irrelevant by the XamlFileGenerator
}
))
.GenericApply(((c1) =>
{
/* _isTopLevelDictionary:False */
__that._component_0 = c1;
// Class TestRepro.MainPage
global::Uno.UI.ResourceResolverSingleton.Instance.ApplyResource(c1, global::TestRepro.BasePage.BackgroundProperty, "ApplicationPageBackgroundThemeBrush", isThemeResourceExtension: true, isHotReloadSupported: false, context: global::MyProject.GlobalStaticResources.__ParseContext_);
global::Uno.UI.FrameworkElementHelper.SetBaseUri(c1, __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d);
c1.CreationComplete();
}
))
;
OnInitializeCompleted();

Bindings = new MainPage_Bindings(this);
((global::Microsoft.UI.Xaml.FrameworkElement)this).Loading += (s, e) =>
{
__that.Bindings.UpdateResources();
}
;
}
partial void OnInitializeCompleted();
private global::Microsoft.UI.Xaml.Markup.ComponentHolder _component_0_Holder = new global::Microsoft.UI.Xaml.Markup.ComponentHolder(isWeak: true);
private global::TestRepro.BasePage _component_0
{
get
{
return (global::TestRepro.BasePage)_component_0_Holder.Instance;
}
set
{
_component_0_Holder.Instance = value;
}
}
private interface IMainPage_Bindings
{
void Initialize();
void Update();
void UpdateResources();
void StopTracking();
void NotifyXLoad(string name);
}
#pragma warning disable 0169 // Suppress unused field warning in case Bindings is not used.
private IMainPage_Bindings Bindings;
#pragma warning restore 0169
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
private class MainPage_Bindings : IMainPage_Bindings
{
#if UNO_HAS_UIELEMENT_IMPLICIT_PINNING
private global::System.WeakReference _ownerReference;
private global::TestRepro.MainPage Owner { get => (global::TestRepro.MainPage)_ownerReference?.Target; set => _ownerReference = new global::System.WeakReference(value); }
#else
private global::TestRepro.MainPage Owner { get; set; }
#endif
public MainPage_Bindings(global::TestRepro.MainPage owner)
{
Owner = owner;
}
void IMainPage_Bindings.NotifyXLoad(string name)
{
}
void IMainPage_Bindings.Initialize()
{
}
void IMainPage_Bindings.Update()
{
var owner = Owner;
}
void IMainPage_Bindings.UpdateResources()
{
var owner = Owner;
owner._component_0.UpdateResourceBindings(resourceContextProvider: null);
}
void IMainPage_Bindings.StopTracking()
{
}
}
}
}
namespace MyProject
{
static class MainPage_d6cd66944958ced0c513e0a04797b51dXamlApplyExtensions
{
}
}
Loading

0 comments on commit 5648bea

Please sign in to comment.