gpt4 book ai didi

c# - Xamarin Forms/ReactiveUI Router-在关闭 subview 后显示 subview 并执行父ViewModel代码

转载 作者:行者123 更新时间:2023-12-03 10:25:39 25 4
gpt4 key购买 nike

在我的父ViewModel中,我使用以下代码通过ReactiveUI的路由导航到ChildView

appBootstrapper.Router.Navigate.Execute(new ChildViewModel(HostScreen)).Subscribe();

此代码按预期方式工作。

但是,当关闭 subview 时,我想执行一些额外的操作(让我们使用通过bool ViewModel属性隐藏 Activity 指示器的示例),例如
ShowActivityIndicator = true;
appBootstrapper.Router.Navigate.Execute(new ChildViewModel(HostScreen)).Subscribe();
ShowActivityIndicator = false;

由于显示了 subview ,因此该代码未按预期执行,然后立即执行 ShowActivityIndicator = false;

我如何重写代码,以便在关闭并关闭 ShowActivityIndicator = false;之后执行 ?谢谢!

更新

感谢Glenn的建议,我尝试使用ChildView技术,但是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会按预期触发。

Xamarin Forms实现WhenNavigatingFromObservable().Subscribe(...)似乎有问题,但是我愿意接受其他观察!

最佳答案

您可以采用几种不同的方法来处理它。

路由器具有几个可观察对象,您可以在调用导航堆栈上的操作时进行订阅。您可以使用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();
}

第二种选择,如果您不介意 child 身上发生的功能,则可以使用WhenActivated()机制

public ChildView()
{
this.WhenActivated(disposable =>
{
// do activation logic.
return new[] { Disposable.Create(() => DoDeactivationLogic());
}

此处有更多详细信息 https://reactiveui.net/docs/handbook/when-activated/

关于c# - Xamarin Forms/ReactiveUI Router-在关闭 subview 后显示 subview 并执行父ViewModel代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56147770/

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