Hi,
I'm learning how Xamarin works, and need help to understand how to make the binding on XAML to an object that is a property of my ContentPage:
XAML
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Campo_Minado.ConfigPage"> <Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Padding="20"> <Label Text="Iniciar com um bloco em branco aberto?" Grid.Row="0" Grid.Column="0" VerticalOptions="Center"/> <Switch IsToggled="{Binding conf.abrirBlocoBranco, Mode=TwoWay}" Grid.Row="0" Grid.Column="1" VerticalOptions="Center"/> <Label Text="Idioma" Grid.Row="1" Grid.Column="0" VerticalOptions="Center"/> <Label Text="Português" Grid.Row="1" Grid.Column="1" VerticalOptions="Center"/> </Grid> </ContentPage>
C# ContentPage
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Xamarin.Forms; using Xamarin.Forms.Xaml; namespace Campo_Minado { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class ConfigPage : ContentPage { protected Config conf { get; set; } = Config.obj; public ConfigPage () { InitializeComponent (); } protected override void OnDisappearing() { Config.obj.salvar(); } } }
C# Object
using System; using System.Collections.Generic; using System.Text; using Xamarin.Forms; namespace Campo_Minado { public class Config { public static Config obj = new Config(); public Config() { carregar(); } //----------------------------------------------------- /* * Propriedades de vibração e som: * 1 - vibração (0x0001) * 2 - som (0x0010) * 3 - vibração e som (0x0011) */ public byte tapHold { set; get; } = 1; public bool abrirBlocoBranco { set; get; } = true; public String idioma { set; get; } = "por"; public void carregar() { IDictionary<String,Object> props = Application.Current.Properties; if (props.ContainsKey("tapHold")) tapHold = (byte)props["tapHold"]; if (props.ContainsKey("abrirBlocoBranco")) abrirBlocoBranco = (Boolean)props["abrirBlocoBranco"]; if (props.ContainsKey("idioma")) idioma = (props["idioma"] as string)?? "por"; } public void salvar() { IDictionary<String, Object> props = Application.Current.Properties; props["tapHold"] = tapHold; props["abrirBlocoBranco"] = abrirBlocoBranco; props["idioma"] = idioma; Console.WriteLine("Gravando informações: abrirBlocoBranco = {0}", abrirBlocoBranco); Application.Current.SavePropertiesAsync(); } } }
What i'm trying to do: use the app and have the "conf.abrirBlocoBranco" set to the value of the switch.
I also try:
<Switch BindingContext="{x:Reference conf}" IsToggled="{Binding abrirBlocoBranco, Mode=TwoWay}" Grid.Row="0" Grid.Column="1" VerticalOptions="Center"/>
but with this i receive the error: "Can't resolve name on Element".
The log (on "salvar... Console.WriteLine(...)") is always "true" for the value (the default that i set lines above).
Thanks for your attention guys!
Answers
This might help
https://redpillxamarin.com/2017/01/28/206-reusable-controls/
Hi @rcheruti
One option is to give your ContentPage a x:Name and then refer to its properties in Binding statements:
Please see here.
Hope this helps,
Tim
Thanks guys for your help!
@Hunuman , this is exatly (part) of what i need!
i have added on XAML:
Also is important to note that all that will only work if the property on C# is public, i declared it protected first time and it was not working until i changed it.
@ClintStLaurent , thanks for the article, there is too a case where was used the same strategy (the "x:Name"). I will study more about that custom tags/components.
I mark @Hunuman because the answer was closest to the solution, more direct to the solution.
Thanks guys!