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 · 3 comments
Open

Error when OnPlatform doesn't match #17562

halfninja opened this issue Nov 19, 2024 · 3 comments

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.

@timunie
Copy link
Contributor

timunie commented Nov 20, 2024

<OnPlatform.Default> is there for it afaik.

@halfninja
Copy link
Author

halfninja commented Nov 20, 2024

Thanks both - I did add OnPlatform.Default to fix it as soon as I realised the error, and understood that it was essentially returning null. Could simply be a docs thing - which does have a default case in the example snippets, but perhaps explicitly stating that you likely need to match all platforms in XAML to avoid inserting a null style (though I understand it's not just for styles)

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

3 participants