保护条款未触发

发布于 2024-11-24 15:04:00 字数 1630 浏览 2 评论 0原文

所以我一直在尝试让保护子句与 Caliburn.Micro 和绑定文本框一起使用。

视图:

<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:

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();
    }

由于某种原因,在我开始在文本框中键入内容后,保护子句没有触发,这是我认为应该发生的情况。有什么想法吗?

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

The View:

<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>

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();
    }

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鲜肉鲜肉永远不皱 2024-12-01 15:04:00

当您在文本框中键入内容然后选择另一个元素(以便文本框失去焦点)时,保护子句是否会触发?如果是这样,请尝试模拟绑定的 UpdateSourceTrigger=PropertyChanged 设置。请参阅 Windows Phone 7 的 "UpdateSourceTrigger=PropertyChanged" 等效项的答案TextBox 查看如何模拟此行为。

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

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

This + 模拟 PropertyChanged 触发器应该可以工作。

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.

EDIT: 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);
   }
}

This + simulating PropertyChanged trigger should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文