Xamarin.Forms DataBinding不起作用

时间:2018-07-01 04:30:07

标签: xamarin data-binding xamarin.forms

我知道这个问题已经问过很多次了,但是每种情况都有点“独特”,让我们这样说。

我的环境是Visual Studio 15.7.4,使用所有Nuget包(首先是Xamarin.Forms)更新到最新版本。

问题是我创建了一个简单的绑定测试应用程序,并且绑定不再起作用! 我已经用Xamarin.Forms创建了一个Android应用程序,前段时间一切正常。 现在,所有绑定的工作方式都类似于“ OneTime”,仅在开始时设置一次,而不会在UI中进行更新(但仍会更新主ViewModel中的数据)。

我想知道这是一个普遍的Xamarin问题,还是仅仅是我做错了什么。 我已经尽力了,但是什么都没有改变。

这是完整的代码(要测试,请创建一个Xamarin.Forms应用程序,其中“共享项目”作为选项;我通常将所有逻辑应用程序代码放在项目的共享部分中):

App.cs

namespace App1
{
   public partial class App : Application
   {
    public static AppViewModel ViewModel { get; set; }

    public App ()
    {
        InitializeComponent();

        ViewModel = new AppViewModel();
        MainPage = new MainPage();
    }

    // Other App lifecycle methods
  }
}

AppViewModel.cs

namespace App1.ViewModels
{
  public class AppViewModel : BaseBind
  {
    public AppViewModel()
    {
        TestoProva = "---";
        BoolProva = false;
    }

    private string testoProva;
    public String TestoProva
    {
        get { return testoProva; }
        set
        {
            SetProperty(ref testoProva, value);
        }
    }

    private bool boolProva;
    public Boolean BoolProva
    {
        get { return boolProva; }
        set { SetProperty(ref boolProva, value); }
    }
  }
}

主页

<?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:App1"
         xmlns:vm="clr-namespace:App1.ViewModels"
         x:Class="App1.MainPage">
  <ContentPage.BindingContext>
    <vm:AppViewModel/>
  </ContentPage.BindingContext>
  <StackLayout>
    <Label Text="{Binding TestoProva, Mode=OneWay}"/>
    <Button Text="Test" Clicked="Test"/>
  </StackLayout>
</ContentPage>

MainPage.cs

namespace App1
{
public partial class MainPage : ContentPage
{
    public static AppViewModel ViewModel => App.ViewModel;

    public MainPage()
    {
        InitializeComponent();
        BindingContext = ViewModel;
    }

    public void Test()
    {
        ViewModel.TestoProva = "TESTO CAMBIATO!!!";
    }
  }
}

BaseBind.cs

 namespace App1.ViewModels
 {
  public class BaseBind
  {
    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged([CallerMemberName] string propertyName = null) =>
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

    protected bool SetProperty<T>(ref T storage, T value,
        [CallerMemberName] String propertyName = null)
    {
        if (object.Equals(storage, value)) return false;
        storage = value;
        OnPropertyChanged(propertyName);
        return true;
    }
  }
}

就是这样……一个简单的应用程序测试,因为我正在工作的那个应用程序也显示了这个问题。

真的想知道是什么问题。.me或Xamarin框架。

感谢大家的关注和耐心。

3 个答案:

答案 0 :(得分:2)

我还没有使用您的代码创建/运行测试项目,但是看来问题出在您的BaseBind类上。添加必需的事件后,实际上并没有将其标记为实现INotifyPropertyChanged,这是绑定引擎为连接事件而需要的内容。

尝试一下:

namespace App1.ViewModels
 {
  public class BaseBind : System.ComponentModel.INotifyPropertyChanged
  {
    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged([CallerMemberName] string propertyName = null) =>
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

    protected bool SetProperty<T>(ref T storage, T value,
        [CallerMemberName] String propertyName = null)
    {
        if (object.Equals(storage, value)) return false;
        storage = value;
        OnPropertyChanged(propertyName);
        return true;
    }
  }
}

答案 1 :(得分:0)

不应在MainPage.cs中定义您的Clicked处理程序: public void Test(object sender, System.EventArgs e) { ... }吗?

答案 2 :(得分:0)

也许是由于您的OnPropertyChanged(propertyName);在运行时调用OnPropertyChanged(null);的原因。在调试器中检查它。

相关问题