xamarin.forms - 从 Xamarin 表单中的事件执行 ReactiveUI 命令
问题描述
我在 Xamarin Forms 页面中有输入字段,我想在用户完成向其中输入文本时触发 ReactiveUI 命令。我正在使用 ReactiveUI.Events.XamForms 并尝试根据 Unfocused 事件触发命令,但我不确定如何设置命令以使其正常工作。
这是我的 XAML:
<?xml version="1.0" encoding="utf-8" ?>
<rxui:ReactiveContentPage
x:Class="XamarinReactiveUISwipeView.MainPage"
x:TypeArguments="vm:MainPageViewModel"
xmlns:vm="clr-namespace:XamarinReactiveUITest.ViewModel;assembly=XamarinReactiveUITest"
xmlns:rxui="clr-namespace:ReactiveUI.XamForms;assembly=ReactiveUI.XamForms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:ios="clr- namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
xmlns="http://xamarin.com/schemas/2014/forms"
ios:Page.UseSafeArea="true">
<StackLayout>
<StackLayout Margin="10,0,0,0" Orientation="Horizontal">
<Label Text="Task ID: " />
<Entry x:Name="EntryTaskID" />
</StackLayout>
<StackLayout Margin="10,0,0,0" Orientation="Horizontal">
<Label Text="Task Name: " />
<Entry x:Name="EntryTaskName" />
</StackLayout>
</StackLayout>
</rxui:ReactiveContentPage>
这是我背后的代码:
public partial class MainPage : ReactiveContentPage<MainPageViewModel>
{
public MainPage()
{
InitializeComponent();
ViewModel = new MainPageViewModel();
this.WhenActivated(disposable =>
{
this.Bind(ViewModel, vm => vm.TheTaskItem.TaskID, page => page.EntryTaskID.Text)
.DisposeWith(disposable);
this.Bind(ViewModel, vm => vm.TheTaskItem.TaskName, page => page.EntryTaskName.Text)
.DisposeWith(disposable);
EntryTaskName.Events().Unfocused.InvokeCommand(ViewModel, vm => vm.TheCommand);
});
}
}
这是我的模型:
public class TaskItem
{
public TaskItem() { }
public string TaskID { get; set; }
public string TaskName { get; set; }
}
这是我的视图模型:
public class MainPageViewModel : ReactiveObject
{
public MainPageViewModel()
{
TheTaskItem = new TaskItem { TaskID = "1", TaskName = "TheTaskName" };
TheCommand = ReactiveCommand.Create<FocusEventArgs, Unit>(ExecuteTheCommand);
}
public ReactiveCommand<FocusEventArgs, Unit> TheCommand { get; }
private void ExecuteTheCommand(FocusEventArgs args)
{
//do something
}
private TaskItem _theTaskItem;
public TaskItem TheTaskItem
{
get => _theTaskItem;
set => this.RaiseAndSetIfChanged(ref _theTaskItem, value);
}
}
在上面的视图模型中,它不会编译,但我不知道如何设置 ExecuteTheCommand 方法。错误是:
“void MainPageViewModel.ExecuteTheCommand(FocusEventArgs)”返回类型错误
但是在查看示例时,看起来具有 void 返回的方法使用了 Unit 类型。
我需要在这里做什么才能正确设置命令以使其正常工作?
解决方案
在上面的评论中,OP 说如果发生变化,这会起作用:
TheCommand = ReactiveCommand.Create<FocusEventArgs, Unit>(ExecuteTheCommand);
至:
TheCommand = ReactiveCommand.Create<FocusEventArgs>(ExecuteTheCommand);
出乎意料的是,保存 的结果的变量的类型Create
不使用相同的泛型类型签名。它需要是:
public ReactiveCommand<FocusEventArgs, Unit> TheCommand { get; }
根据 Rodney Littles 在评论中的链接中的文档,Unit
用于表示“无效返回类型”。(通用类型“TOutput”将为“void”;但“void”不是有效的通用类型。)
推荐阅读
- r - 在tidygraph中过滤
- reactjs - 搜索栏故障,随机(据说)发生
- c++ - std::sort 不适用于我的自定义迭代器
- python - GAN中生成器的输出形状和判别器的输入形状如何匹配?
- rollupjs - Rollup 不会捆绑 Proptypes 依赖项
- ios - 升级的 Appcelerator 现在 ios Simulators 将无法工作
- python - 函数返回空数据框
- c++ - 如何生成 C++ 定义调用的动态生成
- asp.net-core - .Net Core Azure Active Directory 身份验证问题(照片)
- javascript - 我正在尝试使用 async/await 来获取服务,但第二个服务返回不填充我的变量