Xamarin.forms 未调用标签TextProperty强制值委托
我使用Xamarin.forms 未调用标签TextProperty强制值委托,xamarin.forms,Xamarin.forms,我使用Reflection将Label.TextProperty.concurveValue设置为我的自定义委托(TextProperty.concurveValue默认为空) 但当标签文本更改时,不会调用委托 在Image.SourceProperty,Entry.TextProperty 一切都被称为成功 是bug还是Label.TextPropertyby design不会调用强制值委托 多谢各位 Xamarin.Forms 4.3.0.947036 var-property=Label.
Reflection
将Label.TextProperty.concurveValue
设置为我的自定义委托(TextProperty.concurveValue
默认为空)
但当标签文本更改时,不会调用委托
在Image.SourceProperty
,Entry.TextProperty
一切都被称为成功
是bug还是Label.TextProperty
by design不会调用强制值委托
多谢各位
Xamarin.Forms 4.3.0.947036
var-property=Label.TextProperty;
var-concurveValue=property.GetType().GetProperty(“concurveValue”,BindingFlags.NonPublic | BindingFlags.Instance);
oldDelegate=强制值?.GetValue(属性)作为BindableProperty.强制值委托;
强制值?.SetValue(属性,(可绑定,值)=>{
var modified=ModifyValue(value);//如果需要,只需修改值即可
返回修改
});
我在一个Xamarin.Forms项目中创建了一个新的页面Page1,在代码后面有以下简单代码:
public Page1()
{
InitializeComponent();
}
protected override void OnAppearing()
{
Label l = new Label();
BindableProperty.CoerceValueDelegate d = (s, a) =>
{
string modified = "textY"; // simply modify the value if required
return modified;
};
var property = Label.TextProperty;
var coerceValue = property.GetType().GetProperty("CoerceValue", BindingFlags.NonPublic | BindingFlags.Instance);
var oldDelegate = coerceValue?.GetValue(property) as BindableProperty.CoerceValueDelegate;
coerceValue?.SetValue(property, d);
l.Text = "1"; // Text property is set to textY thanks to CoerceValueDelegate!
base.OnAppearing();
}
当我调用l.Text=“1”
时,正确调用了我定义的BindableProperty.胁迫值委托,并且将l.Text
设置为textY
@codetale,你能自己运行这个代码吗?我在一个Xamarin.Forms项目中创建了一个新的页面Page1,在代码后面有以下简单代码:
public Page1()
{
InitializeComponent();
}
protected override void OnAppearing()
{
Label l = new Label();
BindableProperty.CoerceValueDelegate d = (s, a) =>
{
string modified = "textY"; // simply modify the value if required
return modified;
};
var property = Label.TextProperty;
var coerceValue = property.GetType().GetProperty("CoerceValue", BindingFlags.NonPublic | BindingFlags.Instance);
var oldDelegate = coerceValue?.GetValue(property) as BindableProperty.CoerceValueDelegate;
coerceValue?.SetValue(property, d);
l.Text = "1"; // Text property is set to textY thanks to CoerceValueDelegate!
base.OnAppearing();
}
当我调用l.Text=“1”
时,正确调用了我定义的BindableProperty.胁迫值委托,并且将l.Text
设置为textY
@codetale,您是否能够运行此代码?如果您想在Label.Text更改时调用强制值,我建议您可以使用Bindable属性绑定Label.textproperty
public partial class Page9 : ContentPage
{
public static readonly BindableProperty labelvalueProperty = BindableProperty.Create("labelvalue", typeof(string), typeof(Page9), null , coerceValue: CoerceValue);
public string labelvalue
{
get { return (string)GetValue(labelvalueProperty); }
set { SetValue(labelvalueProperty, value); }
}
private static object CoerceValue(BindableObject bindable, object value)
{
string str = (string)value;
if(str=="cherry")
{
str = "hello world!";
}
return str;
}
public Page9 ()
{
InitializeComponent ();
label1.SetBinding(Label.TextProperty, "labelvalue");
labelvalue = "this is test";
BindingContext = this;
}
private void Btn1_Clicked(object sender, EventArgs e)
{
labelvalue = "cherry";
}
}
您可以看到当Label.Text属性更改时可以触发强制值。如果您想在Label.Text更改时调用强制值,我建议您可以使用可绑定属性绑定Label.textproperty
public partial class Page9 : ContentPage
{
public static readonly BindableProperty labelvalueProperty = BindableProperty.Create("labelvalue", typeof(string), typeof(Page9), null , coerceValue: CoerceValue);
public string labelvalue
{
get { return (string)GetValue(labelvalueProperty); }
set { SetValue(labelvalueProperty, value); }
}
private static object CoerceValue(BindableObject bindable, object value)
{
string str = (string)value;
if(str=="cherry")
{
str = "hello world!";
}
return str;
}
public Page9 ()
{
InitializeComponent ();
label1.SetBinding(Label.TextProperty, "labelvalue");
labelvalue = "this is test";
BindingContext = this;
}
private void Btn1_Clicked(object sender, EventArgs e)
{
labelvalue = "cherry";
}
}
您可以看到,当Label.Text属性更改时,可以触发强制值。请向我们展示codehi Jason,代码非常简单,只需设置自定义委托以应用一些逻辑并返回新值。请向我们展示codehi Jason,而不是描述您在做什么,代码非常简单,只需设置自定义委托以应用一些逻辑并返回新值。Hi@Julipan,奇怪,我发现如果定义:*在
App.OnStart
,它将无法工作*在第一页上出现它的工作*在App.OnStart
上用BeginInvokenMainthread包装,它的working@codetale,我只是在App.OnStart的答案中复制粘贴了SetValue代码,它正常工作。设置标签的值后,将调用强制ValueDelegate。我用的是X.F.4.3。如果您在App.OnStart(不带BeginInvokeOnMainThread)中使用我的答案中的代码,是否未调用强制值委托?是的,我在App.OnStart中使用您的代码,但必须包装强制值?.SetValue(属性,d)在BeginInvokeOnMainThread中编写>以调用委托。不知道为什么…嗨@Julipan,我发现如果你移除Label l=new Label();l、 Text=“1”代码>从您的代码中,但在XAML上输入,并在设置代理后将MainPage
设置为此XAML。然后,除非wrapcompressevalue?.SetValue(属性,d),否则将不会调用compressevalueDelegate
奇怪的是,在BeginInvokenMainThreadHi@Julipan中,我发现如果定义:*在App.OnStart
中,它在第一页出现时不起作用
它的工作*在App.OnStart
中用BeginInvokenMainThread包装,它的working@codetale,我只是在App.OnStart的答案中复制粘贴了设置值代码,而且它工作正常。设置标签的值后,将调用强制ValueDelegate。我用的是X.F.4.3。如果您在App.OnStart(不带BeginInvokeOnMainThread)中使用我的答案中的代码,是否未调用强制值委托?是的,我在App.OnStart中使用您的代码,但必须包装强制值?.SetValue(属性,d)在BeginInvokeOnMainThread中编写>以调用委托。不知道为什么…嗨@Julipan,我发现如果你移除Label l=new Label();l、 Text=“1”代码>从您的代码中,但在XAML上输入,并在设置代理后将MainPage
设置为此XAML。然后,除非wrapcompressevalue?.SetValue(属性,d),否则将不会调用compressevalueDelegate
感谢您的想法,但是有了这个解决方案,我必须为我所有的应用程序创建数千个BindablePropertylabels@codetale,对于label,它没有TextChanged事件,所以当文本更改时,您没有任何事件可处理,例如条目。如果你不想创建bindableproperty,我建议你可以像这样使用bind:,我不确定它是bug还是其他,所以你可以在github上创建一个线程以获得更好的帮助。实际上,我不是在跟踪文本更改事件,而是在文本即将更新时尝试验证和更改值,这就是为什么强制值是handy@codetale现在,你的问题解决了吗?如果是,请在此处共享您的解决方案,并将其标记为答案,谢谢。hi@cherry谢谢您的想法,但使用此解决方案,我必须为我的所有帐户创建数千个BindablePropertylabels@codetale,对于label,它没有TextChanged事件,所以当文本更改时,您没有任何事件可处理,例如条目。如果您不想创建bindableproperty,我建议您可以像这样使用bind:,我不确定它是bug还是其他,所以您可以在github创建一个线程以获得更好的帮助。实际上,我没有尝试跟踪文本更改事件,我尝试验证和