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 thrown when accessing resource values with OnPlatform from Resource Dictionary #16494

Closed
sbxamdev opened this issue Aug 2, 2023 · 6 comments
Assignees
Labels
area-xaml XAML, CSS, Triggers, Behaviors platform/android 🤖 s/needs-attention Issue has more information and needs another look t/bug Something isn't working

Comments

@sbxamdev
Copy link

sbxamdev commented Aug 2, 2023

Description

Hi guys,

From MAUI's default app project, I just added a Resource item having double as the type and have separate values per platform.
Screenshot:
image

I dont have any issues in using it from xaml, however when I access it from c#, an error is thrown
Screenshot:
image

Additional Note: Trying the above c# code for resource that has single value for both platform works fine (Not OnPlatform syntax).

Please can anyone help? Thanks in advance, will greatly appreciate it. :)

Steps to Reproduce

No response

Link to public reproduction project repository

No response

Version with bug

7.0.49

Last version that worked well

6.0

Affected platforms

Android, I was not able test on other platforms

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

No response

@sbxamdev sbxamdev added the t/bug Something isn't working label Aug 2, 2023
@Eilon Eilon added the area-xaml XAML, CSS, Triggers, Behaviors label Aug 2, 2023
@rachelkang rachelkang marked this as a duplicate of #16327 Aug 2, 2023
@mattleibow mattleibow marked this as not a duplicate of #16327 Aug 2, 2023
@dotnet dotnet deleted a comment from rachelkang Aug 2, 2023
@mattleibow mattleibow reopened this Aug 2, 2023
@mattleibow
Copy link
Member

What is the value of value in the debugger?

@mattleibow mattleibow added the s/needs-info Issue needs more info from the author label Aug 2, 2023
@ghost
Copy link

ghost commented Aug 2, 2023

Hi @sbxamdev. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@sbxamdev
Copy link
Author

sbxamdev commented Aug 3, 2023

What is the value of value in the debugger?

Hi, Its an OnPlatform<double>.
image

BTW It is working in my existing Xamarin.Forms App.
Also, someone in stackoverflow gave me a work-around which can be found here: https://sirjohnk.com/coding/MAUI-Snack-2/

@ghost ghost added s/needs-attention Issue has more information and needs another look and removed s/needs-info Issue needs more info from the author labels Aug 3, 2023
@StephaneDelcroix StephaneDelcroix self-assigned this Sep 21, 2023
@StephaneDelcroix
Copy link
Contributor

but but but... OnPlatform declare an implicit cast operator to T, so it should work

@StephaneDelcroix
Copy link
Contributor

I can reproduce this

@StephaneDelcroix
Copy link
Contributor

StephaneDelcroix commented Sep 21, 2023

Ok... there's no bug here, but you need to hint the C# compiler. From the compiler point of view, value is of type object, so it can not generate code that invokes the implicit cast operator defined in OnPlatform.

you can do it by changing your code to one of this 3 options

  1. page.Resources.TryGetValue("RegularFontSize", out double value)
  2. dblValue = (double)(value as OnPlatform<double>);
  3. dblValue = (double)(OnPlatform<double>)value;

@ghost ghost locked as resolved and limited conversation to collaborators Oct 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-xaml XAML, CSS, Triggers, Behaviors platform/android 🤖 s/needs-attention Issue has more information and needs another look t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants