maui icon indicating copy to clipboard operation
maui copied to clipboard

[Windows] [RC2] Conditional ToolbarItem Text Display Using OnIdiom Not Working in Release Mode

Open malsabi opened this issue 1 year ago • 11 comments

Description

When I use Text="{ext:Translate ProfileToolBarText}" without OnIdiom, it works correctly in both Release and Debug modes. However, I intend to display the text on Desktop only, but this setup only works in Debug mode. In Release mode, it incorrectly displays Microsoft.Maui.Controls.Binding

Behavior in Release Mode (incorrect) Issue1

Behavior in Debug Mode (expected): Issue2

No exceptions are evident in the output that could indicate the root cause. The issue seems to arise when using OnIdiom.

Steps to Reproduce

  1. Clone the Repository.
  2. Run the application in Release Mode.
  3. You will notice the text is displaying Microsoft.Maui.Controls.Binding.

Link to public reproduction project repository

https://github.com/malsabi/ToolBarItemIssue

Version with bug

8.0.0-rc.2.9511

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Windows

Affected platform versions

Windows10.0.19041.0

Did you find any workaround?

No I didn't find any workaround.

Relevant log output

No response

malsabi avatar Nov 12 '23 21:11 malsabi

@StephaneDelcroix Thoughts?

jsuarezruiz avatar Nov 13 '23 15:11 jsuarezruiz

one path is using SetValue, the other is using SetBinding... I would not have considered this as a bug and only a possible enhancement, but the mismatch between debug and release require a solution

StephaneDelcroix avatar Nov 14 '23 08:11 StephaneDelcroix

one path is using SetValue, the other is using SetBinding... I would not have considered this as a bug and only a possible enhancement, but the mismatch between debug and release require a solution

Is there any workaround that we can use at the moment ?

malsabi avatar Nov 14 '23 10:11 malsabi

@StephaneDelcroix If there's any workaround for this issue please let me know, we are deploying our app on December.

malsabi avatar Nov 19 '23 07:11 malsabi

@malsabi i have it working like this in case you don't need anything from the binding public object ProvideValue(IServiceProvider serviceProvider) { return Translator.Instance[Key]; }

Just return the string value and it will work in both debug/release

cgomezm avatar Nov 30 '23 23:11 cgomezm

@cgomezm Hello thanks for replying. I tried the approach but it didn't work on release mode it kept on crashing, here is how I tried it: Maybe you could create a PR to the same repo I provided earlier.

namespace ToolBarItemIssue.Common.Extensions
{
   [ContentProperty(nameof(Name))]
   public class TranslateExtension : IMarkupExtension<BindingBase>
   {
       public string? Name { get; set; }

       public BindingBase ProvideValue(IServiceProvider serviceProvider)
       {
           throw new NotImplementedException();
       }

       object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider)
       {
           if (string.IsNullOrEmpty(Name))
               return "N/A";
           return LocalizationResourceManager.Instance[Name];
       }
   }
}

malsabi avatar Dec 02 '23 00:12 malsabi

@malsabi I moved away from returning the binding and just return the string value.

[ContentProperty(nameof(Key))]
internal class TranslateExtension : IMarkupExtension
{
    public string Key { get; set; }

    public object ProvideValue(IServiceProvider serviceProvider)
    {
        return Translator.Instance[Key];
    }
} 

cgomezm avatar Dec 04 '23 01:12 cgomezm

@cgomezm I can confirm your code works on Release / Debug mode however when I try to change the language in runtime it does not work. I tried using the following snippet:

if (AppResources.Culture.TwoLetterISOLanguageName.Equals("en"))
{
    LocalizationResourceManager.Instance.SetCulture(new CultureInfo("ar"));
}
else
{
    LocalizationResourceManager.Instance.SetCulture(new CultureInfo("en"));
}

malsabi avatar Dec 04 '23 01:12 malsabi

@malsabi yeah, that could be a downside as is the value being returned instead of the binding. Maybe dotnet team has made some progress on their investigation of this.

cgomezm avatar Dec 05 '23 01:12 cgomezm

@jsuarezruiz any thoughts or progress about this issue ?

malsabi avatar Dec 05 '23 01:12 malsabi

Can repro this issue at Windows platform on the latest 17.10.0 Preview 3(8.0.7&8.0.20).

RoiChen001 avatar Apr 12 '24 08:04 RoiChen001

We can not fix this, in either way: it's not possible (using a reasonable effort) to make it succeed (in all cases) when XamlC is on, and it's not possible to have it fail when XamlC is off

one way would be to deprecate OnIdiom in favour of OnIdiom<TReturnType> (oh, well, it used to be that way)

StephaneDelcroix avatar Jun 25 '24 12:06 StephaneDelcroix

found a solution, finally

StephaneDelcroix avatar Jun 26 '24 12:06 StephaneDelcroix

found a solution, finally

Can you tell us what was the solution ? Is there any changes we need to know about ?

malsabi avatar Jun 26 '24 17:06 malsabi