Xamarin形式(MVVM)中的绑定问题 [英] Problems with Bindings in Xamarin Forms (MVVM)

查看:58
本文介绍了Xamarin形式(MVVM)中的绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用mvvm理解xaml时遇到了一些问题.有时可以,但是其他人则不能.

I am having some problems understanding xaml with mvvm. Sometimes works but others doesn't.

ViewModel(实现INotifyPropertyChanged):

ViewModel (implements INotifyPropertyChanged):

private Class1 firstClass;
public Class1 FirstClass{
    get{return firstClass;}
    set{firstClass = value; OnPropertyChanged();}
}

private string name;
public string Name{
    get{return name;}
    set{name = value; OnPropertyChanged();}
}

private string address;
public string Address{
    get{return address;}
    set{address = value; OnPropertyChanged();}
}

查看:

private ViewModel vm;

在View构造函数中:

In the View constructor:

vm = new ViewModel(id);
BindingContext = vm;

OnAppearing(异步):

OnAppearing (async):

base.OnAppearing();
await vm.LoadDataAsync();
lAddress.SetBinding(Label.TextProperty, new Binding("Address");

  • 如果我在xaml中设置BindingContext并将其从构造函数,它不起作用.

    • If I set the BindingContext in xaml and remove it from the constructor, it does not work.

      如果我在xaml中的地址"标签上设置绑定"并将其从后面的代码中删除,则它将无法正常工作.

      If I set the Binding on the Address label in xaml and remove it from the code behind, it does not work.

      如果我尝试使用名称"作为页面标题,则无法使用.

      If I try to use Name as the Title of the page, it does not work.

      在上述任何一种情况下,我都不会收到类似"Binding:在BindingContext上找不到属性"之类的错误,因此我知道它们已被发现,但也许是空的.

      In any of these cases I am not getting any error like 'Binding: property not found on BindingContext' so I understand that they are being found but maybe they are empty.

      如果我从Class1修改属性,则该属性不会显示在页面上.我可以假设原因是Class没有实现INotifyPropertyChanged?

      If I modify a property from Class1, it does not appear on the page. Can I assume that the reason is that Class does not implement INotifyPropertyChanged?

      在VM构造函数(Task.Run)或Page.OnAppearing(await vm.LoadData())上加载LoadData是更好还是明智的选择?

      Is it better or advisable to LoadData in VM constructor (Task.Run) or on Page.OnAppearing(await vm.LoadData())?

      有人可以提供一些指导吗?

      Could anybody provide a bit of guidance?

      XAML代码(抱歉,无法添加代码本身)

      推荐答案

      这将进行相当多的调试(以及每个帖子一个问题可能更有意义的原因);但这里去了:

      This is getting to a fair bit of debugging (and a reason why one question per post might make more sense); but here goes:

      1. 您的视图模型具有一个带参数的构造函数.如果在XAML中构造,它将使用默认值.基本上,您正在比较的两段代码并不相同.如果需要该参数,请保持该参数不变.

      1. Your view model has a constructor that takes a parameter. If you construct in XAML it will use the default. Basically, the two pieces of code you are comparing are not equivalent. If you need the parameter keep it like you have it.

      您将需要调试这一个;确保对于初学者来说,绑定属性实际上不是空的.

      You'll need to debug this one; ensure the bound property isn't actually empty for starters.

      不知道;与您可能需要调试的地址相同.

      No idea; same as the address you probably need to debug it.

      您对此的解释是正确的;我会检查我的数据是否正确加载.

      Your interpretation of this is correct; I would be checking that my data is loaded properly.

      是的.仅当重新分配 FirstClass 时,您当前的代码才会更新.要在该对象中捕获属性的分配, Class1 将需要INPC(也许可以解释2和/或3)

      Yes. Your current code will only update if FirstClass is reassigned. To catch assignment of properties within that object Class1 will need INPC (perhaps this explains 2 and/or 3)

      不确定这真的很重要.

      这篇关于Xamarin形式(MVVM)中的绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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