具有ViewModel和行为的Xamarin视图 [英] Xamarin View with ViewModel and Behaviour

查看:70
本文介绍了具有ViewModel和行为的Xamarin视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含当前用户的用户资料的视图.该视图包含一个视图模型,该模型具有双向绑定到诸如用户名或电子邮件地址之类的单个属性.该视图还具有一个行为,该行为会验证输入并显示错误,表明输入无效.由于双向绑定,即使行为表明输入错误,视图模型也会更新该值.我需要解决这个问题.

I have a View which contains the userprofile of the current user. The view contains a viewmodel with a two-way binding to the single attributes like username or email adress. The view also has a behaviour, which validates the input and shows an error, of the input is not valid. Because of the two-way binding, the viewmodel updates the value even if the behavior says the input is wrong. I need to solve that.

我当前的方法是使用viewmodel中的行为作为属性.因此,我可以在视图模型的属性设置器中访问行为的属性.所以我无法停止对错误输入的更新.但是我无法从视图的xaml中的viewmodel访问行为.那是我完全可以做到的方式吗?

My current approach is to use include the behavior in the viewmodel as a attribute. So I can access the attributes of the behavior in the setter of the attributes in the viewmodel. So I cant stop the update to the wrong inputs. But I can not get access the behavior from my viewmodel in the xaml of the view. Is that a way I can do it at all?

我的下一个方法是将行为的"isValid"属性传递给视图模型.但是又一次,我不知道该怎么做,有可能吗?

My next approach would be to pass the "isValid" attribute of the behavior to the viewmodel. But here again, I don't know how to do it, is it possible at all?

最后一种方法是在视图模型中创建命令,使用单向绑定将其绑定到用户配置文件中的新按钮,然后以某种方式将输入从视图传递到命令并更新用户配置文件.

Last approach would be to create a command in the viewmodel, binding it to a new button in the userprofile, using a one-way binding and somehow pass the inputs from the view to the command and updateing the userprofile.

可能有人可以帮助我吗?

May be here is someone who can help me?

我发布了一些代码:

这是我的行为的一个条目:

This is a single entry with my behavior:

<Entry x:Name="phoneNumber" Text="{Binding TelephoneNum, Mode=TwoWay}">
    <Entry.Behaviors>
        <behaviors:TelNumBehavior x:Name="NumValidatorUser"/>
    </Entry.Behaviors>
</Entry>

TelNum行为:

public class TelNumBehavior : Behavior<Entry>
{
...
public static readonly BindablePropertyKey IsVisiblePropertyKey = 
        BindableProperty.CreateReadOnly("IsVisible", typeof(bool), typeof(TelNumBehavior), false);
public static readonly BindableProperty IsVisibleProperty = IsVisiblePropertyKey.BindableProperty;

...
public bool IsVisible
{
    get { return (bool) this.GetValue(IsVisibleProperty); }
    set
      {
          this.SetValue(IsVisiblePropertyKey, value);
       }
    }
...
(OnAttachedTo, OnDetachingFrom)
...

private void bindable_TextChanged(object sender, TextChangedEventArgs e)
{
    Entry entry = sender as Entry;
    this.IsVisible = (entry.Text == "")
            ? true
            : (Regex.IsMatch(e.NewTextValue, mobileRegex) || Regex.IsMatch(e.NewTextValue, fixedLineRegex));
    entry.TextColor = this.IsVisible ? Color.Default : Color.Red;
}

总结一下:我使用该行为来验证某些输入.但是视图模型不知道输入是否正确.这是目前我的问题.我不知道如何告知ViewModel输入状态.

To sum it up: I use that behavior for validation of some inputs. But the viewmodel does not know if the inputs are correct or not. And this is currently my problem. I dont know how to inform the viewmodel about the state of the inputs.

}

推荐答案

我认为,回答这个问题有点晚了,但是我今天有同样的想法.

I think, it's a little late to answer it but I had the same thing today.

我如何解决.

  1. 在您的cs端,您可以在其中获得viewmodel的实例(应该将其设为单例)

  1. In your cs side, where you get instace of the viewmodel ( you should make it singleton)

在您的TelNumBehavior类中,您应该使用该单例实例.

In your TelNumBehavior class, you should use that singleton instance.

在该实例中,您可以在条目每次更改时触发一个命令.

From that instance, you can trigger a command for each time the entry changes.

如果您仍然遇到相同的问题,我也可以共享代码.

If you still have same issue, I can share the code too.

这篇关于具有ViewModel和行为的Xamarin视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆