Xamarin.forms Xamarin形成双向绑定,初始加载后不触发
我有一个问题,花了我好几个小时的时间。我确信我遗漏了一些明显的东西。我已经复制了一个简化的形式,只使用一个按钮和一个标签的问题。标签正确设置为初始值。单击按钮后,我尝试更改标签文本。从大卫到特里 命令按钮启动,setter被调用,onPropertyChange被调用。有趣的是,在初始调试之后,get'er不会再次启动。(选中所有明显的内容,属性为公共属性和属性,名称正确,双向指定)Xamarin.forms Xamarin形成双向绑定,初始加载后不触发,xamarin.forms,binding,viewmodel,Xamarin.forms,Binding,Viewmodel,我有一个问题,花了我好几个小时的时间。我确信我遗漏了一些明显的东西。我已经复制了一个简化的形式,只使用一个按钮和一个标签的问题。标签正确设置为初始值。单击按钮后,我尝试更改标签文本。从大卫到特里 命令按钮启动,setter被调用,onPropertyChange被调用。有趣的是,在初始调试之后,get'er不会再次启动。(选中所有明显的内容,属性为公共属性和属性,名称正确,双向指定) /---查看模型代码---// 公共类TestBindingVM:INotifyPropertyChanged
/---查看模型代码---//
公共类TestBindingVM:INotifyPropertyChanged
{
私有字符串profileName;
公共ICommand ChangeTextCommand{get;}
公共TestBindingVM()
{
ProfileName=“David”;
ChangeTextCommand=新命令(UpdateTextCommandAction);
}
public void UpdateTextCommandAction()
{
ProfileName=“特里”;
}
公共字符串配置文件名
{
get=>profileName;
设置
{
profileName=值;
OnPropertyChanged(“ProfileName”);
}
}
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void OnPropertyChanged(字符串propertyName)
{
var propertyChangedCallback=PropertyChanged;
propertyChangedCallback?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
}
}
// ---------------------
//完整的XAML布局:
//----------------------
///----------------------------------------------//
//页面代码,创建bindingcontext//
///----------------------------------------------//
[XamlCompilation(XamlCompilationOptions.Compile)]
公共部分类TestBindingPage:ContentPage
{
public ViewModels.TestBindingVM vm=新的ViewModels.TestBindingVM();
公共TestBindingPage()
{
this.BindingContext=vm;
初始化组件();
}
}
这类似于这篇文章,它没有得到一个完整的答案。可能是由于缺少代码:
很奇怪,加载时会显示值“David”。这表明绑定已接近工作状态
感谢您的帮助。我只是想记录在案,我发现了这个问题。出于某种原因,INPC有一个本地的空接口。我猜在使用重构函数时,我单击了ImplementInterface,而不是添加“using”。因此,当INPC没有做它明确应该做的事情时,检查您使用的是实际的INPC而不是一些存根:-)只是为了记录,我发现了问题。出于某种原因,INPC有一个本地的空接口。我猜在使用重构函数时,我单击了ImplementInterface,而不是添加“using”。因此,当INPC没有做它明确应该做的事情时,请检查您使用的是实际的INPC而不是一些存根:-)为什么要使用标签的双向绑定?只有当控件允许用户与之交互(如条目或选择器)时,双向才有意义。标签不是交互式的。它被设置为双向,以便代码可以根据需要更改文本。还尝试了使用和不使用twoWay:-(数据-->UI是单向的。您不需要指定它,Label控件将默认为该绑定。只需尝试
Text=“{binding ProfileName}”
谢谢。当我看到你的评论时,我再试了一次。完全相同的行为。当它被发现时,这将是一件非常愚蠢的事情。不知道。我已经在表单项目中使用了数千次绑定,没有任何问题。你可能想再次检查你的INPC实现与一个Microsoft文档,但乍一看,它似乎正确me.为什么要对标签使用双向绑定?只有当控件允许用户与标签进行交互(如条目或选取器)时,双向绑定才有意义。标签不是交互式的。它被设置为双向,以便后面的代码可以根据需要更改文本。还尝试了使用和不使用双向:-(数据-->UI是一种方式。您不需要指定它,Label控件将默认为该绑定。只需尝试Text=“{binding ProfileName}”
谢谢。当我看到你的评论时,我再试了一次。完全相同的行为。当它被发现时,这将是一件非常愚蠢的事情。不知道。我已经在表单项目中使用了数千次绑定,没有任何问题。你可能想再次检查你的INPC实现与一个Microsoft文档,但乍一看,它似乎正确我
//--- View Model code ---//
public class TestBindingVM : INotifyPropertyChanged
{
private string profileName;
public ICommand ChangeTextCommand { get; }
public TestBindingVM()
{
ProfileName = "David";
ChangeTextCommand = new Command(UpdateTextCommandAction);
}
public void UpdateTextCommandAction()
{
ProfileName = "Terry";
}
public string ProfileName
{
get => profileName;
set
{
profileName = value;
OnPropertyChanged("ProfileName");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
var propertyChangedCallback = PropertyChanged;
propertyChangedCallback?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
// ---------------------
// Complete XAML Layout :
//----------------------
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TrackManager.Views.TestBindingPage">
<ContentPage.Content>
<StackLayout>
<Label Text="{Binding Path=ProfileName}"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
<Button Margin="0,10,0,0" Text="Change Text"
Command="{Binding ChangeTextCommand}"
TextColor="White"
/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
///----------------------------------------------//
// The page code, creating the bindingcontext //
///----------------------------------------------//
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class TestBindingPage : ContentPage
{
public ViewModels.TestBindingVM vm = new ViewModels.TestBindingVM();
public TestBindingPage ()
{
this.BindingContext = vm;
InitializeComponent();
}
}