I have the following code, but I still have no value displaying for my Title value. Where am I going wrong?
ObservableProperty Helper Class
public class ObservableProperty : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Base ViewModel
public abstract class BaseViewModel : ObservableProperty
{
protected readonly IPageService _pageService;
public ICommand NavigateCommand { get; private set; }
public ICommand NavigateModalCommand { get; private set; }
public BaseViewModel (IPageService pageService)
{
_pageService = pageService;
NavigateCommand = new Command<Page>(async pg => await Navigate(pg));
NavigateModalCommand = new Command<Page>(async pg => await NavigateModal(pg));
}
private async Task Navigate(Page page)
{
bool animated = true;
await _pageService.PushAsync(page, animated);
}
private async Task NavigateModal(Page page)
{
bool animated = true;
await _pageService.PushModalAsync(page, animated);
}
}
ViewModel
public class ManifestViewModel : BaseViewModel
{
private string _screenTitle;
public ManifestViewModel(PageService pageService, string screenTitle) : base(pageService)
{
ScreenTitle = screenTitle;
}
public string ScreenTitle
{
get { return _screenTitle; }
set
{
_screenTitle = value;
OnPropertyChanged("ScreenTitle");
}
}
}
View (Code behind)
public partial class ManifestPage : ContentPage
{
public ManifestPage(string screenTitle)
{
ViewModel = new ManifestViewModel(new PageService(), screenTitle);
_manifest = manifest;
InitializeComponent();
}
public ManifestViewModel ViewModel
{
get { return BindingContext as ManifestViewModel; }
set { BindingContext = value; }
}
protected override void OnAppearing()
{
base.OnAppearing();
}
}
View(Xaml)
<?xml version="1.0" encoding="utf-8" ?>
**
<ContentPage.Content>
...
</ContentPage.Content>
Does screenTitle
actually have a value? Have you tried setting a value to the Title
attribute directly to verify that it will really be displayed as expected?
Answers
missing xaml code:
<ContentPage.Content>
...
</ContentPage.Content>
Hmmm... keeps stripping out my xaml code, so I'll just put the line that I want to show:
Title="{Binding ScreenTitle, Mode=OneWay}"
Does
screenTitle
actually have a value? Have you tried setting a value to theTitle
attribute directly to verify that it will really be displayed as expected?The answer to your question was no. I made a mistake. I realized that I was trying to update a label not the page Title value. Anyway, once I binded to the correct object, it worked. Your question led me down the path of discovery. thanks!