代码之家  ›  专栏  ›  技术社区  ›  user1202032

Prism和Xamarin。表单:IApplicationLifecycle

  •  0
  • user1202032  · 技术社区  · 8 年前

    我有一个viewmodel,它继承自BindableBase并实现IApplicationLifecycle。但是,IApplicationLifecycle。OnResume()和IApplicationLifecycle。OnSleep()在iOS和android上都没有被调用。

    如何使用IApplicationLifecycle?我似乎找不到它的任何文档。提前感谢!

    在我的应用程序中。xaml。cs我有:

    NavigationService.NavigateAsync("NavigationPage/MainPage");
    

    和我的主页。xaml如下所示:

    <TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:PrismApp.Views;assembly=PrismApp"
             xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
             prism:ViewModelLocator.AutowireViewModel="True"
             x:Class="PrismApp.Views.MainPage"
             Title="MainPage">
    
    <TabbedPage.Children>
        <local:APage Icon="tabicon_mapspage.png" />
        <local:BPage Icon="tabicon_profilepage.png" />
        <local:CPage Icon="tabicon_statuspage.png" />
    </TabbedPage.Children>
    

    最后,我的MainPageViewModel如下所示:

    public class MainPageViewModel : BindableBase, IApplicationLifecycle
    {
        private string _title;
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }
        public void OnResume()
        {
            var debug = 42;
        }
    
        public void OnSleep()
        {
            var debug = 42;
        }
    }
    

    https://github.com/RandomStuffAndCode/PrismApp

    1 回复  |  直到 8 年前
        1
  •  1
  •   user5420778 user5420778    8 年前

    由于您在TabbedPage中,因此只能在TabbedPage上调用iApplicationWare方法。CurrentPage(SelectedTab),因为这是当前活动页面。

    顺便说一句,您可以通过覆盖应用程序类中的OnResume和OnSleep方法来更改此行为,以实现您的目标。以现有代码为指导: https://github.com/PrismLibrary/Prism/blob/master/Source/Xamarin/Prism.Forms/PrismApplicationBase.cs#L171