在 Xamarin.Forms 上的 XAML 中将 BindingContext 设置为 ViewModel [英] Set BindingContext to ViewModel in XAML on Xamarin.Forms

查看:32
本文介绍了在 Xamarin.Forms 上的 XAML 中将 BindingContext 设置为 ViewModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 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 接口)定义了一个抽象类,并在创建了一个使用简单属性扩展 BaseViewModel 类的 SummaryViewModel 类之后:

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 放在一个名为ViewModels"的文件夹中案例将是 XamarinPOC.ViewModels

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆