【问题标题】:Trigger Async Function on Other Pages by Calling it in Non-Async Constructor in C#通过在 C# 中的非异步构造函数中调用它来触发其他页面上的异步函数
【发布时间】:2020-09-22 17:42:32
【问题描述】:

在我的应用程序中,有一个异步函数ProcessOffer()。当我在构造函数中将其称为ProcessOffer 时,它可以同步工作。我想在构造函数中异步调用这个函数。

ProcessOffer() 是在CredentialViewModel 中实现的功能,但我想要它,它应该在应用程序的任何地方异步触发(IndexViewModel 等)。

如果我在 IndexPage 上,并且 Web 应用程序向移动应用程序发送请求,ProcessOffer() 应该被触发.. 实际上 ProcessOffer 所做的是,它要求用户输入 PIN,如果正确,它会向 Web 应用程序发回响应。

我尝试了其他帖子的答案,但是当我从 Web 应用程序向移动应用程序发送请求时,它们返回了错误 Autofac.Core.DependencyResolutionException: 'An exception was thrown while activating App.Name

我尝试过的解决方案。

1-https://stackoverflow.com/a/64012442/14139029

2-Task.Run(() => ProcessOffer()).Wait();

3-ProcessOffer().GetAwatier().GetResult();

CredentialViewModel.cs

namespace Osma.Mobile.App.ViewModels.Credentials
{
    public class CredentialViewModel : ABaseViewModel
    {
        private readonly CredentialRecord _credential;
        private readonly ICredentialService _credentialService;
        private readonly IAgentProvider _agentContextProvider;
        private readonly IConnectionService _connectionService;
        private readonly IMessageService _messageService;
        private readonly IPoolConfigurator _poolConfigurator;

        [Obsolete]
        public CredentialViewModel(
            IUserDialogs userDialogs,
            INavigationService navigationService,
            ICredentialService credentialService,
            IAgentProvider agentContextProvider,
            IConnectionService connectionService,
            IMessageService messageService,
            IPoolConfigurator poolConfigurator,
            CredentialRecord credential
        ) : base(
            nameof(CredentialViewModel),
            userDialogs,
            navigationService
        )
        {
            _credential = credential;
            _credentialService = credentialService;
            _agentContextProvider = agentContextProvider;
            _connectionService = connectionService;
            _messageService = messageService;
            _poolConfigurator = poolConfigurator;

            _credentialState = _credential.State.ToString();

            if (_credentialState == "Offered")
            {
                ProcessOffer();
            }
        }

        [Obsolete]
        public async Task ProcessOffer()
        {

            foreach (var item in _credential.CredentialAttributesValues)
            {
                await SecureStorage.SetAsync(item.Name.ToString(), item.Value.ToString());
            }

            var RegisteredPIN = await SecureStorage.GetAsync("RegisteredPIN");
            string PIN = await App.Current.MainPage.DisplayPromptAsync("Enter PIN", null, "Ok", "Cancel", null, 6, Keyboard.Numeric);
            if (PIN == RegisteredPIN)
            {
                try
                {
                    //await _poolConfigurator.ConfigurePoolsAsync();
                    var agentContext = await _agentContextProvider.GetContextAsync();
                    var credentialRecord = await _credentialService.GetAsync(agentContext, _credential.Id);
                    var connectionId = credentialRecord.ConnectionId;
                    var connectionRecord = await _connectionService.GetAsync(agentContext, connectionId);
                    (var request, _) = await _credentialService.CreateRequestAsync(agentContext, _credential.Id);
                    await _messageService.SendAsync(agentContext.Wallet, request, connectionRecord);
                    await DialogService.AlertAsync("Request has been sent to the issuer.", "Success", "Ok");
                }
                catch (Exception e)
                {
                    await DialogService.AlertAsync(e.Message, "Error", "Ok");
                }
            }
            else if (PIN != RegisteredPIN && PIN != null)
            {
                DialogService.Alert("Provided PIN is not correct");
            }
        }

        #region Bindable Command
        [Obsolete]
        public ICommand ProcessOfferCommand => new Command(async () => await ProcessOffer());

        public ICommand NavigateBackCommand => new Command(async () =>
        {
            await NavigationService.PopModalAsync();
        });
        #endregion

        #region Bindable Properties
        private string _credentialState;
        public string CredentialState
        {
            get => _credentialState;
            set => this.RaiseAndSetIfChanged(ref _credentialState, value);
        }
        #endregion
    }
}

IndexViewModel.cs

namespace Osma.Mobile.App.ViewModels.Index
{
    public class IndexViewModel : ABaseViewModel
    {
        private readonly IConnectionService _connectionService;
        private readonly IMessageService _messageService;
        private readonly IAgentProvider _agentContextProvider;
        private readonly IEventAggregator _eventAggregator;
        private readonly ILifetimeScope _scope;

        public IndexViewModel(
            IUserDialogs userDialogs,
            INavigationService navigationService,
            IConnectionService connectionService,
            IMessageService messageService,
            IAgentProvider agentContextProvider,
            IEventAggregator eventAggregator,
            ILifetimeScope scope
            ) : base(
                "Index",
                userDialogs,
                navigationService
           )
        {
            _connectionService = connectionService;
            _messageService = messageService;
            _agentContextProvider = agentContextProvider;
            _eventAggregator = eventAggregator;
            _scope = scope;
        }

        public override async Task InitializeAsync(object navigationData)
        {
            await base.InitializeAsync(navigationData);
        }

        public class Post
        {
            public string Success { get; set; }
            public string firstname { get; set; }
        }

        [Obsolete]
        public async Task ScanVerification(object sender, EventArgs e)
        {
            // Code
        }

        public async Task SettingsPage(SettingsViewModel settings) => await NavigationService.NavigateToAsync(settings, null, NavigationType.Modal);

        #region Bindable Command
        public ICommand SettingsPageCommand => new Command<SettingsViewModel>(async (settings) =>
        {
                await SettingsPage(settings);
        });

        [Obsolete]
        public ICommand ScanVerificationCommand => new Command(async () => await ScanVerification(default, default));
        #endregion
    }
}

App.xml.cs

[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
namespace Osma.Mobile.App
{
    public partial class App : Application
    {
        public new static App Current => Application.Current as App;
        public static IContainer Container { get; set; }

        // Timer to check new messages in the configured mediator agent every 10sec
        private readonly Timer timer;
        private static IHost Host { get; set; }

        public App()
        {
            InitializeComponent();

            timer = new Timer
            {
                Enabled = false,
                AutoReset = true,
                Interval = TimeSpan.FromSeconds(10).TotalMilliseconds
            };
            timer.Elapsed += Timer_Elapsed;
        }

        public App(IHost host) : this() => Host = host;

        public static IHostBuilder BuildHost(Assembly platformSpecific = null) =>
            XamarinHost.CreateDefaultBuilder<App>()
                .ConfigureServices((_, services) =>
                {
                    services.AddAriesFramework(builder => builder.RegisterEdgeAgent(
                        options: options =>
                        {
                            options.AgentName = "Mobile Holder";
                            options.EndpointUri = "http://11.222.333.44:5000";

                            options.WalletConfiguration.StorageConfiguration =
                                new WalletConfiguration.WalletStorageConfiguration
                                {
                                    Path = Path.Combine(
                                        path1: FileSystem.AppDataDirectory,
                                        path2: ".indy_client",
                                        path3: "wallets")
                                };
                            options.WalletConfiguration.Id = "MobileWallet";
                            options.WalletCredentials.Key = "SecretWalletKey";
                            options.RevocationRegistryDirectory = Path.Combine(
                                path1: FileSystem.AppDataDirectory,
                                path2: ".indy_client",
                                path3: "tails");

                            // Available network configurations (see PoolConfigurator.cs):
                            options.PoolName = "sovrin-test";
                        },
                        delayProvisioning: true));

                    services.AddSingleton<IPoolConfigurator, PoolConfigurator>();

                    var containerBuilder = new ContainerBuilder();
                    containerBuilder.RegisterAssemblyModules(typeof(CoreModule).Assembly);
                    if (platformSpecific != null)
                    {
                        containerBuilder.RegisterAssemblyModules(platformSpecific);
                    }

                    containerBuilder.Populate(services);
                    Container = containerBuilder.Build();
                });

        protected override async void OnStart()
        {
            await Host.StartAsync();

            // View models and pages mappings
            var _navigationService = Container.Resolve<INavigationService>();
            _navigationService.AddPageViewModelBinding<MainViewModel, MainPage>();
            _navigationService.AddPageViewModelBinding<RegisterViewModel, RegisterPage>();
            _navigationService.AddPageViewModelBinding<IndexViewModel, IndexPage>();
            _navigationService.AddPageViewModelBinding<SettingsViewModel, SettingsPage>();
            _navigationService.AddPageViewModelBinding<CredentialsViewModel, CredentialsPage>();
            _navigationService.AddPageViewModelBinding<CredentialViewModel, CredentialPage>();

            if (Preferences.Get(AppConstant.LocalWalletProvisioned, false))
            {
                await _navigationService.NavigateToAsync<MainViewModel>();
            }
            else
            {
                await _navigationService.NavigateToAsync<ProviderViewModel>();
            }

            timer.Enabled = true;
        }

        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            // Check for new messages with the mediator agent if successfully provisioned
            if (Preferences.Get(AppConstant.LocalWalletProvisioned, false))
            {
                Device.BeginInvokeOnMainThread(async () =>
                {
                    try
                    {
                        var context = await Container.Resolve<IAgentProvider>().GetContextAsync();
                        await Container.Resolve<IEdgeClientService>().FetchInboxAsync(context);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex);
                    }
                });
            }
        }
        protected override void OnSleep() =>
            // Stop timer when application goes to background
            timer.Enabled = false;

        protected override void OnResume() =>
            // Resume timer when application comes in foreground
            timer.Enabled = true;
    }
}

【问题讨论】:

    标签: c# asp.net .net xamarin .net-core


    【解决方案1】:

    看,IndexViewModel 类中有 public override async Task InitializeAsync(object navigationData) 方法。我想框架调用它来初始化视图模型。所以,因此CredentialViewModel 继承了相同的ABaseViewModel,为什么不直接覆盖CredentialViewModel 中的InitializeAsync 并从中调用ProcessOffer

        public CredentialViewModel(
            IUserDialogs userDialogs,
            INavigationService navigationService,
            ICredentialService credentialService,
            IAgentProvider agentContextProvider,
            IConnectionService connectionService,
            IMessageService messageService,
            IPoolConfigurator poolConfigurator,
            CredentialRecord credential
        ) : base(
            nameof(CredentialViewModel),
            userDialogs,
            navigationService
        )
        {
            _credential = credential;
            _credentialService = credentialService;
            _agentContextProvider = agentContextProvider;
            _connectionService = connectionService;
            _messageService = messageService;
            _poolConfigurator = poolConfigurator;
    
            _credentialState = _credential.State.ToString();
        }
    
        public override async Task InitializeAsync(object navigationData)
        {
            if (_credentialState != "Offered") return;
            await ProcessOffer();
        }
    

    无论如何,你必须避免在构造函数中调用异步操作。

    【讨论】:

    • 之前它在IndexPage 上同步弹出对话框.. 现在,它只在CredentialsViewMode.cs 页面上弹出(没有解决问题)..
    • 您可以为您的CredentialViewModelIndexViewModel 实现新的通用基类,其中将包含ProcessOffer 方法和InitializeAsync
    【解决方案2】:

    所以在我的视图模型中,如果我需要异步初始化,我通常将 OnAppearing() 传递给视图模型而不是构造函数。有人可以告诉我这是否不明智,但它解决了我的需求。

    视图背后的代码:

    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class CredentialView : ContentPage
    {
        public CredentialView()
        {
            InitializeComponent();
            BindingContext = new CredentialViewModel();
        }
    
        protected override void OnAppearing()
        {
            base.OnAppearing();
            ((CredentialViewModel)BindingContext).OnAppearing();
        }
    }
    

    查看模型:

    public class CredentialViewModel : INotifyPropertyChanged
    {
    
        public CredentialViewModel ()
        {
        }
    
        public async void OnAppearing()
        {
            await ProcessOffer();
        }
    
     }
    

    也仅供参考,我在 xamarin 上使用异步代码时遇到的最大问题是使用 BeginInvokeOnMainThread。我的理解是触摸的 UI 应该在主线程上执行! (可能是为什么他们称它为 UI 线程。哈哈) 示例:

    Device.BeginInvokeOnMainThread(() =>
    {
        observableCollection.Clear();
    });
    

    【讨论】:

    • ABaseViewModel 可能已经实现了 INotifyPropertyChanged。不过没关系,显示的代码中没有任何内容实际使用该接口。理解代码,不要只是复制粘贴。
    【解决方案3】:

    正如您之前已经问过的,在类的构造函数中启动异步代码可能是一大堆蠕虫。您应该考虑这样做:

    1. 改为在生命周期方法中启动 ProcessOffer。例如,当视图显示调用ProcessOfferCommand
    2. 使用火并忘记不阻塞构造函数:Task.Run(ProcessOffer) 不过你应该避免这种情况。
    3. 使用类似NotifyTaskStephen Cleary 在这里描述的东西:https://docs.microsoft.com/en-us/archive/msdn-magazine/2014/march/async-programming-patterns-for-asynchronous-mvvm-applications-data-binding 你可以在这里找到它的完整代码:https://github.com/StephenCleary/Mvvm.Async/blob/master/src/Nito.Mvvm.Async/NotifyTask.cs
    4. 使用带有私有构造函数的CreateAsync 模式。但是,这通常不适用于依赖注入。在解决过程中执行 IO 密集型工作并不是真正适合的地方。

    在我看来,使用 1. 或 3. 将是最好的解决方案,也许倾向于 1. 的组合,使用 NotifyTask 可以在加载完成时通知您。

    【讨论】:

    猜你喜欢
    • 2020-07-28
    • 2018-09-16
    • 2021-07-07
    • 2016-03-08
    • 2020-03-09
    • 2017-04-14
    • 2013-01-19
    • 2017-08-13
    • 2012-08-05
    相关资源
    最近更新 更多