Adding and Opening an DialogPage (or window) for MacOS (C#) - c#

I'm very new at programming in C# and Xamarin, and now I'm stuck.
I made a menu with some help, now I can do Cut/Copy/Paste which works fine, but now I want to make an Dialog or Window for some User Settings whenever they click on Settings in the menu. (I've made the menu in AppDelegate.cs)
I've look around a lot on Google and Xamarin forums, but al they talk about is to add a new kind of storyboard thingy. Which I guess will work, but I'm not using any storyboard (Just because we want to make everything out of code).
Found on Xamarin:
https://developer.xamarin.com/guides/mac/user-interface/working-with-dialogs/
And then the Preference window (That is what I prefer).
Code I made in AppDelegate.cs (Menu button which has to open an Window or Dialog):
var prefMenuItem = new NSMenuItem("Settings", ",", handler: delegate
{
var prefStyle = NSWindowStyle.Closable | NSWindowStyle.Resizable | NSWindowStyle.Titled;
var rect = new CoreGraphics.CGRect(200, 1000, 1024, 768);
prefWindow = new NSWindow(rect, prefStyle, NSBackingStore.Buffered, false);
prefWindow.Title = "Settings";
prefWindow.TitleVisibility = NSWindowTitleVisibility.Hidden;
});
'Settings' Button in the menu is visible btw.
Probably I forgot something, it won't open a thing.
How can I make this work?

Maybe this will work for you.
var prefMenuItem = new NSMenuItem("Settings", ",", handler: delegate
{
var prefStyle = NSWindowStyle.Closable | NSWindowStyle.Resizable | NSWindowStyle.Titled;
var rect = new CoreGraphics.CGRect(200, 1000, 1024, 768);
prefWindow = new NSWindow(rect, prefStyle, NSBackingStore.Buffered, false);
prefWindow.Title = "Settings";
prefWindow.TitleVisibility = NSWindowTitleVisibility.Hidden;
NSApplication.SharedApplication.RunModalForWindow(prefWindow);
});
I added NSApplication.SharedApplication.RunModalForWindow(prefWindow); in your code. This will open a new window.
Found this on: https://forums.xamarin.com/discussion/22729/xamarin-mac-how-to-set-position-of-modal-window

Related

Creating UI element at runtime

I need to create some buttons at runtime. I tried to find a solution online, but there were only old threads. The only thing I've found is the following code:
ViewGroup layout = (ViewGroup)Resource.Layout.Main;
Button btn = new Button(this);
btn.Text = "text";
btn.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);
layout.AddView(btn);
I don't understand how this should work. There are no compile errors, but the app closes instantly after launching. Can you explain why this happens and how to write this code properly?
Doing
ViewGroup layout = (ViewGroup)Resource.Layout.Main;
Won't give you the currently inflated layout. Resource.Layout.Main is just an int pointing at a resource.
Instead give your currently shown layout and id:
android:id="#+id/root"
Now it does matter what type it is. The default templates use LinearLayout. So finding it would be:
var root = FindViewById<LinearLayout>(Resource.Id.root);
Then you can add your button to that:
var button = new Button(this)
{
Text = "hello",
LayoutParameters = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MatchParent,
ViewGroup.LayoutParams.WrapContent)
};
root.AddView(button);

CEFSharp - Read web responses

I am working on a project that I want to use the Chromium Web Browser and be able to read the data that would normally come though the DevTools "Network" tab. So basically all i really need is the URL and the status code (404, 200, 50x, etc).
I have everything working properly for the ChromiumWebBrowser part because that works perfectly but I cant seem to figure out the details on how to get the network data.
I found this in a github issues section but I dont really understand how to implement it. https://github.com/cefsharp/CefSharp/issues/1379
Any help would be greatly appreciated.
Here is what I have so far...
private ChromiumWebBrowser _wb;
public MainForm()
{
var cefsettings = new CefSettings { CachePath = "cache" };
cefsettings.CachePath = "cache";
if (cefsettings.CefCommandLineArgs.ContainsKey("enable-system-flash"))
{
string flashValue;
cefsettings.CefCommandLineArgs.TryGetValue("enable-system-flash", out flashValue);
if (flashValue != "1")
{
Debug.WriteLine("Flash Might Be Disabled For Chromium Web Browser");
}
}
else
{
cefsettings.CefCommandLineArgs.Add("enable-system-flash", "1");
}
//TODO: Get the latest version version folder
cefsettings.CefCommandLineArgs.Add("ppapi-flash-path","C:\\program Files (x86)\\Google\\Chrome\\Application\\51.0.2704.103\\PepperFlash\\pepflashplayer.dll");
Cef.Initialize(cefsettings);
InitializeComponent();
_wb = new ChromiumWebBrowser("http://youtube.com/")
{
Dock = DockStyle.Fill,
Location = new System.Drawing.Point(0, 22),
MinimumSize = new System.Drawing.Size(20, 20),
Size = new System.Drawing.Size(1280, 900),
TabIndex = 8
};
//Add ChromiumWebBrowser to the Browser Panel
pnlBrowser.Controls.Add(_wb);
}
Here is what I ended up doing...
Implemented a class called "RequestHandler" that implements the IRequestHandler interface. Copied most of the default code for this interfaces methods from the CEFSharp open source project and then just tweaked the "IRequestHandler.OnResourceResponse" portion to my liking.
Then on my main form that uses the web browser, I just used the code below...
//Create ChromiumWebBrowser
_wb = new ChromiumWebBrowser(Urls.HOME)
{
Dock = DockStyle.Fill,
Location = new System.Drawing.Point(0, 22),
MinimumSize = new System.Drawing.Size(20, 20),
Size = new System.Drawing.Size(1280, 900),
TabIndex = 8
};
//Add ChromiumWebBrowser to the Browser Panel and add events
pnlBrowser.Controls.Add(_wb);
var requestHandler = new RequestHandler();
_wb.RequestHandler = requestHandler;
I hope this helps someone!

How to open a new window in browser with focus

I would like to know how to open new window in the browser with focus while keeping the other window open too.
I have a button in my silverlight app that must open a pdf in another window or tab, i want the new window to have focus while keeping the initial page open.
I can get the new window to open but without focus or i can get it to replace the current page , then returning to the app requires login etc.
So i need 3 things...
1)open new window
2)focus new window
3)leave old window open
Uri myUri = new Uri(#"http://......./ViewPDF.aspx");
HtmlPage.Window.Navigate(myUri, "_newWindow");
HtmlPage.Window.Navigate(myUri);
HtmlPage.Window.Eval("window.focs()")
HtmlPage.PopupWindow(myUri, "_blank", null);
None of these meet all 3 requirements.
please help..
Thanking you in advance
HtmlPopupWindowOptions options = new HtmlPopupWindowOptions();
options.Left = 300;
options.Top = 150;
options.Width = 1024;
options.Height = 768;
options.Directories = false;
options.Location = false;
options.Menubar = false;
options.Status = false;
options.Toolbar = false;
Uri myUri = new Uri(#"http://......./ViewPDF.aspx");
HtmlPage.PopupWindow(Uri, "_blank", options);

DevExpress DropDownButton Problems

I'm trying to create a DevEx drop down button. Unfortunately, I'm running into two problems I can't figure out:
1) I can't get the popup menu to skin correctly, i.e. it doesn't skin as "Office 2010 Blue". The code I'm using is shown below:
private void InitializeSendToPricingSheetButton()
{
var barManager = new BarManager();
if (barManager.Controller == null) barManager.Controller = new BarAndDockingController();
barManager.Controller.PaintStyleName = "Skin";
barManager.Controller.LookAndFeel.UseDefaultLookAndFeel = false;
barManager.Controller.LookAndFeel.SkinName = "Office 2010 Blue";
barManager.ItemClick += HandleSendToPricingSheetClick;
barManager.Items.AddRange(new[] { new BarButtonItem(barManager, "Foo"), new BarButtonItem(barManager, "Bar"), new BarButtonItem(barManager, "Baz") });
var popupMenu = new PopupMenu { Manager = barManager };
foreach (var barItem in barManager.Items) popupMenu.ItemLinks.Add((BarItem)barItem);
popupMenu.ItemLinks[1].BeginGroup = true;
dropDownButtonSendToPricingSheet.DropDownControl = popupMenu;
}
2) This button is on a form. If the form loses focus (e.g. I click on Firefox), the pop-up menu still remains on-top. It won't go away until clicked.
Any suggestions would be much appreciated. Thanks for helping me deal with DevEx insanity.
I have solution to your second question.
You should add drop down button event handler as below:
dropDownButton1.LostFocus += new EventHandler(HidePopUp);
Handler method should be as below:
private void HidePopUp(object sender,object e)
{
dropDownButton1.HideDropDown();
}
For your second question, you should assign value to the bar manager property as:
BarManager manager = new BarManager();
manager.Form = this; // refers to current form
Find below link for reference
https://www.devexpress.com/Support/Center/Question/Details/Q274641
It is probably simpler to use DefaultLookAndFeel
Add this comp to your form and set the theme you'd like to use.
There is no need to set the theme for individual components.
defaultLookAndFeel1.LookAndFeel.SetSkinStyle("Office 2010 Blue");

Any hints on how to use notebook control in Mono

Can someone please point me out to a short example of how to add and remove a window to a Notebook control in Mono? I have already searched for any examples but was unable to find anything.
Only thing I found meanwhile: http://docs.go-mono.com/index.aspx?link=T%3AGtk.Notebook
I finally found out how to do this in Mono:
Widget1 win1 = new Widget1();
HBox hbox = new HBox();
hbox.PackStart(new Label("Pane 1") );
Button close = new Button("×"); // Set this up with an image or whatever.
close.Relief = ReliefStyle.None;
close.FocusOnClick = false;
close.Clicked += delegate {
hbox.Destroy();
win1.Destroy();
};
hbox.PackStart(close);
hbox.ShowAll();
nbMain.AppendPage(win1, hbox);
win1.Show();
The main trick is that child panes (Widget1 in my case) should inherit from Widget, not from Window!

Categories

Resources