Hi Everyone, Help please!
This is an accessibility question, and I can't seem to figure out the best way to go about it.
I want to set a variable that is bound to an entity (label.text in this case) in my OnAppearing method. i.e. I want to set the label when the page is loaded.
I've got my xaml page:
<?xml version="1.0" encoding="utf-8" ?>
...
xmlns:local="clr-namespace:App13" x:Class="App13.MainPage"> <ContentPage.BindingContext> <local:myViewModel /> </ContentPage.BindingContext> <StackLayout> <Label Text="{Binding myBindingEntry}" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" /> <Button Text="Command Button" Command="{Binding btnCommand}" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" /> </StackLayout>
'
And I've created another file, called myViewModel.cs :
using System.ComponentModel;
using System.Windows.Input;
using Xamarin.Forms;
namespace App13
{
class myViewModel : INotifyPropertyChanged
{
string _myBindingEntry = "nothing!"; public event PropertyChangedEventHandler PropertyChanged; public ICommand btnCommand { private set; get; } public myViewModel() { btnCommand = new Command( execute: () => { myBindingEntry = "You have clicked the Command Button!"; RefreshCanExecutes(); } ); } void RefreshCanExecutes() { ((Command)btnCommand).ChangeCanExecute(); } public string myBindingEntry { private set { if (_myBindingEntry != value) { _myBindingEntry = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("myBindingEntry")); } } get { return _myBindingEntry; } } }
}
And here's my code behind, which is where I want to set myBindingEntry:
using System.ComponentModel;
using Xamarin.Forms;
namespace App13
{
[DesignTimeVisible(false)]
public partial class MainPage : ContentPage
{
// I've tried declaring an instance of myViewModel class, to use later:
myViewModel fmyViewModel = new myViewModel();
public MainPage()
{
InitializeComponent();
} protected override void OnAppearing() { // here is where I want to set the text of my Label. // i should be able to do it by assigning text to myBindingEntry // This generates 'The name 'myBindingEntry' does not exist in the current context. myBindingEntry = "this is set during OnAppearing"; // or if I use this, I get: // Error CS0272 The property or indexer 'myViewModel.myBindingEntry' cannot be used in this context because the set accessor is inaccessible App13 fmyViewModel.myBindingEntry = "using an instance of the view model!" base.OnAppearing(); } }
}