看起来我无法理解 xamarin.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>
我无法弄清楚我错过了什么。任何帮助将不胜感激
最佳答案
您的 hello
属性(property)需要公开。
public class MainViewModel : INotifyPropertyChanged
{
private string _hello;
public string hello
{
get { return _hello; }
set
{
_hello = value;
OnPropertyChanged();
}
}
...
}
关于c# - 简单的 Mvvm 数据绑定(bind) - xamarin 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46094335/