【问题标题】:MessagingCenter and NavigationService using reflection使用反射的 MessagingCenter 和 NavigationService
【发布时间】:2019-11-14 06:51:16
【问题描述】:

我希望改进在 Xamarin.Forms 中开发的移动应用程序。

我的功能如下:onResume of the application 我要重新加载用户所在的页面。

目前我使用MessagingCenter来操作如下代码。

不幸的是,我的应用程序开始有很多页面,并且不再可读。

因此,我希望将我的类型 (viewModel) 作为导航服务的参数传递 - 我的研究将我引向反射的概念,但我不知道我的问题是否可以解决。

// App.xaml.cs

            protected override void OnResume()
        {
            // Handle when your app resumes

            Page currPage = ((NavigationPage)((MasterDetailPage)Application.Current.MainPage).Detail).CurrentPage;

            MessagingCenter.Send<App, Page>(this, "Hi", currPage);
        }

然后在我的 BaseViewModel 中:

// BaseViewModel.cs

public ViewModelBase()
{
    DialogService = ViewModelLocator.Instance.Resolve<IDialogService>();
    NavigationService = ViewModelLocator.Instance.Resolve<INavigationService>();
    AuthenticationService = ViewModelLocator.Instance.Resolve<IAuthenticationService>();

    MessagingCenter.Subscribe<App, Page>(this, "Hi", async (sender, arg) =>
    {
        // Do something whenever the "Hi" message is received

        Type viewModel = NavigationService.GetViewModelTypeForPage(arg.GetType());

        if(viewModel == typeof(AboutViewModel))
        {
            Debug.WriteLine("AboutViewModel");
            await NavigationService.NavigateToAsync<AboutViewModel>();
            return;
        }

        if (viewModel == typeof(CardViewModel))
        {
            Debug.WriteLine("CardViewModel");
            await NavigationService.NavigateToAsync<CardViewModel>();
            return;                    
        }

        ...

    });

}

【问题讨论】:

    标签: xamarin mvvm xamarin.forms navigation messaging


    【解决方案1】:

    我会给你一些关于如何在使用MessagingCenter 时使你的代码可读的想法。

    首先,你可以有一个实现MessagingCenter.Subscribe的BasePage和一个调用loadData的方法:

    public partial class BasePage : ContentPage
    {
        public BasePage()
        {
            MessagingCenter.Subscribe<App, string>(this, "Hi", (sender, arg) =>
            {
                // Do something whenever the "Hi" message is received
                loadData();
            });
        }
    
        public virtual void loadData()
        {
    
        }
    }
    

    那么,当你新建一个页面需要在应用恢复时刷新,你可以让页面继承BasePage类型:

    public partial class MainPage : BasePage
    {
        public MainPage()
        {
            InitializeComponent();
    
            loadData();
        }
    
        public override void loadData()
        {
            base.loadData();
            Console.WriteLine("loadData");
        }
    
    }
    

    还有 xaml:

    <?xml version="1.0" encoding="utf-8" ?>
    <bases:BasePage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:d="http://xamarin.com/schemas/2014/forms/design"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 xmlns:bases="clr-namespace:App52"
                 mc:Ignorable="d"
                 x:Class="App52.MainPage">
    
    </bases:BasePage>
    

    因此您不必在每个页面中实现MessagingCenter.Subscribe,可以在BasePage 中进行管理。

    我不熟悉reflection,所以reflection 可能无法帮助您实现这一目标。希望这会有所帮助。

    【讨论】:

    • 我的目标是替换类型比较,并为 NavigationService 的泛型类型使用泛型方式。
    猜你喜欢
    • 1970-01-01
    • 2012-02-10
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多