在Xamarin.Forms上的XAML中将BindingContext设置为ViewModel [英] Set BindingContext to ViewModel in XAML on Xamarin.Forms
问题描述
我想用Xamarin.Form和MVVM开发一个简单的项目.在我的解决方案(名为XamarinPOC)中,除了标准的Xamarin.Forms项目之外,我还有一个单独的模型项目(XamarinPOC.Model)和一个单独的ViewModel项目(XamarinPOC.ViewModel).
I want to develop a simple project with Xamarin.Form and MVVM. In my solution (named XamarinPOC) i have (in addition to standard Xamarin.Forms projects) one separate project for the model (XamarinPOC.Model) and one separate project for the ViewModel (XamarinPOC.ViewModel).
我在XamarinPOC.ViewModel项目中为BaseViewModel类(实现了INotifyPropertyChanged接口)定义了一个抽象类,并在我创建了一个SummaryViewModel类之后,该类通过简单的属性扩展了BaseViewModel类:
I defined in a XamarinPOC.ViewModel project an abstract class for a BaseViewModel class (that implements the INotifyPropertyChanged Interface) and after I've created a SummaryViewModel class that extend BaseViewModel class with a simple property:
namespace XamarinPOC.ViewModel
{
public class SummaryViewModel : BaseViewModel
{
private string _test = "The binding is OK!";
public String test
{
get
{
return _test;
}
set
{
_test = value;
OnPropertyChanged("test");
}
}
public SummaryViewModel(){}
}
}
接下来,我在XamarinPOC项目中创建了一个简单的ContentPage(SummatyView),该项目仅包含我要显示ViewModel中定义的文本的标签.我想使用XAML定义视图和绑定,但是当我运行该应用程序时,什么都没有显示,我在编译时和运行时都没有错误,但是没有显示文本.我的XAML是这个
Next I created a simple ContentPage (SummatyView) in a XamarinPOC project that contain only a label that i want show the text defined in ViewModel. I want to use a XAML for defining the View and the binding but when I run the app nothing is displayed, I no errors on compile-time and runtime but the text are not displayed. My XAML is this
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:XamarinPOC.ViewModel,assembly=XamarinPOC.ViewModel"
x:Class="XamarinPOC.Summary"
Title="Summary List"
BindingContext="XamarinPOC.ViewModel.SummaryViewModel">
<StackLayout>
<Label Text="{Binding test}"/>
</StackLayout>
</ContentPage>
最后我的app.cs是:
and finally my app.cs is:
namespace XamarinPOC
{
public class App : Application
{
public App()
{
MainPage = new Summary();
}
}
}
在XamarinPOC项目中,我添加了对XamarinPOC.ViewModel和XamarinPOC.Model程序集的引用.
In the XamarinPOC project I've added a reference to XamarinPOC.ViewModel and XamarinPOC.Model assemblies.
我认为问题出在绑定的XAML定义中,但是我没有找到错误.我在哪里错了?
I think the problem is in the XAML definition of binding, but i don't find the error. Where am I wrong?
推荐答案
要根据您的情况将视图绑定到Xaml的视图模型,请执行以下操作
To bind the view to the viewmodel from Xaml in your case do it like this
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewModels="clr-namespace:XamarinPOC.ViewModel; assembly=XamarinPOC.ViewModel"
x:Class="XamarinPOC.Summary"
Title="Summary List">
<ContentPage.BindingContext>
<viewModels:SummaryViewModel/>
</ContentPage.BindingContext>
<StackLayout>
<Label Text="{Binding test}"/>
</StackLayout>
</ContentPage>
我注意到的一个注意点是命名约定,最好将所有ViewModel(即使只是一个viewModel)放在名为"ViewModel s "的文件夹中情况将是XamarinPOC.ViewModel s
One side note I noticed is with naming conventions, it is better to put all your ViewModels, even if it is only one viewModel, inside a folder named "ViewModels" So the namespace in your case would be XamarinPOC.ViewModels
这篇关于在Xamarin.Forms上的XAML中将BindingContext设置为ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!