Using AutoLayout and UIScrollView programmatically - c#

I am trying to make a copy of the answer from this question here: Xamarin iOS: Auto Layout within scroll view? but without using the interface builder. The code is simple, I have _scrollView which has background color green. Then I have _contentView which has background color red. I add _contentView to _scrollView and I add _scrollView to View in the ViewController. When I run this, all I get is Green controller, the red one never appears inside of it.
public async override void ViewDidLoad()
{
base.ViewDidLoad();
//ui
View.BackgroundColor = UIColor.White;
//views
_scrollView = new UIScrollView();
_scrollView.BackgroundColor = UIColor.Green;
_scrollView.TranslatesAutoresizingMaskIntoConstraints = false;
_contentView = new UIView();
_contentView.TranslatesAutoresizingMaskIntoConstraints = false;
_contentView.BackgroundColor = UIColor.Red;
var label = new UILabel();
label.TranslatesAutoresizingMaskIntoConstraints = false;
label.Text = "Text";
_contentView.AddSubview(label);
_scrollView.AddSubview(_contentView);
View.AddSubviews(new UIView[] { _scrollView });
_scrollView.TopAnchor.ConstraintEqualTo(View.TopAnchor, 100).Active = true;
_scrollView.LeftAnchor.ConstraintEqualTo(View.LeftAnchor, 20).Active = true;
_scrollView.RightAnchor.ConstraintEqualTo(View.RightAnchor, -20).Active = true;
_scrollView.BottomAnchor.ConstraintEqualTo(View.BottomAnchor, -100).Active = true;
_contentView.TopAnchor.ConstraintEqualTo(_contentView.Superview.TopAnchor).Active = true;
_contentView.RightAnchor.ConstraintEqualTo(_contentView.Superview.RightAnchor).Active = true;
_contentView.LeftAnchor.ConstraintEqualTo(_contentView.Superview.LeftAnchor).Active = true;
_contentView.BottomAnchor.ConstraintEqualTo(_contentView.Superview.BottomAnchor).Active = true;
_contentView.WidthAnchor.ConstraintEqualTo(_contentView.Superview.WidthAnchor).Active = true;
label.TopAnchor.ConstraintEqualTo(label.Superview.TopAnchor, 100).Active = true;
label.BottomAnchor.ConstraintEqualTo(label.Superview.BottomAnchor, 100).Active = true;
label.LeftAnchor.ConstraintEqualTo(label.Superview.LeftAnchor, 100).Active = true;
label.RightAnchor.ConstraintEqualTo(label.Superview.RightAnchor, 100).Active = true;
}

You need to constraint the width of the _contentView to the View instead of _scrollView. Change this constraint:
_contentView.WidthAnchor.ConstraintEqualTo(_contentView.Superview.WidthAnchor).Active = true;
For this one:
_contentView.WidthAnchor.ConstraintEqualTo(View.WidthAnchor).Active = true;
Other than that I highly recommend you to use a helper library for using AutoLayout programatically. Check out FluentLayout

The only way I was able to make it work was by adding BottomAnchor to the last UIView inside ContentView. I created a GitHub repo with demo code here

Related

C# enabling or disabling control

So, I'm losing my mind over a bug in my software.
I have this code, I use it 2 times in my software, the other function similar (with a different name) works normally. But this one is inverted...
I mean by that : Instead of enabling the controls when the groupbox.text contains "INC", it disable them.
Any idea on what is going on?
`private void Enable_disableSTM()
{
if (STM_groupBox.Text.Contains("INC"))
{
STM_radioButton_appel.Enabled = true;
STM_radioButton_autre.Enabled = true;
STM_radioButton_resolution.Enabled = true;
STM_Textbox_SR.Enabled = true;
STM_textBox_remarque.Enabled = true;
STM_Dropdown_Sendto.Enabled = true;
STM_pictureBox_Boutonenvoyer.Enabled = true;
}
else
{
STM_radioButton_appel.Enabled = false;
STM_radioButton_autre.Enabled = false;
STM_radioButton_resolution.Enabled = false;
STM_Textbox_SR.Enabled = false;
STM_textBox_remarque.Enabled = false;
STM_Dropdown_Sendto.Enabled = false;
STM_pictureBox_Boutonenvoyer.Enabled = false;
}
} `
Edit :
Like I said, in my software I have this other function that is working fine. I tried also to change my IF to STM_Textbox_reademail.Text != "" and it's still not working correctly. It's inverted. Enabling when it should not and Disabling too.
`if (SQ_TextBox_reademail.Text != "")
{
SQ_radioButton_appel.Enabled = true;
SQ_radioButton_autre.Enabled = true;
SQ_radioButton_resolution.Enabled = true;
SQ_Textbox_SR.Enabled = true;
SQ_textBox_remarque.Enabled = true;
SQ_Dropdown_Sendto.Enabled = true;
SQ_pictureBox_Boutonenvoyer.Enabled = true;
}
else
{
SQ_radioButton_appel.Enabled = false;
SQ_radioButton_autre.Enabled = false;
SQ_radioButton_resolution.Enabled = false;
SQ_Textbox_SR.Enabled = false;
SQ_textBox_remarque.Enabled = false;
SQ_Dropdown_Sendto.Enabled = false;
SQ_pictureBox_Boutonenvoyer.Enabled = false;
} `
Edit 2 : Okay... I figured out something that works. I'm calling my function at a different place now and it's working. Still does not make sense why I can call the other one at the same place and it works but this one doesn't... but hey... now it works! thanks all!
Your problem is that you are checking if a string contains the "INC" word in a case sensitive way the solution is changing the if statement to check in the string the inc word ignoring the case :
private void Enable_disableSTM()
{
if (STM_groupBox.Text.IndexOf("INC", StringComparison.OrdinalIgnoreCase) >= 0;)
{
STM_radioButton_appel.Enabled = true;
STM_radioButton_autre.Enabled = true;
STM_radioButton_resolution.Enabled = true;
STM_Textbox_SR.Enabled = true;
STM_textBox_remarque.Enabled = true;
STM_Dropdown_Sendto.Enabled = true;
STM_pictureBox_Boutonenvoyer.Enabled = true;
}
else
{
STM_radioButton_appel.Enabled = false;
STM_radioButton_autre.Enabled = false;
STM_radioButton_resolution.Enabled = false;
STM_Textbox_SR.Enabled = false;
STM_textBox_remarque.Enabled = false;
STM_Dropdown_Sendto.Enabled = false;
STM_pictureBox_Boutonenvoyer.Enabled = false;
}
}
Okay... I figured out something that works. I'm calling my function at a different place now and it's working. Still does not make sense why I can call the other one at the same place and it works but this one doesn't... but hey... now it works! thanks all!

How do I change my icon of the form on the click of a button?

I am using a form skin and instead of removing everything including the form skin I have been trying to make the form change the icon itself on startup. I have bitmap versions of my logo and when i try to reference those i get the error: Cannot implicitly convert type 'System.Drawing.Bitmap' to 'System.Drawing.Icon'
I have tried adding an icon to my resources and trying to reference it, however, only my bitmaps are able to be referenced.
public Main2()
{
InitializeComponent();
Main2 f1 = new Main2();
f1.Text = "Chaos V2.0.1c";
f1.Icon = Properties.Resources.Logo; //problematic code (line 44)
//My Tab Controls
HomeTabControl.Visible = true;
FullLuaTabControl.Visible = false;
CommandTabControl.Visible = false;
VIPServerTabControl.Visible = false;
JailbreakTabControl.Visible = false;
PhantomForcesTabControl.Visible = false;
MM2TabControl.Visible = false;
RoCitizensTabControl.Visible = false;
BoogaBoogaTabControl.Visible = false;
PrisonLifeTabControl.Visible = false;
BuildABoatTabControl.Visible = false;
LumberTycoon2TabControl.Visible = false;
MeepCityTabControl.Visible = false;
VehicleSimulatorTabControl.Visible = false;
SuperPowerTrainingTabControl.Visible = false;
BeeSwarmSimulatorTabControl.Visible = false;
WeightLiftingTabControl.Visible = false;
MiningSimulatorTabControl.Visible = false;
BlobSimulatorTabControl.Visible = false;
IceCreamSimulatorTabControl.Visible = false;
PetSimulatorTabControl.Visible = false;
StrucidTabControl.Visible = false;
CounterBloxTabControl.Visible = false;
ApocalypseTabControl.Visible = false;
FullLuaScriptsTabControl.Visible = false;
SettingsTabControl.Visible = false;
}
I get the error: 'Resources' does not contain a definition for 'Logo' (Line 44)
Check if your Icon in resources is really called Logo.
You could change Bitmap to Icon, but I don't know if you will be satisfied with result.
public Icon ToIcon(Bitmap bmp)
{
IntPtr hicon = bmp.GetHicon();
Icon icon = Icon.FromHandle(hicon);
DestroyIcon(hicon); // prevent memory leak.
return icon;
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
extern static bool DestroyIcon(IntPtr handle);

How to use a dynamic forms names in a class (C#.Net)

I had long time developing windows applications using VB.NET and one of thing that I was always use is:
Class (Test) and inside it there are some subs like the following:
VB.NET Code
Friend Sub FLoadKeyLock(ByVal FRMNAME As Object)
FRMNAME.Button3.Enabled = true;
FRMNAME.Button4.Enabled = true;
FRMNAME.Button1.Enabled = false;
FRMNAME.Button2.Enabled = false;
FRMNAME.Button5.Enabled = false;
FRMNAME.Button6.Enabled = false;
FRMNAME.Button7.Enabled = false;
FRMNAME.Button8.Enabled = false;
FRMNAME.Button9.Enabled = false;
FRMNAME.Button10.Enabled = false;
End Sub
Where FRMNAME refers to forms name that I send from different forms and I use (me) keyword to send current form name to apply that sub.
That was totally works fine in VB.NET but I cannot use it the same way in C#.NET.
C#.NET Code
internal void FLoadKeyLock (Form FRMNAME)
{
FRMNAME.Button3.Enabled = true;
FRMNAME.Button4.Enabled = true;
FRMNAME.Button1.Enabled = false;
FRMNAME.Button2.Enabled = false;
FRMNAME.Button5.Enabled = false;
FRMNAME.Button6.Enabled = false;
FRMNAME.Button7.Enabled = false;
FRMNAME.Button8.Enabled = false;
FRMNAME.Button9.Enabled = false;
FRMNAME.Button10.Enabled = false;
}
The Error is: System.Windows.Forms.Form' does not contain a definition for 'Button3' and no extension method 'Button3' accepting a first argument of type
And same error for all used buttons.
So I have two questions:
1 - How to use such a function in C#.NET?
2 - Why it's not working the same way as VB.NET?
As #Maarten said.
dynamic has solved my problem
internal void FLoadKeyLock (dynamic FRMNAME)
{
FRMNAME.Button3.Enabled = true;
FRMNAME.Button4.Enabled = true;
FRMNAME.Button1.Enabled = false;
FRMNAME.Button2.Enabled = false;
FRMNAME.Button5.Enabled = false;
FRMNAME.Button6.Enabled = false;
FRMNAME.Button7.Enabled = false;
FRMNAME.Button8.Enabled = false;
FRMNAME.Button9.Enabled = false;
FRMNAME.Button10.Enabled = false;
}

How can I implement an Activity Indicator for a Carousel in Xamarin.Forms?

I'm trying to incorporate an ActivityIndicator before launching a Carousel page; so that when the Carousel page is heavy on content; the user will know that the app is still loading / hasn't hung.
To figure this out, I'm working from this sample which uses a Carousel page:
https://github.com/chrisriesgo/xamarin-forms-carouselview
My main approach was trying to modify the button to create an ActivityIndicator within the button command:
Original Sample from "SwitcherPage.cs"
var none = new Button {
HorizontalOptions = LayoutOptions.Center,
Text = "No pager indicator",
Command = new Command((obj) => Navigation.PushAsync(new HomePage(CarouselLayout.IndicatorStyleEnum.None)))
};
My Attempt
var indicator = new ActivityIndicator ();
var none = new Button {
HorizontalOptions = LayoutOptions.Center,
Text = "No pager indicator",
Command = new Command(
indicator.IsRunning,
indicator.IsVisible,
Task.Delay(2500), // to simulate a long loading time
(obj) => Navigation.PushAsync(new HomePage(CarouselLayout.IndicatorStyleEnum.None)))
};
What I know so far
Intellisense is already telling me that's wrong; I can't define it in the command that way. But hopefully that illustrates what I'm trying to do; essentially trigger an activity indicator while waiting for the page to load.
I've looked for general resources on the ActivityIndicator, but the problem is I haven't found any that I can figure out how to convert that to something that will activate after the button is pressed while waiting for the Carousel page to load.
https://developer.xamarin.com/api/type/Xamarin.Forms.ActivityIndicator/
http://forums.xamarin.com/discussion/34502/xamarin-forms-activity-indicator
Xamarin.Forms - How to overlay an ActivityIndicator in the middle of a StackLayout programmatically
I am not sure if I understood your question correctly, but:
var indicator = new ActivityIndicator ();
var none = new Button {
HorizontalOptions = LayoutOptions.Center,
Text = "No pager indicator",
Command = new Command(async() => {
indicator.IsRunning = true;
indicator.IsVisible = true;
await Task.Delay(2500);
indicator.IsRunning = false;
indicator.IsVisible = false;
Navigation.PushAsync(new HomePage(CarouselLayout.IndicatorStyleEnum.None));
})
};
But It would be a lot cleaner to set appropriate bindings to you ViewModel properties and do things from there.
Another Solution, Using Device.StartTimer
var indicator = new ActivityIndicator ();
var none = new Button {
HorizontalOptions = LayoutOptions.Center,
Text = "No pager indicator",
Command = new Command(() => {
indicator.IsRunning = true;
indicator.IsVisible = true;
Device.StartTimer(Timespan.FromSeconds(2.5),()=>
{
indicator.IsRunning = false;
indicator.IsVisible = false;
Navigation.PushAsync(new HomePage(CarouselLayout.IndicatorStyleEnum.None));
return false;
});
});
};

Geckofx closing and reopening in c#

What is needed : I need it to close and reopen the browser, I've tried a couple of things. Winform, c#.
What i've tried :
using (Viewer = new GeckoWebBrowser())
{
Viewer.Dock = DockStyle.Bottom;
Viewer.Size = new Size(217, 257);
Viewer.Location = new Point(0, 90);
Viewer.Enabled = true;
Viewer.Visible = true;
Xpcom.Initialize("xulrunner");
GeckoPreferences.User["browser.xul.error_pages.enabled"] = false;
GeckoPreferences.User["security.enable_ssl2"] = true;
GeckoPreferences.User["security.default_personal_cert"] = "Ask Never";
GeckoPreferences.User["security.warn_entering_weak"] = true;
GeckoPreferences.User["security.warn_viewing_mixed"] = true;
GeckoPreferences.User["dom.disable_open_during_load"] = true;
GeckoPreferences.User["dom.allow_scripts_to_close_windows"] = true;
GeckoPreferences.User["dom.popup_maximum"] = 0;
GeckoPreferences.User["dom.max_script_run_time"] = 0;
GeckoPreferences.User["extensions.blocklist.enabled"] = false;
Viewer.BringToFront();
Viewer.Navigate("http://google.com");
Viewer.Dispose();
}
What is the problem?
It isn't showing up on the winform window.
Also i tried it with a windows control in the winform and using Viewer.Dispose(); destroyed the control completely and i wasn't able to access it any more.

Categories

Resources