locked
HtmlPage.IsEnabled returns false when hosted in browser RRS feed

  • Question

  •  I have a project where I am testing for HtmlPage.IsEnabled and when it loads in the browser (IE, FF) the property returns false.

     I am running on Windows 7 x64 with Visual Studio 2008. I experience this when I run the project from Visual Studio by hitting F5. Can anyone confirm this? From what I read, I should be able to test HtmlPage.IsEnabled to determine if I'm running in the browser or not.

     For now my workaround is to trap UnauthorizedAccessException. The code in question skips calls to Dispatcher.BeginInvoke if IsEnabled == false (see my previous post as to why I am doing this).

     Here's the code in question (IDispatcher is a custom interface defined elsewhere in my project):

     

    	public class DispatcherFacade : IDispatcher
    	{
    		#region IDispatcher Members
    		private static bool isHosted = false;
    
    		public void BeginInvoke(Action action)
    		{
    			if ((Deployment.Current == null || HtmlPage.IsEnabled == false))
    			{
    				if (!isHosted)
    				{
    					try
    					{
    						action.Invoke();
    						return;
    					}
    					catch (UnauthorizedAccessException)
    					{
    						//HtmlPage.IsEnabled returned the wrong value
    						isHosted = true;
    					}
    				}
    			}
    
    			Deployment.Current.Dispatcher.BeginInvoke(action);
    		}
    
    		public void BeginInvoke(Delegate method, params object[] args)
    		{
    			if (Deployment.Current == null)
    				return;
    
    			Deployment.Current.Dispatcher.BeginInvoke(method, args);
    		}
    
    		#endregion
    	}
    
      
    Wednesday, June 3, 2009 5:14 PM

Answers

  •  Ok, well I figured out a solution. I'd still be curious to find out why this is happening. My solution is that when I register an instance DispatcherFacade in the Unity container I pass in the current value of HtmlPage.IsEnabled. In my case I register DispatcherFacade in Bootstrapper.cs, so the value works here since Bootstrapper is called directly from App.xaml.cs. Here's the new code:

     Bootstrapper.cs:

     

    		protected override DependencyObject CreateShell()
    		{
    			// calling Resolve instead of directly initing allows use of dependency injection
    			Shell shell = Container.Resolve();
    
    #if SILVERLIGHT
    			Application.Current.RootVisual = shell; 
    #else
    			shell.Show();
    #endif
    
    			Container.RegisterInstance(new DispatcherFacade(HtmlPage.IsEnabled));
    
    			return shell;
    		}
    
      

     

    DispatcherFacade.cs: 

    	public class DispatcherFacade : IDispatcher
    	{
    		public DispatcherFacade(bool isHtmlEnabled)
    		{
    			/* this value needs to be passed in when it is initialized 
    			 * because for some reason calling HtmlPage.IsEnabled always 
    			 * returns false in this class. I'll need to look into the
    			 * source code of HtmlPage.IsEnabled to find out why this 
    			 * happens
    			 */ 
    			isEnabled = isHtmlEnabled;
    		}
    
    		#region IDispatcher Members
    		private bool isEnabled = false;
    
    		public void BeginInvoke(Action action)
    		{
    			if (isEnabled == false)
    			{
    				action.Invoke();
    				return;
    			}
    
    			Deployment.Current.Dispatcher.BeginInvoke(action);
    		}
    
    		public void BeginInvoke(Delegate method, params object[] args)
    		{
    			if (Deployment.Current == null)
    				return;
    
    			Deployment.Current.Dispatcher.BeginInvoke(method, args);
    		}
    
    		#endregion
    	}
    
     

     

    Sunday, June 7, 2009 11:40 AM