C# 激活未点火时的反应UI

C# 激活未点火时的反应UI,c#,xamarin,mvvm,xamarin.forms,reactiveui,C#,Xamarin,Mvvm,Xamarin.forms,Reactiveui,我正试图使用下面的代码作为一种安全的方法,从我的viewmodel的构造函数中调用异步服务,如本文所建议的。问题是,这里面并没有任何东西。什么时候有动机开火,你们知道为什么吗 这是我的密码: class MainViewModel : ReactiveObject, ISupportsActivation, IMainViewModel { private IDataService _dataService; private Part _part; public Pa

我正试图使用下面的代码作为一种安全的方法,从我的viewmodel的构造函数中调用异步服务,如本文所建议的。问题是,这里面并没有任何东西。什么时候有动机开火,你们知道为什么吗

这是我的密码:

class MainViewModel : ReactiveObject, ISupportsActivation, IMainViewModel
{
    private IDataService _dataService;

    private Part _part;

    public Part MyPart
    {
        get { return _part; }
        set { this.RaiseAndSetIfChanged(ref _part, value); }
    }

    public MainViewModel(IDataService dataService)
    {
        _dataService = dataService;

        this.WhenActivated(disposables =>
        {
            _dataService.GetPart("9176900515")
                .ToObservable()
                .Subscribe(
                    result => { MyPart = result; },
                    exception => { LogMe.Log<string>(exception.Message); }
                )
                .DisposeWith(disposables);
        });
    }

    private readonly ViewModelActivator activator = new ViewModelActivator();
    ViewModelActivator ISupportsActivation.Activator
    {
        get { return activator; }
    }

}
要使激活在视图模型内工作,视图模型必须是实现IViewFor的视图的视图模型

MainViewModel的WhenActivated随后将由视图的WhenActivated调用

更新:

这是在WPF中完成的,但它在所有平台WPF、UWP、Xamarin上都受支持

该视图实现IViewFor。根据最佳实践,ViewModel属性是Xam.Forms中的DependencyProperty或BindableProperty

public partial class MainWindow : Window, IViewFor<MainViewModel>
{
    public MainWindow()
    {
        InitializeComponent();

        this.WhenActivated(d =>
        {
        // This will be called
    });
    }

    public MainViewModel ViewModel
    {
        get => (MainViewModel)GetValue(ViewModelProperty);
        set => SetValue(ViewModelProperty, value);
    }
    public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register(nameof(ViewModel), typeof(MainViewModel), typeof(MainWindow), new PropertyMetadata(null));

    object IViewFor.ViewModel
    {
        get => ViewModel;
        set => ViewModel = value as MainViewModel;
    }
}
要使激活在视图模型内工作,视图模型必须是实现IViewFor的视图的视图模型

MainViewModel的WhenActivated随后将由视图的WhenActivated调用

更新:

这是在WPF中完成的,但它在所有平台WPF、UWP、Xamarin上都受支持

该视图实现IViewFor。根据最佳实践,ViewModel属性是Xam.Forms中的DependencyProperty或BindableProperty

public partial class MainWindow : Window, IViewFor<MainViewModel>
{
    public MainWindow()
    {
        InitializeComponent();

        this.WhenActivated(d =>
        {
        // This will be called
    });
    }

    public MainViewModel ViewModel
    {
        get => (MainViewModel)GetValue(ViewModelProperty);
        set => SetValue(ViewModelProperty, value);
    }
    public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register(nameof(ViewModel), typeof(MainViewModel), typeof(MainWindow), new PropertyMetadata(null));

    object IViewFor.ViewModel
    {
        get => ViewModel;
        set => ViewModel = value as MainViewModel;
    }
}
实现ViewModel 首先,这里描述了WhenActivated模式:

WhenActivated是跟踪一次性物品的一种方法。除此之外,它还可以用于推迟ViewModel的设置,直到真正需要它。WhenActivated还使我们能够启动或停止对热观测做出反应,比如定期ping网络端点的后台任务或更新用户当前位置的观测任务。此外,当ViewModel出现在舞台上时,可以使用WhenActivated触发启动逻辑

在这个问题上,这部分模式似乎已经做对了

实现视图 要激活ViewModel,必须完全遵循中提供的特定模式

以下是关键领域:

public partial class MainWindow : IViewFor<AppViewModel>
{
    // Using a DependencyProperty as the backing store for ViewModel.
    // This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ViewModelProperty =
        DependencyProperty.Register("ViewModel",
            typeof(AppViewModel), typeof(MainWindow),
            new PropertyMetadata(null));

    public MainWindow()
    {
        InitializeComponent();
        ViewModel = new AppViewModel();

    // ....
    }

    // ....

    // Our main view model instance.
    public AppViewModel ViewModel
    {
        get => (AppViewModel)GetValue(ViewModelProperty);
        set => SetValue(ViewModelProperty, value);
    }

    // This is required by the interface IViewFor, you always just set it to use the
    // main ViewModel property. Note on XAML based platforms we have a control called
    // ReactiveUserControl that abstracts this.
    object IViewFor.ViewModel
    {
        get => ViewModel;
        set => ViewModel = (AppViewModel)value;
    }
}
实现ViewModel 首先,这里描述了WhenActivated模式:

WhenActivated是跟踪一次性物品的一种方法。除此之外,它还可以用于推迟ViewModel的设置,直到真正需要它。WhenActivated还使我们能够启动或停止对热观测做出反应,比如定期ping网络端点的后台任务或更新用户当前位置的观测任务。此外,当ViewModel出现在舞台上时,可以使用WhenActivated触发启动逻辑

在这个问题上,这部分模式似乎已经做对了

实现视图 要激活ViewModel,必须完全遵循中提供的特定模式

以下是关键领域:

public partial class MainWindow : IViewFor<AppViewModel>
{
    // Using a DependencyProperty as the backing store for ViewModel.
    // This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ViewModelProperty =
        DependencyProperty.Register("ViewModel",
            typeof(AppViewModel), typeof(MainWindow),
            new PropertyMetadata(null));

    public MainWindow()
    {
        InitializeComponent();
        ViewModel = new AppViewModel();

    // ....
    }

    // ....

    // Our main view model instance.
    public AppViewModel ViewModel
    {
        get => (AppViewModel)GetValue(ViewModelProperty);
        set => SetValue(ViewModelProperty, value);
    }

    // This is required by the interface IViewFor, you always just set it to use the
    // main ViewModel property. Note on XAML based platforms we have a control called
    // ReactiveUserControl that abstracts this.
    object IViewFor.ViewModel
    {
        get => ViewModel;
        set => ViewModel = (AppViewModel)value;
    }
}

这仅适用于WPF应用程序吗?这只提到了WPF,我无法让它工作。太多的样板导致太多新的红色波形。我在任何地方都找不到完整的示例。这仅适用于WPF应用程序吗?这只提到了WPF,我无法让它工作。太多的样板导致太多新的红色波形。我在任何地方都找不到完整的例子,那是行不通的。设置ViewModel不会触发激活。含糊不清的问题会产生含糊不清的答案。也许在要点上分享你的代码可以帮助别人帮助你。我现在不使用ReactiveUI,现在在C++/PCL/OpenCV/C/Linux上,可能在6-12个月后再次使用ReactiveUI。我已经找到了答案。我们需要从构造函数中调用this.WhenActivated=>{}。这不起作用。设置ViewModel不会触发激活。含糊不清的问题会产生含糊不清的答案。也许在要点上分享你的代码可以帮助别人帮助你。我现在不使用ReactiveUI,现在在C++/PCL/OpenCV/C/Linux上,可能在6-12个月后再次使用ReactiveUI。我已经找到了答案。我们需要从构造函数中调用this.WhenActivated=>{}。