Using a Binding within OnPlatform

JorgeRochaJorgeRocha USMember
edited August 2017 in Xamarin.Forms

In short, is it possible to use a Binding within OnPlatform?

Hi, good morning, I have the following query:

In a ViewModel I have the following properties for a Menu:

Then in the XAML code I need to set a different Icon depending on the platform.

But this does not work, honestly does not specify an Error, only that the object is not referenced.

For this I have created a custom font where the icons are, in Android is already in the specific folder (Assets), also in iOS (Resources).

The device loads the source correctly when they are static, but you would like to do it dynamically, that is, with a Binding.

In XAML this is the common way in which it works:

Also if I use it this way if it works:

As to be static and establish a static property, all this comes from a class where the respective string / identifier of the icon.
This class is located inside the project in a folder called "Class"

Best Answer

  • ClintStLaurentClintStLaurent US ✭✭✭✭✭
    Accepted Answer

    I need to set a different Icon depending on the platform.

    But you don't have to use different names. You can specify "FontIcon" at the PCL layer, and have a green one in Android and red one in iOS - or whatever the difference is - but they would use the same name.

    But stop trying to use unique variables in the PCL - as soon as you do that you're making life hard. If you just have "FontIcon" instead of "AndroidFontIcon" and "iOSFontIcon" things get simpler. Your code just has one thing: FontIcon - and each platform project has its own version of that resource.

    Also - try to use better names. It isn't a font - its a piece of text you're using. Don't call it a font and don't name the class 'Font'. Font is already a key word. All you're going to do is confuse yourself and maybe even confuse your code.

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    Accepted Answer

    I need to set a different Icon depending on the platform.

    But you don't have to use different names. You can specify "FontIcon" at the PCL layer, and have a green one in Android and red one in iOS - or whatever the difference is - but they would use the same name.

    But stop trying to use unique variables in the PCL - as soon as you do that you're making life hard. If you just have "FontIcon" instead of "AndroidFontIcon" and "iOSFontIcon" things get simpler. Your code just has one thing: FontIcon - and each platform project has its own version of that resource.

    Also - try to use better names. It isn't a font - its a piece of text you're using. Don't call it a font and don't name the class 'Font'. Font is already a key word. All you're going to do is confuse yourself and maybe even confuse your code.

  • JorgeRochaJorgeRocha USMember

    Actually I'm not using a reference like "Font", it's just an example, but its answer has served me, and maybe it's not a very feasible code but I can hardly make myself call Jr. programmer and my experience is very short, so I will solve it as I can understand your answer:

    public void LoadMenu()
    {
        switch (Device.RuntimePlatform)
        {
            case Device.iOS:
                Plataforma = "iOS";
            break;
            case Device.Android:
                Plataforma = "Android";
            break;
        }
    
        Menu = new ObservableCollection<MenuItemModel>();
    
        Menu.Add(new MenuItemModel()
        {
            IconFont = Plataforma == "Android" ? "\ue91b" : "\ue94e",
            Title = "Inicio",
            PageName = "MasterHome",
            Visible = true,
            NavigationBar = false,
            IsOpen = false
        });
    }
    

    Likewise if that was not what I wanted to say, it opened my head and way of thinking. Thank you very much, friend, believe it or not, I had time to solve this problem.

    I will also follow your advice and look for ways to simplify and optimize my code. Thank you.

Sign In or Register to comment.