gpt4 book ai didi

xaml - 如何将命令绑定(bind)到 Xamarin 表单中的条目 "Unfocused"事件

转载 作者:行者123 更新时间:2023-12-03 09:54:47 27 4
gpt4 key购买 nike

我有一个命令(或一段代码),我想在特定条目失去焦点时运行它。

我正在使用 MVVM,所以我想使用绑定(bind)方式而不是在我的 Form.xaml.cs 中编写代码。 .

当我的条目在 XAML 内失去焦点时,如何让我的命令(或我的一段代码)运行文件?

最佳答案

有一个 EventToCommandBehavior 的示例在 Xamarin.Forms 示例 (see here) 中。使用它你可以绑定(bind)你的控件

<Entry>
<Entry.Behaviors>
<behaviors:EventToCommandBehavior
EventName="Unfocused"
Command="{Binding EntryUnfocused}" />
</Entry.Behaviors>
</Entry>

然后定义 EntryUnfocused在你的类 viewmodel.cs您的特定 View 的文件,如下所示:

public class LoginViewModel : XamarinViewModel
{
public ICommand EntryUnfocused{ get; protected set; }
public LoginViewModel()
{
EntryUnfocused= new Command(CompletedCommandExecutedAsync);
}

private void CompletedCommandExecutedAsync(object param)
{
//yourcode...
}
}

如果您使用的是 Prism 库,您可以使用它们的实现,它更成熟一些(允许您通过指定应传递的参数来映射事件参数), see here .

( 请注意 您必须将行为所在的相应命名空间添加到 XAML 文件中)。

关于xaml - 如何将命令绑定(bind)到 Xamarin 表单中的条目 "Unfocused"事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54434934/

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