Shell SizeChanged event not invoked / Size not set
- Dominant language
- C#
- Stars
- 23.3k
- Forks
- 2k
- Avg merge
- 1d 15h
- Merged PRs (30d)
- 290
Description
### Description
The `Microsoft.Maui.Controls.Shell.SizeChanged` even inherited from `Microsoft.Maui.Controls.VisualElement.SizeChanged` is not invoked after resizing the window.
Possibly due to `Shell.Width` and `Shell.Height` not being set. (they remain at `-1`)
### Steps to Reproduce
1. `File` -> `New` -> `Project...`
2. Select `A project for creating .NET MAUI application for iOS, Android, Mac, Catalyst, Tizen and WinUI.` -> `Next` -> `Create`
3. Open `AppShell.xaml.cs` and paste:
```c#
namespace MauiAppBugOnSizeChange;
public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();
SizeChanged += AppShell_SizeChanged;
}
private void AppShell_SizeChanged(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("Shell size changed!");
System.Diagnostics.Debug.WriteLine($"Width: {this.Width}\t WidthRequest: {this.WidthRequest}");
}
}
```
4. (Optional) Add a breakpoint inside the `AppShell_SizeChanged()` function.
5. `F5 Start Debuging` (Windows Machine)
6. Resize the application window
### Version with bug
Release Candidate 3 (current)
### Last version that worked well
Unknown/Other
### Affected platforms
Windows
### Affected platform versions
Windows 21H2 (OS Build 19044.1645)
### Did you find any workaround?
The `SizeChanged` event in `ContentPage` does work.
I've tried linking this event to the `Shell` object's event as follows
```c#
private void MainPage_SizeChanged(object sender, EventArgs e)
{
try
{
AppShell shell = (AppShell)Application.Current.MainPage;
shell.AppShell_SizeChanged(sender, e);
}
catch (NullReferenceException) { }
}
```
While this does _sort of_ work it it wasn't a solution to my use case. (#7161)
It did however give a possible explanation as to why this event is not fired.
The `Width` and `WidthRequest` properties of a `Shell` object remain at `-1`.
Thus @jsuarezruiz implementation https://github.com/dotnet/maui/commit/250021582ca9e08395f0d7fd61f0a80525f85259 / #4541 isn't processed.
### Relevant log output
_No response_
Contributor guide
Research direction
Start with the AppShell.xaml.cs reproduction on a Windows Machine and compare Shell.SizeChanged with the working ContentPage.SizeChanged behavior. Inspect the Shell sizing path related to #4541 and commit 250021582ca9e08395f0d7fd61f0a80525f85259; done means resizing invokes the Shell event and updates its Width and Height.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- desktop, frontend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100