Display a WebPage using System.Windows.Controls.WebBrowser(LightSwitch, VS2012) - c#

I have followed;
http://dotnettim.wordpress.com/2011/04/17/lightswitchdisplaying-web-pages-html-on-a-screen/
But instead of a webpage im left with;
Im using VS2012 and the example is in VS2010. The diffrence in code that i have encounterd is the following;
Original;
private void WebPage_Activated()
Mine;
partial void WebPage_Activated()
I have checked the debugger and the event fires smoothly, is there any major changes made in beteween the versions or is there anything else that i have overlooked.
I include all code an a picture of where i have put the custom control (im abit uncertain about that part);
public partial class WebPage
{
partial void WebPage_Activated()
{
// Write your code here.
this.FindControl("prpWebPage").ControlAvailable += webControlAvailable;
}
private void webControlAvailable(object sender, ControlAvailableEventArgs e)
{
((System.Windows.Controls.WebBrowser)e.Control).Navigate(new Uri("http://news.bbc.co.uk"));
}
}

I solved it, the size is set to 0 initally. So;
fixed it :)

Related

Cannot explicitly call operator or accessor and static

I have two stupid questions :
The first one :
I have already asked this and I'm truly sorry to ask it again (even more so because there are a lot of posts about it), but now I really don't understand why that happens even if I read every other post, here is my code :
public class PageTitre {
...
public void situation(string s) {
onglet.get_Range("C11").Value = "(" + s + ")";
}
}
public class PPE_Process {
public static PageTitre pageTitre;
public static void MainProcess() {
...
pageTitre = new PageTitre();
...
}
}
public partial class PPE_Ribbon {
private void SituationEditBox_TextChanged(object sender, RibbonControlEventArgs e)
{
PPE_Process.pageTitre.situation(SituationEditBox.Text);
}
}
I have tried some other things, like putting situation as a variable of PageTitre and having a get and a set, and
private void SituationEditBox_TextChanged(object sender, RibbonControlEventArgs e)
{
PPE_Process.pageTitre.set_situation(SituationEditBox.Text);
}
Or
private void SituationEditBox_TextChanged(object sender, RibbonControlEventArgs e)
{
PPE_Process.pageTitre.situation = SituationEditBox.Text;
}
But nothing worked, with the same error: cannot explicitly call operator or accessor.
I guess there is a problem with the static? If that is the case, here is my second question:
Second one :
I read a lot of documentation about it but I really can't understand what is the use of "static"... Is it just so that we can't change the value outside of the class, or something like this? Then, would it really change something if I take off every static there is in my code?
Again, I am sorry that you have to answer this question again and again, but I understand a lot better if that is directly related to my code, and not someone answering someone else about some other random code, which has a different problem than mine. :/
Edit to add more information:
There shouldn't be any problem with .Value or .get_Range, as it works on other parts of the code, but
For .Value, here is the information given by Visual Studio:
void Range.set_Value([object RangeValueDataType = System.Type.Missing], [object value = System.Type.Missing])
For .get_Range: Excel.Range_Worksheet.get_Range(object Cell1, [object Cell2 = System.Type.Missing])
For static, I still don't really understand all these "instances" things, but I will try to look more, and add another question in this forum if I still don't understand after this.
To your first question: I don't see any problem with your call of the method situation, maybe the problem is the statement inside the method (get_Range("C11"))?
To your second question: When you make a variable/property/method static it means that it is independent from any instance (object) of the class. Otherwise you couldn't access the property PPE_Process.pageTitre without an instance of PPE_Process.
Alrighty, I changed nothing in my code but now it works with
private void SituationEditBox_TextChanged(object sender, RibbonControlEventArgs e)
{
PPE_Process.pageTitre.situation = SituationEditBox.Text;
}
So... I don't know, it was maybe a problem with the build of my solution, but that's pretty fortunate :)

c# Double Agent command event handler

here I'm talking about Double Agent (a windows7/8 version of MS Agent, the same as office 2007 I suppose).
I'm sorry I'm talking about a full product but i'm really being mad to catch an event( the bundled sample does not help for this)...
In the sample i have a similar handle:
public MsaWithDa()
{
InitializeComponent();
mDaControl = new DoubleAgent.Control.Control ();
mDaControl.Show += new DoubleAgent.Control.ShowEventHandler (mDaControl_Show);
}
and this:
private void mDaControl_Show(string CharacterID, DoubleAgent.Control.VisibilityCauseType Cause)
{
SetDaControlButtons();
}
Now I need to handle a different event (when the user select a command from the menu of the Agent).. and I have this
private void mainAgent_Command(object sender, AgentObjects.IAgentCtlCommand e)
{
mDaControlChar.Play("Wave");
mDaControlChar.Speak("Hello!");
}
It's based on the user manual of the product:
Double Agent sends this event when your application is input-active
and the user chooses a command from the character's pop-up menu, or by
spoken input.
public event CtlCommandEventHandler CtlCommand
I added this to the main form:
mDaControl.Command += new DoubleAgent.Control.Command(mDaControl_Command);
but something is missing and I have to pass the two values to be able to test.
Sorry, I understand this is a stupid question and sure super-basic but this is the first time I need to use Event Handlers in c#
Hope someone should help, thanks a lot
EDIT:
Based on this article: Understanding events and event handlers in C#
I now coded this:
public delegate void MyEventHandler(object sender, AgentObjects.IAgentCtlCommand e);
public event MyEventHandler AgentObjects;
and this:
private void InitializeAgent()
{
mDaControl.Command += new MyEventHandler(HandleSomethingHappened);
}
private void HandleSomethingHappened(object sender, AgentObjects.IAgentCtlCommand e)
{
mDaControlChar.Play("Wave");
mDaControlChar.Speak("Hello!");
}
BUT i have an error here:
new MyEventHandler(HandleSomethingHappened)
Error 1 Cannot implicitly convert type 'XCopyPro.Main.MyEventHandler'
to
'DoubleAgent.Control.CommandEventHandler' C:\Users\Shawn\Documents\Visual
Studio 2013\Projects\XCopyPro\XCopyPro\FormMain.cs 159 37 XCopyPro
You should be able to do a new DoubleAgent.Control.CommandEventHandler instead of a new MyEventHandler. As long as your method has the same signature as the DoubleAgent event handler it should work.
Sorry, i'm a newbie in C#! You should not help me without docs...
I solved myself following the manual...
The code is pretty simple, somewhere this:
mDaControl.Command += new DoubleAgent.Control.CommandEventHandler(mDaControl_Commands);
And this:
private void mDaControl_Commands(DoubleAgent.Control.UserInput e)
{
mDaControlChar.Play("Wave");
mDaControlChar.Speak("Hello!");
}

CefSharp WPF web browser is not displayed Or rendered

Am new to CefSharp
I have created a class library project and referenced the CefSharp library to render the Web browser, However I am facing some issues showing the web Browser. Please find the exact code
WebBrowser_test1:
public partial class ChildWidget : Window
{
public CefSharp.Wpf.ChromiumWebBrowser webView;
public Widget()
{
InitializeComponent();
CefSharp.CefSettings settings = new CefSharp.CefSettings();
settings.PackLoadingDisabled = true;
if (CefSharp.Cef.Initialize(settings))
{
webView = new CefSharp.Wpf.ChromiumWebBrowser();
main_grid.Children.Add(webView);
webView.Address = "http://www.google.co.uk";
}
}
}
and I am referencing this library (dll) in another project
public MainWindow()
{
InitializeComponent();
Button newbutton = new Button();
newbutton.Width = 50;
main_grid.Children.Add(newbutton);
newbutton.Click += ButtonClick;
}
private void ButtonClick(object sender, RoutedEventArgs e)
{
try
{
Webbrowser_test1.ChildWidget childWidget = new Widget();
childWidget.Show();
}
catch (Exception)
{
throw;
}
}
Now on the Button click I will open the (WebBrowser_test1) child widget in which I will show the web browser .. when the window opens it is showing blank.
Please let me know if I missing anything
Subscribe to IsBrowserInitializedChanged after creating a ChromiumWebBrowser. Then once the browser is initialized you can call Load and your control will be displayed.
...
_browser = new ChromiumWebBrowser();
mainGrid.Children.Add(_browser);
_browser.IsBrowserInitializedChanged += OnIsBrowserInitializedChanged;
...
void OnIsBrowserInitializedChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (_browser.IsBrowserInitialized)
{
_browser.Load("https://www.google.com/");
}
}
I can think of the first three potential issues. But it's hard to tell what the real issue is from your code alone as it strays off a bit from the official examples
Move Cef.Initialize() to your MainWindow constructor. It should only be called once to launch the CefSharp.BrowserSubprocess.exe renderer process.
See my answer to CefSharp 3 always failing Cef.Initialize() for a few things to check regarding binaries and their placement. Really, the recommended approach is to start having the WPF example in the CefSharp.MinimalExample repo running first and then adjust to your use case from there.
I'm not sure a ChromiumWebBrowser() without explicitly setting a width and height works. A 0x0 window might not receive any rendered content. I haven't tried with recent code.
Have you tried replacing
webView.Address = "http://www.google.co.uk";
with
webView.Load("http://www.google.co.uk");
Like jornh mentions, you may have to explicitly set the height and width of the ChromiumWebBrowser. If you don't know the exact size, setting HorizontalAlignment and VerticalAlignment to Stretch (to fill the parent container) will probably also work.
Have you checked if the Cef.Initialize() actually returns true? You could be missing some files, and CefSharp doesn't always give clear error messages when this is the case.

Basic use of user controls, closing a user control and opening another, user control parameters

ladies and gentlemen, once again I unfortunately am going to bother you with newbie stuff. I have searched for this information for hours, so if there’s a thread with what I want on it, it’s buried deeper than I could find.
This was my first question here, and Mark Hall was kind enough to set me straight. Since then, I have created a new project and recreated my first three screens as user controls – a container/login, a choice screen, and a main screen (currently empty). If a user has more than one collection, the choice screen pops up and allows them to choose a collection.
I did run into a snag with parameters, but I’m solving that by overloading the form declaration (solution found here) – yes, I know it’s much better to send parameters through calls, but I’d hate to have to create a call for each parameter (do I?) and… OK, OK, I’m better at this than {get, set}. Man, I hate being a newbie.
Anyways, I’m having trouble with the choice form – I can’t seem to call it, close it, then go to the main form. I have no problem (if there’s only one collection) in going straight to the main form, it’s that darn choice form. Yes, I know I could include a choice datagridview, but a few of our end users aren’t the sharpest bulb in the tool shed, and need hand-holding. Anyways, here’s the code.
CONTAINER/LOGIN SCREEN
namespace DeleteThis
{
public partial class ContainerForm : Form
{
Main Main = new Main();
LoginCollectionChoice LoginChoice = new LoginCollectionChoice();
DataTable dtPermissions = new DataTable();
public ContainerForm()
{
InitializeComponent();
Main.ExitEvent += new Main.ExitEventHandler(Main_ExitEvent);
LoginChoice.ExitEvent += new
LoginCollectionChoice.ExitEventHandler(LoginChoice_ExitEvent);
}
void LoginChoice_ExitEvent(object sender, EventArgs e)
{
pnlcontainer.Controls.Remove(LoginChoice);
}
void Main_ExitEvent(object sender, EventArgs e)
{
pnlcontainer.Controls.Remove(Main);
}
private void btnLogin_Click(object sender, EventArgs e)
{
LoginProcedure();
}
private void LoginProcedure()
{
DataTable dtPermissions = AdminClass.GetCollectionsForUser(int.Parse(txtbxUserName.Text));
if (dtPermissions.Rows.Count == 1)
{
//Valid user, one collection. Head right in.
pnlcontainer.Controls.Add(Main);
Main.BringToFront();
}
else
{
//More than one collection found. Giving user choice
LoginCollectionChoice LoginChoice = new LoginCollectionChoice(dtPermissions);
pnlcontainer.Controls.Add(LoginChoice);
LoginChoice.BringToFront();
}
}
private void btnExitProgram_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
I hope I didn't kill anything in the snipping. And now the choice screen…
public partial class LoginCollectionChoice : UserControl
{
public delegate void ExitEventHandler(object sender, EventArgs e);
public event ExitEventHandler ExitEvent;
private static DataTable dtPermit;
DataTable dtPermissions = new DataTable();
public LoginCollectionChoice()
{
}
public LoginCollectionChoice(DataTable dtPermissions)
{
InitializeComponent();
GrdCollection.DataSource = dtPermissions;
dtPermit = dtPermissions;
}
private void btnChoose_Click(object sender, EventArgs e)
{
//Code for the user to choose a collection
ExitEvent(this, new EventArgs());
}
}
I’ve snipped all non-relevent code, I hope you gentlemen and ladies can help this newbie get on the right path. Please, be gentle, you wouldn’t want to see me cry, would you? :) Oh! And if you know of any great tutorial sites, please email them to me. I'd prefer to spend a week on tutorials than a week on stumbling and asking here. Thank you all very much.
Are you calling logonForm.Show()?
It seems like you'd need to show it that way.
You use BringToFront(), but I think it needs to be shown first.
The first thing that jumps out to me is that you are calling
LoginCollectionChoice LoginChoice = new LoginCollectionChoice(dtPermissions);
which is creating a local variable called LoginChoice, which is not the same as your class variable even though they share the same name.
What you want to do is to not declare a local variable in that method. Change that line to
LoginChoice = new LoginCollectionChoice(dtPermissions);
Having said that, I believe tylerjgarland in that you need to call .Show() first. And the way you are closing the form is certainly odd. I would create a form, showDialog, get the result and then close the form that way.

Refactoring big ball of mud; not sure static is being used properly here. Advice?

I'll admit sometimes the deeper nuances of the keyword static escape me.
Here's what I'm seeing:
public partial class Default : CSSDEIStatusBase
{
private static Default _csWitWeb;
protected void Page_Load(object sender, EventArgs e)
{
//DoStuff
_csWitWeb = this;
//OtherStuff
}
public static void ForceLoadSyncOperation(int? index)
{
Default._csWitWeb.LoadSelectedSyncOperation(index);
}
}
The only references to ForceLoadSyncOperation are:
Default.ForceLoadSyncOperation(index);
or
Default.ForceLoadSyncOperation(null);
Both of these calls originate from:
public partial class DataOriginUserControl : System.Web.UI.UserControl
and are not located inside of static methods.
E.G:
protected void btnCancelSyncOperation_Click(object sender, EventArgs e)
{
this.lblErrorMessage.Text = string.Empty;
this.lblErrorMessage.Visible = false;
int index = _syncOperation.Sequence - 1;
Default.ForceLoadSyncOperation(index);
}
This all seems really quirky to me. Does this smell to anyone else? Not really sure how to untangle it, though, as I can't exactly create an instance of the Default page inside of a user control.
Thoughts? Thanks for reading.
protected void LoadSelectedSyncOperation(int? index)
{
SyncOperationConfiguration[] syncOperations = CSServiceClient.GetInterfaceConfiguration().SyncOperationConfigurations.ToArray();
PopulateSyncOperationsListView(syncOperations);
SyncOperationConfiguration syncOperation = null;
try
{
syncOperation = syncOperations[index.HasValue ? index.Value : 0];
}
catch
{
syncOperation = syncOperations[0];
}
ucDataOrigin.LoadSyncOperationData(syncOperation);
Session["ConfigMenuActiveIndex"] = 1;
menuConfiguration.Items[(int)Session["ConfigMenuActiveIndex"]].Selected = true;
mvwConfiguration.ActiveViewIndex = (int)Session["ConfigMenuActiveIndex"];
}
Presumably, the user control is contained within the Default page and the static member is being used as a shortcut to get the current instance of Default. I would've done it this way:
Default defaultPage = this.Page as Default;
if (defaultPage != null)
{
defaultPage.LoadSelectedSyncOperation(index);
}
Using a static member in this way is not safe. It opens up the door for race conditions. There is the potential risk that the user control is loaded in another page and calls LoadSelectedSyncOperation() on a separate request's instance of Default, thus wreaking all kinds of potential havoc.
I don't know what LoadSelectedSyncOperation does but this code looks weird. Whenever you click btnCancelSyncOperation you end up calling this method on some page, but you never know on which of them. It doesn't make much sense to me.
I would definitely say your concerns are valid. I can't think of any reason that this design would make sense, ever. This would throw a flag for me, too.
Based on your reply to my comment, if the Default.LoadSelectedSyncOperation is not dependent upon the Default page somehow, then I suggest it be refactored into a separate class (not an ASP.NET Page).
Whether it makes sense for the method or new class to be static or not is a separate concern and would be based on the logic contained within the method.

Categories