警卫条款未解除 [英] Guard Clause Not Firing

查看:64
本文介绍了警卫条款未解除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我一直在尝试使防护子句与Caliburn.Micro和绑定的文本框一起使用。

So I have been trying to get guard clauses to work with Caliburn.Micro and a bound textbox.

视图:

<TextBox x:Name="UserAccount_DisplayName" Margin="-10,-5,-10,8"/>

<phone:PhoneApplicationPage.ApplicationBar>
  <shell:ApplicationBar IsVisible="True" IsMenuEnabled="False">
     <shell:ApplicationBar.Buttons>
        <cal:AppBarButton IconUri="\Resources\Iconography\appbar.check.rest.png"
                          Text="Save"
                          Message="SaveAndNavigateToAddAccountView" />
     </shell:ApplicationBar.Buttons>
  </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

ViewModel:

The ViewModel:

public class EditAccountNameViewModel: PropertyChangedBase







    public Account UserAccount
    {
        get
        {
            return account;
        }
        set
        {
            account = value;
            NotifyOfPropertyChange(() => UserAccount);
            NotifyOfPropertyChange(() => CanSaveAndNavigateToAddAccountView);
        }
    }







    public bool CanSaveAndNavigateToAddAccountView
    {
        get
        {
            if (string.IsNullOrEmpty(UserAccount.DisplayName) == true)
            {
                return false;
            }

            return true;
        }   
    }







    public void SaveAndNavigateToAddAccountView()
    {
        CommitAccountToStorage();
        navigationService.UriFor<AddAccountViewModel>().Navigate();
    }

出于某种原因,在我开始在文本框中输入内容后,guard子句未触发,这是我本来应该发生的。有任何想法吗?

For some reason the guard clause is not firing after I begin typing in the textbox, which is what I would have assumed should happen. Any ideas?

推荐答案

在文本框中键入内容然后选择另一个元素时,guard子句是否会触发(因此,文本框会丢失焦点)?如果是这样,请尝试模拟绑定的UpdateSourceTrigger = PropertyChanged设置。请参阅> UpdateSourceTrigger = PropertyChanged的答案。等价于Windows Phone 7文本框,以了解如何模拟这种行为。

Does the guard clause fire when you type something in the textbox and then select another element (so that textbox loses focus)? If so, try simulating UpdateSourceTrigger=PropertyChanged setting of a binding. See anwsers to "UpdateSourceTrigger=PropertyChanged" equivalent for a Windows Phone 7 TextBox to see how to simulate this behavior.

编辑:我看到的是,您(按照惯例)绑定到 DisplayName属性。这意味着,当您在文本框中键入内容时,不会调用EditAccountNameViewModel.UserAccount属性的设置方法。相反,将调用UserAccount.DisplayName上的setter。我建议您做的是在ViewModel中创建另一个属性,例如UserAccountDisplayName,看起来像这样,然后绑定到它:

I see, that you are binding (by convention) to a "DisplayName" property of UserAccount. This means, that setter of EditAccountNameViewModel.UserAccount property will not be called when you type something in the textbox. Instead, a setter on UserAccount.DisplayName will be called. What I'd suggest you to do is to create another property in your ViewModel, say UserAccountDisplayName, that would look sth like this, and bind to it instead:

public string UserAccountDisplayName
{
   get { return UserAccount.DisplayName; }
   set 
   {
      UserAccount.DisplayName = value;
      NotifyOfPropertyChange(() => UserAccountDisplayName);
      NotifyOfPropertyChange(() => CanSaveAndNavigateToAddAccountView);
   }
}

此+模拟PropertyChanged触发器应该有效。

This + simulating PropertyChanged trigger should work.

这篇关于警卫条款未解除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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