No property, BindableProperty, or event found for "Clicked", or mismatching type between value and property. TextChanged method doesn't get recognized

huangapple go评论50阅读模式
英文:

No property, BindableProperty, or event found for "Clicked", or mismatching type between value and property. TextChanged method doesn't get recognized

问题

这是要翻译的部分:

"所以,我找到了一些类似的问题,但它们没有帮助我解决我的问题。由于这是我第一次使用Xamarin.Forms,我犯了一个错误,没有使用ViewModels,所以我正在将所有方法移到ViewModels中。我认为我理解了,TextChanged是一个事件,而不是一个BindableProperty,所以我正在做的事情不会起作用。

我正在检查基于TextChanged事件的正则表达式来验证电子邮件。这是代码:

<controls:StandardEntry x:Name="email" Text="{Binding Email}" TextChanged="{Binding ValidateEmail}"/>

public void ValidateEmail(object sender, TextChangedEventArgs e)
{
if (RegexUtils.ValidEmail().IsMatch(email))
{
// 做一些操作
}
else
{
// 做一些操作
}
}"

英文:

So, I found some similar questions but it didn't help me to solve my problem. Since it's the first time using Xamarin.Forms, I made the error of not using ViewModels so I'm moving all the methods to viewmodels. As I think I understood, TextChanged is an event and not a BindableProperty so what I'm doing won't work.

I'm checking regex to validate an email based on TextChanged.
This is the code:

&lt;controls:StandardEntry x:Name=&quot;email&quot; Text=&quot;{Binding Email}&quot; TextChanged=&quot;{Binding ValidateEmail}&quot;/&gt;

public void ValidateEmail(object sender, TextChangedEventArgs e)
        {
            if (RegexUtils.ValidEmail().IsMatch(email))
            {
                // do stuff
            }
            else
            {
                // do stuff
            }
        }

答案1

得分: 0

  • 你不能将 TextChanged 移动到视图模型中,它应该属于视图本身。

  • [观点] 在视图中将一切放在一起,而不使用视图模型是一种有效的技巧。只有在有理由这样做时才创建视图模型。

  • 如果你确实创建了一个视图模型,这并不意味着 EVERYTHING 都要移到其中。MVVM 的理念是视图操作数据;数据存放在视图模型中。

  • 那些只需要数据而不引用视图元素的方法可以(可选地)移到视图模型中。

  • 一些事件可以转换为命令。因此,按钮点击事件可以在视图模型中用命令替代。根据需要让方法执行哪个更方便,是一种判断。无论是在代码后端使用 Click 还是在视图模型中使用 Command 都可以。

  • 但是,诸如 TextChanged 这样的事件处理程序从本质上来说是视图元素的一部分。它应该保留在视图中。它保留在视图中,因为它使用事件参数属性,以允许对文本元素进行操作。

  • [为了方便起见] 为了方便访问视图模型,可以在视图的代码后端中定义一个属性:

MyViewModel VM => (MyViewModel)BindingContext;

然后访问视图模型的属性:

... VM.SomeProperty ...

因此,如果 TextChanged 以前能够直接使用 SomeProperty,因为它位于视图中,那么请在 TextChanged 中的所有位置将其更改为 VM.SomeProperty

英文:
  • You cannot move TextChanged to a viewmodel. It belongs in the view itself.

  • [OPINION] It is a valid technique to have everything in the view, not have a viewmodel. Only make a viewmodel if you have a reason to do so.

  • If you do make a viewmodel, that doesn't mean EVERYTHING moves to it. The idea of MVVM is that a view operates on Data; that data goes in the VM.

  • Methods that only need the Data, that don't refer to View Elements, can (optionally) move to the VM.

  • Some events can be converted to commands. Thus Button Click event can be replaced by a Command in viewmodel. Its a judgement call whether to use Click in code-behind, or Command in viewmodel. Use whichever is more convenient, given what you need the method to do.

  • However an event handler such as TextChanged is inherently part of the functioning of its view element. It stays in View. It stays because it uses event arg properties, to allow manipulation of the text element.

  • [FOR CONVENIENCE] To make access to VM convenient, define a property in your view's code behind:

MyViewModel VM =&gt; (MyViewModel)BindingContext;

Then to access a VM property:

... VM.SomeProperty ...

So if TextChanged previously was able to use SomeProperty directly, because it was in the view, change that to VM.SomeProperty everywhere in TextChanged.

huangapple
  • 本文由 发表于 2023年7月10日 22:08:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76654577.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定