Cześć
Uczę się C# i Xamarina i mam problem z odpaleniem aplikacji. Ma ona za zadanie po naciśnięciu przycisku pokazać napis albo go ukryć. Chce użyć MVVM i tu właśnie zaczął się problem. Proszę o napisanie mi co powinienem poprawić i gdzie leży błąd
Model
using System;
namespace TestApp
{
public class MessageModel
{
public string Message { get; set; }
}
}
ViewModel
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace TestApp.ViewModels
{
public class BaseNotifyPropertyChanged : INotifyPropertyChanged
{
#region Fields
public event PropertyChangedEventHandler PropertyChanged;
#endregion
#region Methods
/// <summary>
/// OnPropertyChanged
/// </summary>
/// <param name="propertyName">thanks to [CallerMemberName] we can call only OnPropertyChanged from object which we want te reget</param>
protected void OnPropertyChanged([CallerMemberName]string propertyName = "")
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
}
using System.Linq;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace TestApp.ViewModels
{
/// <summary>
/// BaseViewModel
/// </summary>
public abstract class BaseViewModel : BaseNotifyPropertyChanged
{
#region Events
public delegate void NavigateFromDetailPageHandler(ContentPage page);
public event NavigateFromDetailPageHandler NavigateFromDetailPageEvent;
private void OnNavigateFromDetailPage(ContentPage page)
{
if (NavigateFromDetailPageEvent != null) NavigateFromDetailPageEvent(page);
}
public delegate void ResetDetailPageHandler();
public event ResetDetailPageHandler ResetDetailPageEvent;
private void OnResetDetailPage()
{
if (ResetDetailPageEvent != null) ResetDetailPageEvent();
}
#endregion
#region Fields
private INavigation _navigation;
#endregion
#region Methods
protected virtual void OnNavigatedFrom() { }
/// <summary>
/// BaseViewModel
/// </summary>
/// <param name="navigation"></param>
/// <param name="currentPage"></param>
public BaseViewModel(INavigation navigation)
{
_navigation = navigation;
}
public async Task GoBack()
{
if (_navigation.NavigationStack.Count > 0)
{
await _navigation.PopAsync();
}
}
public abstract void RemoveHandlers();
#endregion
}
}
using System;
using System.Windows.Input;
using TestApp.Commons;
using Xamarin.Forms;
namespace TestApp.ViewModels
{
public sealed class MessageViewModel : BaseViewModel
{
public ICommand ClickCommand { get; set; }
public MessageModel MessageModel { get; set; }
private bool _isSelect;
public bool IsSelect
{
get
{
return _isSelect;
}
set
{
_isSelect = value;
OnPropertyChanged();
}
}
private void InitializeCommands()
{
ClickCommand = new DelegateCommand(ClickCommandExecute);
}
void ClickCommandExecute()
{
if (IsSelect == true)
{
IsSelect = false;
}
else IsSelect = true;
}
public MessageViewModel(INavigation navigation) : base(navigation)
{
InitializeCommands();
}
internal void InitializeHandlers()
{
IsSelect = true;
MessageModel = new MessageModel
{
Message = "Hello World",
};
}
public override void RemoveHandlers()
{
throw new NotImplementedException();
}
}
}
View
using TestApp.ViewModels;
using Xamarin.Forms;
namespace TestApp.Views
{
public class ExtendedContentPage : ContentPage
{
protected override void OnDisappearing()
{
if (BindingContext != null)
{
(BindingContext as BaseViewModel).RemoveHandlers();
}
base.OnDisappearing();
}
}
}
using System;
using TestApp.ViewModels;
namespace TestApp.Views
{
public partial class MainPageView : ExtendedContentPage
{
public MainPageView()
{
InitializeComponent();
BindingContext = new MessageViewModel(Navigation);
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<d:ExtendedContentPage xmlns:d="clr-namespace:TestApp.Views;assembly=TestApp"
xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TestApp.MainPageView"
BackgroundColor="Blue" >
<Contend.Page>
<StackLayout VerticalOptions="Center" >
<Label Text="{Binding MessageModel.Message}" HorizontalOptions="Center" IsVisible="{Binding IsSelect}" />
<Button Text="Click Me" Command="{Binding ClickCommand}" />
</StackLayout>
</Contend.Page>
</d:ExtendedContentPage>