我正在寻找一种在str = "[12,34,35,231]"
str.split(/\D+/).reject(&:empty?)
=> ["12", "34", "35", "231"]
# If you want the elements as numbers instead of strings, do:
str.split(/\D+/).reject(&:empty?).map(&:to_i)
=> [12, 34, 35, 231]
控件中运行Angular2
应用程序并在两个方向上建立通信的方法。让我举两个例子:
C#代码:
CefSharp
角度代码:
private void RegisterJsObjects()
{
_browser.RegisterJsObject("cefTestClass", new TestClass());
}
class TestClass
{
public void csF(string message)
{
MessageBox.Show(message, "#C", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
}
}
<button (click) ="cefTestClass.csF('text')"> C# Button </button>
应用程序中调用Angular
个函数,因为它们在编译后会得到不同的名称,我无法访问它们。答案 0 :(得分:4)
已注册的对象绑定到javascript
的窗口对象。您在调用时错过了window
这个词。你必须这样称呼它:
<button (click) ="appComponentFunction()"> C# Button </button>
并在AppComponent中:
appComponentFunction(){
window['cefTestClass'].csF('text');
}
注册对象的方法以小写字母开头非常重要,否则javascript
不会执行它们。
如果你想从c#调用Angular函数,你可以在你的c#类中注册回调,如下所示:
private IJavascriptCallback callback;
public void registerCallback(IJavascriptCallback callback)
{
this.callback = callback;
}
现在您必须在Angular应用程序中定义回调(例如,在根组件的NgOnInit中):
window['cefTestClass'].registerCallback(function() {
window.alert("Hallo");
});
现在您可以随时在c#
中调用该函数:
callback.ExecuteAsync();
答案 1 :(得分:0)
如此处所述:https://github.com/cefsharp/CefSharp/issues/2990,“RegisterJsObject”函数已过时。
以下是推荐的替代品:
//Old method
browser.RegisterJsObject("bound", new BoundObject(), options: BindingOptions.DefaultBinder);
//Replaced with
CefSharpSettings.WcfEnabled = true;
browser.JavascriptObjectRepository.Settings.LegacyBindingEnabled = true;
browser.JavascriptObjectRepository.Register("bound", new BoundObject(), isAsync:false, options: BindingOptions.DefaultBinder);
异步函数也以类似的方式替换。