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

Error when OnPlatform doesn't match #17562

Open
halfninja opened this issue Nov 19, 2024 · 1 comment
Open

Error when OnPlatform doesn't match #17562

halfninja opened this issue Nov 19, 2024 · 1 comment

Comments

@halfninja
Copy link

Describe the bug

I'd written a style with an override for when the window is maximised in Windows:

<Style Selector="ToggleButton:checked ContentPresenter">
    <Setter Property="BorderThickness" Value="3 0 0 0" />                  
</Style>
<OnPlatform>
    <OnPlatform.Windows>
        <Style Selector="Window[WindowState=Maximized]">
            <Style Selector="^ ToggleButton:checked ContentPresenter">
                <Setter Property="BorderThickness" Value="10 0 0 0" />                  
            </Style>
        </Style>
    </OnPlatform.Windows>
</OnPlatform>

Works fine on Windows. When I start the app on Macos (or probably any other platform) I get this ominous exception

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at Avalonia.Styling.Styles.TryGetResource(Object key, ThemeVariant theme, Object& value)
   at Avalonia.Application.TryGetResource(Object key, ThemeVariant theme, Object& value)
   at Avalonia.Controls.ResourceNodeExtensions.TryFindResource(IResourceHost control, Object key, ThemeVariant theme, Object& value)
   at Avalonia.Controls.ResourceNodeExtensions.TryFindResource(IResourceHost control, Object key, Object& value)
   at Avalonia.StyledElement.GetEffectiveTheme()
   at Avalonia.StyledElement.ApplyControlTheme()
   at Avalonia.StyledElement.ApplyStyling()
   at Avalonia.StyledElement.EndInit()
   at MyApp.Views.TestWindow.!XamlIlPopulate(IServiceProvider, TestWindow) in /Users/self/Documents/workspace/app/App/Views/TestWindow.axaml:line 7
   at MyApp.Views.TestWindow.!XamlIlPopulateTrampoline(TestWindow)
   at MyApp.Views.TestWindow.InitializeComponent(Boolean loadXaml, Boolean attachDevTools) in /Users/self/Documents/workspace/app/App/obj/Debug/net8.0/Avalonia.Generators/Avalonia.Generators.NameGenerator.AvaloniaNameSourceGenerator/MyApp.Views.TestWindow.g.cs:line 25
   at MyApp.Views.TestWindow..ctor() in /Users/self/Documents/workspace/app/App/Views/TestWindow.axaml.cs:line 11
   at MyApp.App.OnFrameworkInitializationCompleted() in /Users/self/Documents/workspace/app/App/App.axaml.cs:line 117
   at Avalonia.AppBuilder.SetupUnsafe()
   at Avalonia.AppBuilder.Setup()
   at Avalonia.AppBuilder.SetupWithLifetime(IApplicationLifetime lifetime)
   at Avalonia.ClassicDesktopStyleApplicationLifetimeExtensions.StartWithClassicDesktopLifetime(AppBuilder builder, String[] args, Action`1 lifetimeBuilder)
   at MyApp.Program.Main(String[] args) in /Users/self/Documents/workspace/app/App.Desktop/Program.cs:line 31

To Reproduce

Define in Application.Styles an OnPlatform element that doesn't match the current platform

Expected behavior

To do nothing when there's no match, or if this usage isn't allowed, to output a meaningful error that explains the mistake. I can add a default match with an unused style in it as a workaround.

Avalonia version

11.2.0

OS

Windows, macOS

Additional context

No response

@halfninja halfninja added the bug label Nov 19, 2024
@maxkatz6
Copy link
Member

To do nothing when there's no match, or if this usage isn't allowed, to output a meaningful error that explains the mistake.

OnPlatform follows ternary operator logic. It always needs to return a value. In this case it's a null .
It's still expected to return null as a default in some contexts.
We should at least generate a diagnostic error, which can be optionally disabled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants