private void SelectNewViewPresenter(IVsUIDataSource dataContext, ContentControl contentHost) { Shell.ThreadHelper.ThrowIfNotOnUIThread(); try { ProviderDataSourceWrapper providerModel = new ProviderDataSourceWrapper(dataContext); VsUIElementDescriptor newViewDescriptor = GetAppropriateViewDescriptor(providerModel); Telemetry.Client.TrackPageView(providerModel.Name); // Decide if the view should change, or if we can just update the datasource on the existing view. if (ShouldChangeView(newViewDescriptor)) { // Update the new content and Dispose of the old using IDisposable oldContent = contentHost.Content as IDisposable; SetNewView(contentHost, newViewDescriptor, providerModel.ViewModel); } else { // Use the same view, just update the data context. UpdateCurrentViewElementModel(contentHost, providerModel.ViewModel); } } catch (ViewCreationException ex) { HandleViewCreationException(contentHost, ex); } catch (UIFactoryException ex) { HandleViewCreationException(contentHost, ex); } }
/// <summary> /// Given the diagnostics data model of a single Component Diagnostics provider, /// select the appropriate view. If the "ViewFactory" property contains a Guid /// then use that, along with the "View" field to create a new UI element from /// a UI factory. /// </summary> /// <param name="provider">Data model of the selected provider</param> /// <returns>A descriptor of the view to display</returns> /// <exception cref="ArgumentNullException">The provider is null</exception> /// <exception cref="ViewCreationException">The view could not be created for the /// reason given in the exception message</exception> static VsUIElementDescriptor GetAppropriateViewDescriptor(ProviderDataSourceWrapper provider) { if (provider == null) { throw new ViewCreationException(Messages.ProviderIsNull); } VsUIElementDescriptor viewDescriptor = provider.ViewDescriptor; // If no factory was specified, then use the default view if (viewDescriptor.Factory == Guid.Empty) { viewDescriptor = DefaultViewDescriptor; } return(viewDescriptor); }
/// <summary> /// When the ContentControl is reloaded, recreate the current view /// </summary> void viewPresenter_Reloaded(object sender, RoutedEventArgs e) { Shell.ThreadHelper.ThrowIfNotOnUIThread(); ContentControl contentHost = (ContentControl)sender; contentHost.Loaded -= viewPresenter_Reloaded; try { ProviderDataSourceWrapper providerModel = new ProviderDataSourceWrapper(contentHost.DataContext as IVsUIDataSource); VsUIElementDescriptor viewDescriptor = GetAppropriateViewDescriptor(providerModel); SetNewView(contentHost, viewDescriptor, providerModel.ViewModel); } catch (ViewCreationException ex) { HandleViewCreationException(contentHost, ex); } catch (UIFactoryException ex) { HandleViewCreationException(contentHost, ex); } }