看起来我无法理解mamarin.forms应用程序中的mvvm模式是如何工作的。我想制作简单的hello world应用程序以及我如何做到这一点。
视图模型
namespace HelloWorldApp.ViewModels
{
public class MainViewModel : INotifyPropertyChanged
{
private string _hello;
private string hello
{
get { return _hello; }
set
{
_hello = value;
OnPropertyChanged();
}
}
public MainViewModel() {
hello = "Hello world";
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
查看
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="SkanerDetali.Views.LoginPage"
xmlns:ViewModels="clr-namespace:HelloWorldApp.ViewModels;assembly=HelloWorldApp">
<ContentPage.BindingContext>
<ViewModels:MainViewModel />
</ContentPage.BindingContext>
<StackLayout>
<Label Text="{Binding hello}" />
</StackLayout>
</ContentPage>
我无法弄清楚我错过了什么......任何帮助都会非常感激
答案 0 :(得分:3)
您的hello
财产需要公开。
public class MainViewModel : INotifyPropertyChanged
{
private string _hello;
public string hello
{
get { return _hello; }
set
{
_hello = value;
OnPropertyChanged();
}
}
...
}