Esempio n. 1
0
        /// <summary>
        /// Called when the <c>TargetView</c> has just been loaded.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        public override async Task OnTargetViewLoadedAsync(object sender, EventArgs e)
        {
            await CompleteViewModelClosingAsync();

            // Do not call base because it will create a VM. We will create the VM ourselves
            //base.OnTargetControlLoaded(sender, e);

            // Manually updating target control content wrapper here (not by content property changed event handler),
            // because in WinRT UserControl does NOT update bindings while InitializeComponents() method is executing,
            // even if the Content property was changed while InitializeComponents() running there is no triggering of a binding update.
            CreateViewModelWrapper();

#if NET
            if (!SkipSearchingForInfoBarMessageControl)
            {
                Log.Debug("Searching for an instance of the InfoBarMessageControl");

                _infoBarMessageControl = TargetView.FindParentByPredicate(o => o is InfoBarMessageControl) as InfoBarMessageControl;

                ApiCop.UpdateRule <UnusedFeatureApiCopRule>("UserControlLogic.InfoBarMessageControl",
                                                            rule => rule.IncreaseCount(_infoBarMessageControl != null, TargetViewType.FullName));

                if (CreateWarningAndErrorValidatorForViewModel)
                {
                    ApiCop.UpdateRule <UnusedFeatureApiCopRule>("UserControlLogic.CreateWarningAndErrorValidator",
                                                                rule => rule.IncreaseCount(_infoBarMessageControl != null, TargetViewType.FullName));
                }

                Log.Debug("Finished searching for an instance of the InfoBarMessageControl");

                if (_infoBarMessageControl == null)
                {
                    Log.Warning("No InfoBarMessageControl is found in the visual tree of '{0}', consider using the SkipSearchingForInfoBarMessageControl property to improve performance", GetType().Name);
                }
            }
            else
            {
                Log.Debug("Skipping the search for an instance of the InfoBarMessageControl");
            }
#endif

            if (!CloseViewModelOnUnloaded && (ViewModel != null))
            {
                // Re-use view model
                Log.Debug("Re-using existing view model");
            }

            if (ViewModel == null)
            {
                // Try to create view model based on data context
                await UpdateDataContextToUseViewModelAsync(TargetView.DataContext);
            }

            if (DisableWhenNoViewModel)
            {
                TargetView.IsEnabled = (ViewModel != null);
            }
        }