你可以在 WPF 应用程序中使用 jQuery 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21482232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can you use jQuery in WPF Applications?
提问by Ben
I have been creating a WPF Application over the past few months and I wanted to add abit of animation to my Application.I have been told before that jQuery is a good Javascript library to use for animation.
在过去的几个月里,我一直在创建一个 WPF 应用程序,我想在我的应用程序中添加一些动画。之前有人告诉我 jQuery 是一个很好的用于动画的 Javascript 库。
The problem is after looking around abit there are not many things on the internet that lets me know if this is possible.
问题是在环顾四周之后,互联网上没有多少东西可以让我知道这是否可行。
I'm very new to jQuery so i wont know much. If jQuery isnt possible in WPF, what is?
我对 jQuery 很陌生,所以我不会知道太多。如果 jQuery 在 WPF 中是不可能的,那是什么?
回答by Terrance
So technically you can use jQuery in WPF. Just not in a way that you might necessarily like. With the web browser controlyou could load html, css, and javascript from local or remote sources.
所以从技术上讲,您可以在 WPF 中使用 jQuery。只是不是您可能喜欢的方式。使用Web 浏览器控件,您可以从本地或远程源加载 html、css 和 javascript。
Please note* I did not make this example up from scratch. It comes from this site
请注意*我没有从头开始制作这个例子。它来自这个网站
in xaml add the control
在 xaml 中添加控件
<WebBrowser x:Name="wbMain" Margin="30"></WebBrowser>
and in code
并在代码中
wbMain.Navigate(new Uri("c:/MyFileThatUsesJqueryAndHasJqueryAsARef.htm"));
Additionally communication between the two is also possible but does require a bit of work. You would have to create a com visible class with full trust security
此外,两者之间的通信也是可能的,但确实需要一些工作。您必须创建一个具有完全信任安全性的 com 可见类
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[ComVisible(true)]
public class ObjectForScriptingHelper
{
Mainwindow mExternalWPF;
public ObjectForScriptingHelper(Window1w)
{
this.mExternalWPF = w;
}
public void InvokeMeFromJavascript(string jsscript)
{
this.mExternalWPF.tbMessageFromBrowser.Text = string.Format("Message :{0}", jsscript);
}
}
With that you would create an instance in your class and access it via javascript like
有了它,您将在类中创建一个实例并通过 javascript 访问它,例如
<input type="text" id="txtMessage" />
<input type="button" value="InvokeMe" onclick="javascript:window.external.InvokeMeFromJavascript(document.getElementById('txtMessage').value);" />
Please look at the article for even more stuff you can do.
请查看这篇文章,了解更多您可以做的事情。
Caveats
注意事项
- This is more than likely not the best way to go for a new desktop application. If its just a matter of using an existing tool out of familiarity then I'd consider other avenues of approach like learning WPF or looking to something like this questionor oneofthese
If in fact you are out to use as much of an existing code base coded in html and javascript with with an offline app you could utilize this approach to recycle your existing code base by all means look at this or method or one of the other methods mentioned like using WPF animations.
事实上,如果您打算在离线应用程序中尽可能多地使用以 html 和 javascript 编码的现有代码库,您可以利用这种方法来回收您现有的代码库,请务必查看此或方法或其他方法之一提到像使用WPF 动画。

