Created
December 29, 2016 16:00
-
-
Save Genteure/c94483c39de952cb4eaee20800f0254d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Jint; | |
using Jint.Native; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using System.Windows; | |
using System.Windows.Markup; | |
using System.Windows.Threading; | |
namespace JintJavascriptTest | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Engine e = new Engine((o) => { o.Strict(); }); | |
e.SetValue("log", new Action<object>(Console.WriteLine)); | |
e.SetValue("WindowBuilder", new WindowBuilder()); | |
e.Execute( | |
@"var xaml = """"; | |
xaml += ""<Window xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' ""; | |
xaml += "" xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ""; | |
xaml += "" xmlns:d='http://schemas.microsoft.com/expression/blend/2008' ""; | |
xaml += "" xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006' ""; | |
xaml += "" mc:Ignorable='d' ""; | |
xaml += "" Title='This is title' Height='350' Width='525'> ""; | |
xaml += "" <Grid> ""; | |
xaml += "" <TextBlock FontSize='30'> ""; | |
xaml += "" ABAbababababab ""; | |
xaml += "" </TextBlock> ""; | |
xaml += "" </Grid> ""; | |
xaml += ""</Window> ""; | |
var window = WindowBuilder.Build(xaml); | |
log(window.Dispatcher.CheckAccess()); | |
log(window.Dispatcher.Invoke); | |
WindowBuilder.STARun(function(){ | |
log(1); | |
window.Show(); | |
}); | |
WindowBuilder.InvokeRun(window.Dispatcher, function(){ | |
log(2); | |
window.Show(); | |
}); | |
window.Dispatcher.Invoke(function(){ | |
log(3); | |
window.Show(); | |
}, null); | |
//ShowWindowSafe(window); | |
function ShowWindowSafe(w) | |
{ | |
if (w.Dispatcher.CheckAccess()) | |
w.Show(); | |
else | |
w.Dispatcher.Invoke(9, function(){w.Show}); | |
} | |
" | |
); | |
while(true) | |
{ | |
Thread.Sleep(500); | |
} | |
} | |
internal static void log(object obj) | |
{ | |
return;//for breakpoint | |
} | |
} | |
internal class WindowBuilder | |
{ | |
internal WindowBuilder() | |
{ | |
} | |
public Window Build(string xaml) | |
{ | |
try | |
{ | |
byte[] b = Encoding.UTF8.GetBytes(xaml); | |
var m = new MemoryStream(); | |
m.Write(b, 0, b.Length); | |
Window w = null; | |
Thread thread = new Thread(() => | |
{ | |
w = XamlReader.Parse(xaml) as Window; | |
//w.Dispatcher.Invoke(); | |
//w.Show(); //test show | |
Thread.Sleep(1);//for breakpoint | |
}); | |
thread.SetApartmentState(ApartmentState.STA); | |
thread.Start(); | |
thread.Join(); | |
Thread.Sleep(1);//for breakpoint | |
return w; | |
} | |
catch(Exception ex) | |
{ | |
return null; | |
} | |
} | |
public void InvokeRun(Dispatcher dis, Delegate d) | |
{ | |
var t = dis.InvokeAsync(() => { d.DynamicInvoke(JsValue.Undefined, new[] { JsValue.Undefined }); }); | |
//t.Wait(TimeSpan.FromSeconds(5)); | |
//seems not woking | |
return; | |
} | |
public void STARun(Delegate d) | |
{ | |
Thread thread = new Thread(() => { d.DynamicInvoke(JsValue.Undefined, new[] { JsValue.Undefined }); }); | |
thread.SetApartmentState(ApartmentState.STA); | |
thread.Start(); | |
thread.Join(); | |
//not woking | |
return; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment