本文介绍了如何获得BindableProperty的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获得BindableProperty的值,因为我希望让BindableProperty的两个状态能够区分
I want get the value of a BindableProperty, because i want that have my BindableProperty two states for i can differentiate
<local:MyEntry event="{Binding Flag, Mode=TwoWay}">
并在我的自定义条目中:
and in my custom entry:
public static BindableProperty EventProperty =
BindableProperty.Create("event", typeof(string), typeof(MyEntry), "sigle", propertyChanged: (obj, oldValue, newValue) =>
{ });
private string flag;
public string Flag
{
get { return flag; }
set
{
if (flag != value)
{
flag = value;
NotifyPropertyChanged("event");
}
}
}
在我的viewModel中,这是代码:
and in my viewModel, this is the code:
private string flag;
public string Flag
{
get { return flag; }
set
{
if (flag != value)
{
flag = value;
RaisePropertyChanged(() => Flag);
}
}
}
private async Task Execute()
{
Flag = "Change";
}
在我的自定义渲染器中
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
Console.WriteLine("HERE "+ (entry as MyEntry).Flag);
//here i want to get the value
在答案的帮助下
if (e.PropertyName == MyEntry.EventProperty.PropertyName)
{
var entry = Element as MyEntry;
var Flag = entry.Flag;
Console.WriteLine("... "+ Flag);
推荐答案
在自定义渲染器中
public static BindableProperty FlagProperty = BindableProperty.Create("Flag", typeof(string), typeof(MyEntry), string.Empty, propertyChanged: (obj, oldValue, newValue) =>
{
var entry = obj as MyEntry;
entry.Flag = newValue.ToString();
});
private string flag;
public string Flag
{
get { return flag; }
set
{
if (flag != value)
{
flag = value;
NotifyPropertyChanged("Flag");
}
}
}
在自定义渲染器中
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == MyEntry.EventProperty.PropertyName)
{
// will been invoked when the value changed
var entry = Element as MyEntry;
var Flag = entry.Flag;
}
}
这篇关于如何获得BindableProperty的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!