微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从 Xamarin 表单中的事件执行 ReactiveUI 命令

如何解决从 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" Orientation="Horizontal">
            <Label Text="Task ID: " />
            <Entry x:Name="EntryTaskID" />
        </StackLayout>
        <StackLayout Margin="10,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 用于表示“void 返回类型”。 (泛型类型“TOutput”将是“void”;但“void”不是有效的泛型类型。)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。