在我的父ViewModel中,我正在使用以下代码通过ReactiveUI的路由导航到ChildView
appBootstrapper.Router.Navigate.Execute(new ChildViewModel(HostScreen)).Subscribe();
此代码可以正常工作。
但是,当子视图关闭时,我想执行一些额外的操作(让我们使用通过bool ViewModel属性隐藏活动指示器的示例),例如
ShowActivityIndicator = true;
appBootstrapper.Router.Navigate.Execute(new ChildViewModel(HostScreen)).Subscribe();
ShowActivityIndicator = false;
由于显示了子视图,然后立即执行ShowActivityIndicator = false;
,因此该代码未按预期执行。
如何重写代码,以便在ShowActivityIndicator = false;
关闭后 执行ChildView
?谢谢!
更新
由于Glenn的建议,我尝试使用WhenNavigatingFromObservable().Subscribe(...)
技术,但是lambda代码从不执行。我的代码如下...
var microsoftSignInVM = new ViewModel.MicrosoftSignInVM(HostScreen);
//
microsoftSignInVM.WhenNavigatingFromObservable().Subscribe((_) =>
{
ShowActivityIndicator = true;
//
Data.Synchronisation.SynchroniseLocalDataCacheAsync();
//
appBootstrapper.Router.Navigate.Execute(new ViewModel.SelectJobVM(HostScreen)).Subscribe();
//
ShowActivityIndicator = false;
});
//
appBootstrapper.Router.Navigate.Execute(microsoftSignInVM).Subscribe();
我在lambda中放置了一个断点,它从未被击中。
更新2-更新
我创建了几个最小的测试类。 这些在WPF项目中的工作效果很好,但不能使用XAMARIN形式 ...
共享
TestAV.xaml.cs
namespace TestApp.Test
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class TestAV : ReactiveContentPage<TestAVM>
{
public TestAV()
{
InitializeComponent();
//
this.WhenActivated(
disposables =>
{
this.BindCommand(this.ViewModel, x => x.ClickCommand, x => x.button)
.DisposeWith(disposables);
});
}
}
}
TestAVM.cs
namespace TestApp.Test
{
public class TestAVM : ReactiveObject, IEnableLogger, IRoutableViewModel
{
public TestAVM(IScreen hostScreen)
{
_HostScreen = hostScreen;
//
TestBVM testBVM = new TestBVM(_HostScreen);
//
_ClickCommand = ReactiveCommand.CreateFromObservable(() => _HostScreen.Router.Navigate.Execute(testBVM).Select(_ => Unit.Default));
//
testBVM.WhenNavigatingFromObservable().Subscribe((_) =>
{
ShowActivityIndicator = true;
});
}
//
public bool ShowActivityIndicator { get; set; }
//
private readonly ReactiveCommand<Unit, Unit> _ClickCommand; public ReactiveCommand<Unit, Unit> ClickCommand => _ClickCommand;
//
private readonly IScreen _HostScreen; public IScreen HostScreen => _HostScreen;
//
public string UrlPathSegment => "TestA";
}
}
TestBV.xaml.cs
namespace TestApp.Test
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class TestBV : ReactiveContentPage<TestBVM>
{
public TestBV()
{
InitializeComponent();
//
this.WhenActivated(
disposables =>
{
this
.BindCommand(this.ViewModel, x => x.ClickCommand, x => x.button)
.DisposeWith(disposables);
});
}
}
}
TestBVM.cs
namespace TestApp.Test
{
public class TestBVM : ReactiveObject, IEnableLogger, IRoutableViewModel
{
public TestBVM(IScreen hostScreen)
{
_HostScreen = hostScreen;
}
//
public ReactiveCommand<Unit, Unit> ClickCommand => _HostScreen.Router.NavigateBack;
//
private readonly IScreen _HostScreen; public IScreen HostScreen => _HostScreen;
//
public string UrlPathSegment => "TestB";
}
}
Xamarin特定文件
AppBootstrapper.cs
namespace TestApp
{
public class AppBootstrapper : ReactiveObject, IScreen
{
public AppBootstrapper(IMutableDependencyResolver dependencyResolver = null, RoutingState router = null)
{
Router = router ?? new RoutingState();
//
RegisterParts(dependencyResolver ?? Locator.CurrentMutable);
//
Router.Navigate.Execute(new Test.TestAVM(this));
}
public RoutingState Router { get; private set; }
private void RegisterParts(IMutableDependencyResolver dependencyResolver)
{
dependencyResolver.RegisterConstant(this, typeof(IScreen));
//
dependencyResolver.Register(() => new Test.TestAV(), typeof(IViewFor<Test.TestAVM>));
dependencyResolver.Register(() => new Test.TestBV(), typeof(IViewFor<Test.TestBVM>));
}
public Page CreateMainPage()
{
return new RoutedViewHost();
}
}
}
TestAV.xaml
<?xml version="1.0" encoding="utf-8" ?>
<rxui:ReactiveContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:rxui="clr-namespace:ReactiveUI.XamForms;assembly=ReactiveUI.XamForms"
xmlns:vm="clr-namespace:TestApp.Test"
x:TypeArguments="vm:TestAVM"
x:Class="TestApp.Test.TestAV">
<ContentPage.Content>
<StackLayout>
<Button x:Name="button" Text="click A" HorizontalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage.Content>
</rxui:ReactiveContentPage>
TestBV.xaml
<?xml version="1.0" encoding="utf-8" ?>
<rxui:ReactiveContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:rxui="clr-namespace:ReactiveUI.XamForms;assembly=ReactiveUI.XamForms"
xmlns:vm="clr-namespace:TestApp.Test"
x:TypeArguments="vm:TestBVM"
x:Class="TestApp.Test.TestBV">
<ContentPage.Content>
<StackLayout>
<Button x:Name="button" Text="click B" HorizontalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage.Content>
</rxui:ReactiveContentPage>
WPF特定文件
TestAV.xaml
<rxui:ReactiveUserControl x:Class="TestApp.TestAV"
x:TypeArguments="vm:TestAVM"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:rxui="http://reactiveui.net"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:TestApp.Test">
<Grid>
<Button x:Name="button" Content="click A" />
</Grid>
</rxui:ReactiveUserControl>
TestBV.xaml
<rxui:ReactiveUserControl x:Class="TestApp.TestBV"
x:TypeArguments="vm:TestBVM"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:rxui="http://reactiveui.net"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:TestApp.Test">
<Grid>
<Button x:Name="button" Content="click B" />
</Grid>
</rxui:ReactiveUserControl>
值得注意的是,如果我使用
_ClickCommand = ReactiveCommand.CreateFromObservable(() => _HostScreen.Router.Navigate.Execute(new TestAVM()).Select(_ => Unit.Default));
在 TestBVM.cs 中,然后lambda会按预期触发。
似乎Router.NavigateBack
的Xamarin Forms实现存在问题,但我愿意接受其他观察结果!
答案 0 :(得分:2)
您可以采用多种不同的方式。
在调用导航堆栈上的操作时,您可以订阅路由器的多个观察项。 RoutableViewModelMixin
中还有一种扩展方法,称为WhenNavigatingFromObservable
。
public void ShowChildView()
{
ShowActivityIndicator = true;
var childViewModel = new ChildViewModel(HostScreen);
// Use the extension method to get an observable when the child view is navigated away from, subscribe and set the variable.
childViewModel.WhenNavigatingFromObservable().Subscribe(x => ShowActivityIndicator = false);
appBootstrapper.Router.Navigate.Execute(childViewModel).Subscribe();
}
第二个选项,如果您不介意子级上发生的功能,则可以使用WhenActivated()机制
public ChildView()
{
this.WhenActivated(disposable =>
{
// do activation logic.
return new[] { Disposable.Create(() => DoDeactivationLogic());
}