WPF play youtube video - c#

I'm trying to embed a youtube video into WebBrowser, however, it shows just a blank screen
<WebBrowser util:WebBrowserUtility.BindableSource="{Binding UrlContent}"/>
DI:
public static void BindableSourcePropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
WebBrowser browser = o as WebBrowser;
if (browser != null)
{
string uri = e.NewValue as string;
if (string.IsNullOrEmpty(uri)) return;
browser.NavigateToString(uri);
}
}
Output:
Tried:
Formated HTML
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html>
<head>
</head>
<iframe width='560' height='315' src='http://www.youtube.com/embed/diB65scQU' frameborder='0' allowfullscreen></iframe>
<body>
</body>
</html>
Plain Link => http://www.youtube.com/embed/diB65scQU
http://www.youtube.com/embed/diB65scQU
Results - the same - just a blank view
Any ideas why?

You can use WebView2 control instead, it is an official replacement, take a look WebView2.
See WebView2Samples on github

Related

How to open ASP.NET page using Shortcut Keys in C#?

I am developing an ASP.NET web application. in which have a home page,
when we press F2 in home page we need to load item Master page &
when press F4 in home page we need to load city Master page.
I don't know how to open these pages in ASP.NET C# using short cut keys..
Can any one help me?
write a javascript code onkeypress
function keypress() {
switch (event.keyCode) {
case ascii of f2:
//redirect/or load item master
}
}
or
<script type="text/javascript">
document.attachEvent('onkeyup', KeysShortcut);
// Now implement the KeysShortcut
function KeysShortcut ()
{
if (event.keyCode == 49)
{
document.getElementById('<%= button1.ClientID %>').click();
}
}
</script>
Using linkbutton Example...
C#
Note : The javascript, depending on the keycode, invokes the click event of the element, in our case the link button. The element is obtained by using document.getElementById(' '<%=Control.ClientID%>'').
Note: While using Master Pages, you need to refer to the control using the control's ClientID. I have directly used the ID generated.
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript" language="javascript" src="shortcut.js"> </script>
</head>
Page 1
Add a javascript file to the project called shortcut.js. Add the following code to the javascript file.
function HomeKey()
{
if(event.keyCode == 72)
{
document.getElementById('ctl00_ContentPlaceHolder1_btnHome').click();
}
}
now on backside
C#
protected void Page_Load(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "Home", "document.attachEvent('onkeyup',HomeKey);", true);
}

how to get selected text in webbrowser control

I am able to get the selected text of a webbrowser control in WPF by the following:
IHTMLDocument2 doc1 = webBrowser.Document as IHTMLDocument2;
IHTMLDocument3 doc = webBrowser.Document as IHTMLDocument3;
IHTMLSelectionObject currentSelection = doc1.selection;
if (doc1.selection.type == "Text")
{
IHTMLTxtRange range = (IHTMLTxtRange)doc1.selection.createRange();
}
This works just fine and if I set the value of range.text to something else it changes the value of the text. The only issue I'm having is on web pages such as Gmail that have some kind of WYSIWYG editor on it, the selection.type is always 'None'. I suspect it is because the text editor is technically a child document. I'm not sure how to find child documents and check to see if text is selected. Can anyone help me? Thanks!
You could check if document.activeElement is a frame and has contentWindow property (document.activeElement.contentWindow != null) Then you could use contentWindow.document to get to the frame's inner document. Do this recursively until you find a frame with document.selection != null.
To illustrate this:
main.html:
<!DOCTYPE html>
<body>
<iframe src="iframe.html"></iframe>
</body>
iframe.html:
<!DOCTYPE html>
<head>
<script>
window.onload = function()
{
window.focus();
document.execCommand("SelectAll", false);
}
</script>
</head>
<body contentEditable="true">
This is editable
</body>
C#:
var text = this.wb.Document.InvokeScript("eval", new object[] {
"document.activeElement.contentWindow.document.selection.createRange().text" });
MessageBox.Show(text.ToString());
Shows:
This is editable

How to display partial HTML page in Windows 8 metro app

Let's say I have the following page:
<html>
<body>
<content1>
</content1>
<content2>
</content2>
<content3>
</content3>
</body>
</html>
I want in my Metro app to display only the part of the page contained between <content2></content2>. For a full page I would use a <WebView> and the Navigate() method. But I don't seem to find a way to adapt that to what I need.
to actually write any solution code I would need to know what are Content1 and content3. assuming them being DIV with an ID, I can say
void WebView5_LoadCompleted(object sender, NavigationEventArgs e)
{
string script = #"var d=document.getElementById('content1');d.style.visibility='hidden'";
string[] args = { script };
string foo = WebView5.InvokeScript("eval", args);
}`

Create dynamic .aspx pages in asp.net

The following code creates file.aspx and file.aspx.cs:
protected void Button1_Click(object sender, EventArgs e)
{
string fielName = Server.MapPath("~/file.aspx");
TextWriter tw = new StreamWriter(fielName);
tw.WriteLine(#"<%# Page Language=""C#"" AutoEventWireup=""true"" CodeFile=""file.aspx.cs"" Inherits=""file"" %>
<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">
<html xmlns=""http://www.w3.org/1999/xhtml"">
<head runat=""server"">
<title></title>
</head>
<body>
<form id=""form1"" runat=""server"">
<div>
</div>
</form>
</body>
</html>
");
tw.Close();
tw = new StreamWriter(fielName + ".cs");
tw.WriteLine(#"using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class file : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(""new File "");
}
}
");
tw.Close();
}
I want to make the page name written in my Textbox.
I have tried putting textbox in html source of above code, but I'm getting an error.
CodeFile="""+TextBox1.Text+""" Inherits="""+TextBox1.Text+"""
You would be much farther ahead to work the way ASP.NET "thinks" about the page. I once worked on a very large, dynamic questionnaire. All of the controls were generated dynamically along with validations and everything else. At it's core, the way we did it was:
place a panel on the page
add controls to the panel
the code very roughly would look something like this:
var btn = new Button();
btn.ID = "theId";
btn.Text = "hi";
pnlDynamic.Controls.Add(btn);
Because you're dealing with dynamic controls, you might also want to make sure that you understand the page life-cycle...: http://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx
Make sure your web project is declared as a web site rather than a web application.
A web site is willing to dynamically compile each page on demand, unlike a web application, so something like this is in principle doable. If you really want to do it.

Upload an image with wmd?

Is it possible with the wmd editor to add a button to let the user upload an image to the web server and place the corresponding img markdown in the textbox? If not, will another good inplace editor do it? Context: I'm using asp.net mvc, C# and I am a true beginner with javascript.
A brief perusal of the WMD seems to indicate that this feature is not supported directly and that the control is not particularly pluggable.
That being said, there's nothing stopping you from creating a button/upload-field/whatever that sends an image to your servers and injects the appropriate:
<img src="http://your.server.com/path/to/attachments/..." />
Into the control's underlying textarea.
Here's a variation to the minimal example that comes with WMD:
<!DOCTYPE html>
<html>
<head>
<title>WMD minimal example</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>
<script type="text/javascript">
$.fn.insertAtCaret = function (myValue) {
return this.each(function(){
//IE support
if (document.selection) {
this.focus();
sel = document.selection.createRange();
sel.text = myValue;
this.focus();
}
//MOZILLA/NETSCAPE support
else if (this.selectionStart || this.selectionStart == '0') {
var startPos = this.selectionStart;
var endPos = this.selectionEnd;
var scrollTop = this.scrollTop;
this.value = this.value.substring(0, startPos)
+ myValue
+ this.value.substring(endPos,
this.value.length);
this.focus();
this.selectionStart = startPos + myValue.length;
this.selectionEnd = startPos + myValue.length;
this.scrollTop = scrollTop;
} else {
this.value += myValue;
this.focus();
}
});
};
int i = 50;
function Add()
{
$("#myTextarea").insertAtCaret("![alt text][" +(i++)+"]");
// You'll need to add the link too, at the bottom
}
</script>
</head>
<body>
<form>
test
<textarea id="myTextarea" style="width: 500px; height: 200px;">*This* is a minimal example.</textarea>
</form>
<div class="wmd-preview"></div>
<script type="text/javascript" src="wmd/wmd.js"></script>
</body>
</html>
But it's only the beginnings as you can probably tell. This markdown editor looks better
I wrote a blog post that explains how I solved this. In the post, I use PHP - if you're comfortable converting my PHP logic into ASP.NET, you may find it helpful!

Categories