我有循环运行,等待和其他事情的业务逻辑。当前,这在后面的代码中。
据我所知,这是错误的地方,应该将其放置在viewModel中(如果错误,请更正)。如果是这种情况,那么我应该在我的VM中有一个OnAppearing方法,如果是这样,应该如何将OnAppearing传递给视图模型?
当前,我的页面OnAppearing看起来像这样:
protected async override void OnAppearing()
{
base.OnAppearing();
Title = Settings.mode.Text() + " Deck";
vm.LearnViewVisible = Settings.mode.IsLearn();
vm.PracticeViewVisible = Settings.mode.IsPractice();
vm.QuizViewVisible = Settings.mode.IsQuiz();
vm.QuizStartViewVisible = false;
如果我要将大部分业务逻辑转移到ViewModel,那是否意味着所有这些都将转移到我在ViewModel中创建的OnAppearing()方法?
答案 0 :(得分:2)
其他方式是使用行为
...
<ContentPage.Behaviors>
<behaviors:EventHandlerBehavior EventName="Appearing">
<behaviors:InvokeCommandAction Command="{Binding PageAppearingCommand}" />
</behaviors:EventHandlerBehavior>
<behaviors:EventHandlerBehavior EventName="Disappearing">
<behaviors:InvokeCommandAction Command="{Binding PageDisappearingCommand}" />
</behaviors:EventHandlerBehavior>
</ContentPage.Behaviors>
...
答案 1 :(得分:0)
以下是我的解决方案示例
public partial class TaskDetailsPage : MvvmContentPage
{
private readonly TaskDetailsViewModel _model;
public TaskDetailsPage()
{
InitializeComponent();
Shell.SetNavBarIsVisible(this, true);
Shell.SetTabBarIsVisible(this, false);
_model = BindingContext as TaskDetailsViewModel;
}
protected override string NavigationRoute => UniqeCodes.Routes.TaskDetailsPage;
protected override void OnAppearing()
{
_model.Init();
}
}
答案 2 :(得分:0)
这就是我链接我的 Viewmodel 的方式。我建议使用以下方法设置 ViewModelBase:VModelActive 和 VModelInactive
背后的代码:
{
"devDependencies": {
"@fullhuman/postcss-purgecss": "^4.0.0",
"@fullhuman/vue-cli-plugin-purgecss": "~4.0.2",
"@vue/cli-plugin-babel": "~4.5.0",
"@vue/cli-plugin-eslint": "~4.5.0",
"@vue/cli-plugin-router": "~4.5.0",
"@vue/cli-plugin-vuex": "~4.5.0",
"@vue/cli-service": "~4.5.0",
"@vue/compiler-sfc": "^3.0.0",
"@vue/eslint-config-prettier": "^6.0.0",
"autoprefixer": "^10.2.5",
"babel-eslint": "^10.1.0",
"eslint": "^6.7.2",
"eslint-plugin-prettier": "^3.1.3",
"eslint-plugin-vue": "^7.0.0-0",
"node-sass": "^5.0.0",
"postcss": "^8.2.8",
"prettier": "^1.19.1",
"sass-loader": "^10.1.1",
"tailwindcss": "^2.0.3"
}
}
查看模型
public partial class YourClass : ContentPage
{
ViewModelClass viewModelClass;
public YourClass()
{
InitializeComponent();
viewModelClass = new ViewModelClass();
this.BindingContext = viewModelClass;
}
protected override void OnAppearing()
{
base.OnAppearing();
viewModelClass.VModelActive(this, EventArgs.Empty);
}
protected override void OnDisappearing()
{
base.OnDisappearing();
viewModelClass.VModelInactive(this, EventArgs.Empty);
}
}