gpt4 book ai didi

c# - Caliburn Micro Action

转载 作者:行者123 更新时间:2023-12-03 10:25:57 25 4
gpt4 key购买 nike

我对一件事感到困惑。当我删除参数 (string firstName)来自称为 ClearText 的方法(或其中之一)和 CanClearText ,当数据被清除时,该按钮不会被禁用。

你能解释发生了什么吗?

这是属性(property);

public string FirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
NotifyOfPropertyChange(() => FirstName);
}
}

这些是方法:
public bool CanClearText(string firstName)
{
return !string.IsNullOrWhiteSpace(FirstName);
}

public void ClearText(string firstName)
{
FirstName = "";
}

这是对应的文本框和按钮
<TextBox x:Name ="FirstName" MinWidth="100" Grid.Column="1" Grid.Row="2"></TextBox>
<Button Grid.Row="4" Grid.Column="1" x:Name="ClearText"> Clear Names </Button>

最佳答案

从技术上讲,您没有使用传递给 Action 和守卫的参数,因此实际上不需要它。

你也可以使用属性作为 Action 守卫

public string FirstName {
get {
return _firstName;
}
set {
_firstName = value;
NotifyOfPropertyChange(() => FirstName);
NotifyOfPropertyChange(() => CanClearText);
}
}

public bool CanClearText {
get {
return !string.IsNullOrEmpty(FirstName);
}
}

public void ClearText() {
FirstName = "";
}

之前发生的事情是 UI 不知道关于 Action 的守卫的任何更改。使用属性方法并通知 UI 它应该重新检查守卫将更新按钮的可用性。

通过使用属性方法,您还可以利用绑定(bind)到 Action 守卫的优势。比如像

如果没有理由清除文本,因为它是空的,那么您也可以隐藏按钮。

<TextBox x:Name ="FirstName" MinWidth="100" Grid.Column="1" Grid.Row="2"></TextBox>
<Border x:Name="CanClearText" Grid.Row="4" Grid.Column="1">
<Button x:Name="ClearText" Content="Clear Names" />
</Border>

通过Caliburn.Micro自动绑定(bind) CanClearText边框对属性的可见性,它会在 CanClearText 时隐藏属性为假。

关于c# - Caliburn Micro Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49805112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com