I have to unfortunately fire this window on a different thread than the main app. Then I am trying to update it every time a string input _html changes in my MakeWindow class.
Right now I am getting an error saying that I cannot execute this because I am calling it from a different thread that created it. How can i make it work here?
Is there a way to implement that event listener on the same thread that window exists? keep in mind that the _html will always be set and updated on the main thread. Ideas?
Thank you!
my classes:
namespace Windamow
{
public static class WebBrowserUtility
{
public static readonly DependencyProperty BindableSourceProperty =
DependencyProperty.RegisterAttached("BindableSource", typeof(string),
typeof(WebBrowserUtility), new UIPropertyMetadata(null,
BindableSourcePropertyChanged));
public static string GetBindableSource(DependencyObject obj)
{
return (string)obj.GetValue(BindableSourceProperty);
}
public static void SetBindableSource(DependencyObject obj, string value)
{
obj.SetValue(BindableSourceProperty, value);
}
public static void BindableSourcePropertyChanged(DependencyObject o,
DependencyPropertyChangedEventArgs e)
{
var webBrowser = (WebBrowser)o;
webBrowser.NavigateToString((string)e.NewValue);
}
}
public class Windamow
{
private DynamoWindow window;
public static string html;
internal void ThreadProc()
{
string appName = "";
try
{
appName = Path.GetFileName(System.Reflection.Assembly.GetEntryAssembly().Location);
const string IE_EMULATION = @"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION";
using (var fbeKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(IE_EMULATION, true))
{
fbeKey.SetValue(appName, 9000, Microsoft.Win32.RegistryValueKind.DWord);
}
}
catch (Exception ex)
{
MessageBox.Show(appName + "\n" + ex.ToString(), "Unexpected error setting browser mode!");
}
window = new DynamoWindow();
window.ShowDialog();
}
internal Windamow()
{
Thread t = new Thread(ThreadProc);
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
/// <summary>
/// asd
/// </summary>
/// <param name="launch"></param>
/// <param name="_html"></param>
/// <returns></returns>
public static DynamoWindow MakeWindow(bool launch, string _html)
{
if (launch)
{
if (MyProject.Utility.WebBrowserUtility.IsWindowOpen<Window>("MainWindow"))
{
html = _html;
return null;
}
else
{
html = _html;
Windamow mow = new Windamow();
return mow.window;
}
}
else
{
return null;
}
}
}
}
xaml:
<Window x:Class="Windamow.DynamoWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
xmlns:ns="clr-namespace:Windamow"
Title="MainWindow"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<!--<WebBrowser x:Name="browser" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>-->
<WebBrowser x:Name="browser" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ns:WebBrowserUtility.BindableSource="{Binding ReportPage}"/>
</Grid>
xaml.cs:
namespace Windamow
{
/// <summary>
/// Interaction logic for DynamoWindow.xaml
/// </summary>
public partial class DynamoWindow : Window
{
public DynamoWindow()
{
InitializeComponent();
}
}
}