Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

How do I correctly bind the page Title to a property using MVVM

$
0
0

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>


Viewing all articles
Browse latest Browse all 204402

Trending Articles