Passing Query string to silverlight - silverlight

I have a silverlight project that has many xaml pages. i have an external website that will call the silverlight website so e.g. http://mysilverlightproject:1230.com?queryString1=Page1.xaml.
i want to change the page by passing the values from query string.
is it possible to change the main xaml page to be another page from the query string?
Thanks

string val = string.Empty;
if (HtmlPage.Document.QueryString.ContainsKey(”foo”))
{val = HtmlPage.Document.QueryString["foo"];}

As far as I know you can't change Main page after it is assigned from App class. But you can use Navigation framework and navigate to needed page. In this case you also will be able to use browsers back/forward button.
This post is about navigating from code behind.

Take a look at how a Silverlight Navigation Application works. It will give you the functionality you request.

you can pass pageId to SL application by initparams specific to different URLs and load required page inside SL application instead of default start page
Init params are placed in html and are passed inside SL app, like the following
<param name="InitParameters" value="queryString=Page10" />
Inside you can use SilverlightHost class to get them
SilverlightHost host = new SilverlightHost();
if (host.InitParams.Count > 0)
{
foreach (var c in host.InitParams)
{
if(c.Key == "queryString")
RedirectToUIPage(c.Value) // your method
}
}

Related

Is approach I have mentioned OK to call AngularJS function of WebBrowser control from WinForm App

My requirement is to load HTML5/AngularJS form in the web browser control of WinForm app and exchange data between both.
To pass data from .Net form to WebBrowser control HTML/Angular form, I thought to call simple JavaScript method of HTML page and that method would subsequently trigger Angular complex workflow.
This is how I do call JavaScript function from my winform.
WebBrowserControl1.InvokeScript("FunctionName", new object[] {"param"});
Could you suggest whether this approach is good for large applciation or there is a better way to pass data from .net winform to webbrowser AngularJS form.
Please note sometime back I have posted question to call Typescript function and I got answer as well. However, that was to initialize class of Angular from webbrowser control which is not feasible for complex Angular app.
Hence I have thought this approach to call simple JS function of Angular page and that would trigger complex angular flow.
Call TypeScript function from C# webbrowser control
Thanks in advance for your help.
Thanks,
Manoj
That will get you one-way communication and it is totally fine. For 2-way communication, you should take a look at ObjectForScripting.
With that, you can have your JavaScript directly call C# methods to send data back via window.external
Here is a blatant paste from the MSDN.
webBrowser1.AllowWebBrowserDrop = false;
webBrowser1.IsWebBrowserContextMenuEnabled = false;
webBrowser1.WebBrowserShortcutsEnabled = false;
webBrowser1.ObjectForScripting = this;
// Uncomment the following line when you are finished debugging.
//webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.DocumentText =
"<html><head><script>" +
"function test(message) { alert(message); }" +
"</script></head><body><button " +
"onclick=\"window.external.Test('called from script code')\">" +
"call client code from script code</button>" +
"</body></html>";
EDIT: Updating answer to show other ways of running script on the page.
private void WebBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser wb = sender as WebBrowser;
HtmlElement he = webBrowser1.Document.CreateElement("script");
he.InnerHtml = "alert('popup');";
wb.Document.Body.InsertAdjacentElement(HtmlElementInsertionOrientation.AfterEnd, he);
}
On a side note, be careful about memory leaks. The web browser control has the same rendering and JavaScript engine as Internet Explorer, however unlike IE, when it leaks memory it can't restart a tab process. It lives in your process space. I am saying be careful as Angular JS and IE have historically not played nicely together. Make sure to handles your scopes properly and such.

Episerver nested master page from EPiServerUI.master

I want to have my custom master page for all GuiPlugIn reports. As we know, by default GuiPlugIn referes to EPiServerUI.master page which is part of installation. I want to create a nested master page for my GuiPlugIn instead of default.
Please share your thoughts.
Thanks,
Kris
I think the reason that you cannot change master page for a plugin is for visual consistency. You could try to change the master page through code like this (assuming your plug in is a user control:
protected override void OnInit(EventArgs e)
{
this.Page.MasterPageFile = "~/NewMaster.master";
}
Maybe there is a better way to do what you want, if you provide more detail?
You can always access the Page object if you want to inject custom css or scripts to use with your plugin like this:
HtmlGenericControl js = new HtmlGenericControl("script");
js.Attributes["type"] = "text/javascript";
js.Attributes["src"] = "mylibrary.js";
this.Page.Header.Controls.Add(js);

Custom Button or Link to a Visualforce page with a custom controller

I have a Visualforce page using a custom controller that is used to edit multiple records under an opportunity.
I'd like to create a custom button or link from Opportunities to this Visualforce page.
Currently the link looks like:
/apex/ExamplePage?oppId={!Opportunity.Id}
This works fine in the development sandbox, but when it is deployed as part of a managed package the link breaks as the page reference doesn't have the namespace prefix.
I found the post Managed Package Redirecting Problem on the Force.com Discussion Boards which implied it should be possible to use $Page to reference the Visualforce page in the URL. E.g.
{!URLFOR($Page.MyExamplePage,'',[objectId = campaign.id])}
But doing so only gives me the syntax error:
Error: Field $Page.MyExamplePage does not exist. Check spelling.
There is another part to the post that suggests using an Apex class and Execute Javascript to work around it. But it appears to me that this has just moved the namespace issue into the Javascript.
How can I safely reference the Visualforce page to work both inside and outside a managed package?
Best to do this from an Apex PageReference return value. Something like this will work:
public PageReference returnPage()
{
return Page.MyExamplePage;
}
Then call this from Visualforce:
<apex:commandButton value="Go To New Page" action="{!returnPage}"/>
The Apex Page call will handle the translation for you.
[EDIT]
Create a bare bones Visualforce page like this:
<apex:page standardController="Opportunity" extensions="TheController" action="{!returnPage}"/>
Add the above returnPage() method to a new TheController (or whatever) class. It doesn't even need a constructor. The class can look like this:
public TheController
{
public PageReference returnPage()
{
return Page.MyExamplePage;
}
}
Then from the Opportunity settings page go to Buttons and Links and create a new custom Visualforce button selecting the new page you just created.
That should do it.
It occurred to me that one less than ideal option would be to create two custom buttons in each case. One with the managed package namespace and one without.
When building the package the correct custom button could be selected.
One issue with this approach is the need to maintain two custom buttons.
It seems the answer is simply /apex/package__Page as provided here by #zachelrath. I can confirm this works in managed packages in production orgs as well as in development.
The post on the developer boards that you've linked to shows the following javascript being used for the button:
{!REQUIRESCRIPT("/soap/ajax/15.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/15.0/apex.js")}
var pageUrl = sforce.apex.execute("mynamespace.PageUrl", "getPageUrl", {objectId:"{!Campaign.Id}"});
window.location.href = pageUrl;
i.e. they're using javascript to call a webservice method in the class they've defined in order to get the page reference. Doing this would allow you to get the URL of the page in apex, where the managed package won't play an impacting part.
That said, the first parameter is the fully-qualified class name, so you could probably check the return value for an error (I don't know the error return value, so I'm assuming it's null here):
// try the namespace first
var pageUrl = sforce.apex.execute("mynamespace.myClass", "getPageUrl", {objectId:"{!Campaign.Id}"});
if (pageUrl == null)
{
pageUrl = sforce.apex.execute("myClass", "getPageUrl", {objectId:"{!Campaign.Id}"});
}
window.location.href = pageUrl;
Obviously you need to check what happens when sforce.apex.execute() fails, and you'll likely want some more error handling.

.Net Winfoms WebBrowser control - When content loaded from Stream, how to permit submit operation?

I am using the WebBrowser control on a Windows Forms application in Visual Studio 2010 - targeting .Net framework 3.5.
I have loaded the contents of the WebBrowser control via setting the DocumentStream property. The stream content is from the response to an Http (POST) request to a third party web page which is called in code using the HttpWebRequest object. We need to use the POST request verb type. The form is populated with data based on the request parameters.
Within the Windows application, the user needs to fill out a few additional text fields and then submit. Having been loaded via the stream, the page has no knowledge of full url of the original page. Therefore the submit fails (displays the name of the page in the WebBrowser control).
Is there any way to give the control the full path to the document such that the Submit operation will have the correct context? Setting WebBrowser.Url property does not work as this simply results in navigation to the page without the data displayed as it is not passed any parameters.
Below is the code (so far):
//Class to call website to make http post
var webBridge = new WebCallHandler();
//Make the request. Response returned as string
var result = webBridge.MakeHttpRequest();
//Get string as stream
var byteArray = Encoding.ASCII.GetBytes(result);
var stream = new MemoryStream(byteArray) { Position = 0 };
//webBrowser1.Url = new Uri(URL);
webBrowser1.DocumentStream = stream;
//Need to set the context of the page like "http://example.com/somepage.aspx"
var dom = webBrowser1.Document.DomDocument;
If you have filled the result variable (which seems to be a string), you should rather easily be able to modify the contents.
Using e.g. Regular Expressions or HTML Agility Pack to search for the
<form ... action="relative-url" ...
and replace it with
<form ... action="http://somedomain.com/relative-url" ...
then pass it to the DocumentStream property of your browser.
I searched the string being returned from the HttpWebrequest for the form element action attribute. This only showed the name of the page received from the httpWebRequest. I modified it in code to be the full url.
Thanks for your comment Kevin. This was the prompt that helped.

In Silverlight, how to launch / navigate to another page?

In Silverlight how can I launch / navigate to another page?
System.Windows.Browser.HtmlPage.Window.Navigate(
new Uri( "http://www.google.com" ),
"_blank"
);
You can leave out the target ("_blank") if you just want to navigate within your current browser window.
To navigate to another page in from another page.
Frame frame =this.parent as Frame;
frame.navigate(new Uri("/Views/Details.xaml"),Uri.Relative);
Note, you must have a frame already in the MainPage.xaml.
So other pages are just calling the frame in the parent
Assume You are editing code behind file of Page class
this.NavigationService.Navigate(new Uri("/OtherPage.xaml", UriKind.Relative));
To avoid issues with popups being blocked when you use _blank, ensure you call Navigate from the click event of a HyperlinkButton control, as described here:
http://www.tjsblog.net/2010/10/20/opening-a-new-chrome-page-in-silverlight/
You all can also try this
this.content=new (place the page name which You want to navigate);
but this code only works while navigate page having in same folder else You have to write like in given below manner
this.Content = new Views.(place the page name which You want to navigate);
here in place of Views write the folder name where the page having...
Hope it's also helpful for you all.

Resources