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 Membersprivate 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
}
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:
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 Membersprivate 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
}
developmenta...
Member
5 Points
18 Posts
HtmlPage.IsEnabled returns false when hosted in browser
Jun 03, 2009 10:14 PM | LINK
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):
developmenta...
Member
5 Points
18 Posts
Re: HtmlPage.IsEnabled returns false when hosted in browser
Jun 07, 2009 04:40 PM | LINK
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:
DispatcherFacade.cs: