Children.Add() in Xamarin Forms - c#

im really new to c# and Xamarin Forms but can someone please explain to me why I'm getting an error in the last line of code below ?
Visual Studio is indicating a curly red underline on the word Children on the last line of this code indicating that there is some syntax error but I don't see why that is an error since I'm just accessing a member of the class ....
var content = new ContentPage();
content.Title = "Appuler";
Label CompanyName = new Label();
CompanyName.HorizontalTextAlignment = TextAlignment.Center;
CompanyName.Text = "Test";
Button NextPage = new Button();
NextPage.Text = "Next Page";
NextPage.Font = Font.SystemFontOfSize(NamedSize.Large);
NextPage.BorderWidth = 1;
NextPage.HorizontalOptions = LayoutOptions.Center;
NextPage.VerticalOptions = LayoutOptions.CenterAndExpand;
content.Content = new StackLayout();
content.Content.VerticalOptions = LayoutOptions.CenterAndExpand;
content.Content.Children.Add(CompanyName);

Refactor the last few lines to the following
var layout = new StackLayout();
layout.VerticalOptions = LayoutOptions.CenterAndExpand;
layout.Children.Add(CompanyName);
content.Content = layout;
ContentPage.Content does not have that Children property as it is a View. StackLayout, however does have a Children property that can be accessed.
populate the layout control's accessible properties and then assign it to the Content property of the ContentPage.

Related

embedded image in Xamarin Forms using c# [duplicate]

This question already has answers here:
Xamarin Forms - how to add background image to a content page
(2 answers)
Closed 5 years ago.
Guys Im having a little trouble trying to make an image the background for a certain page. Im trying to do it via the embedded method mentioned here:https://developer.xamarin.com/guides/xamarin-forms/user-interface/images/#Embedded_Images
.For some reason when I run the app, it crashes. I think something went wrong at 'BackgroundImage =' line cause I kinda guessed when I did that. When I comment out that line, the app loads fine, just without the background image. I used build action = content and copy to output directory as settings...
public class LoginPage : ContentPage
{
Entry emailBox = new Entry();
Entry passwordBox = new Entry();
Button createAccount = new Button();
Button forgotPassword = new Button();
StackLayout layout = new StackLayout();
Image embeddedImage = new Image { Source = ImageSource.FromResource("Charity.Properties.hands-426x640.jpg") };
public LoginPage()
{
Title = "Login";
BackgroundColor = Color.LightPink;
BackgroundImage = "Charity.Properties.hands-426x640.jpg";
emailBox.Placeholder = "email";
emailBox.BackgroundColor = Color.LightPink;
emailBox.PlaceholderColor = Color.HotPink;
passwordBox.Placeholder = "password";
passwordBox.BackgroundColor = Color.LightPink;
passwordBox.PlaceholderColor = Color.HotPink;
passwordBox.IsPassword = true;
createAccount.Text = "create an account";
createAccount.TextColor = Color.HotPink;
createAccount.Font = Font.SystemFontOfSize(NamedSize.Medium);
createAccount.BorderWidth = 0;
createAccount.HorizontalOptions = LayoutOptions.Center;
createAccount.VerticalOptions = LayoutOptions.CenterAndExpand;
forgotPassword.Text = "recover password";
forgotPassword.TextColor = Color.HotPink;
forgotPassword.Font = Font.SystemFontOfSize(NamedSize.Medium);
forgotPassword.BorderWidth = 0;
forgotPassword.HorizontalOptions = LayoutOptions.Center;
forgotPassword.VerticalOptions = LayoutOptions.CenterAndExpand;
layout.VerticalOptions = LayoutOptions.End;
layout.BackgroundColor = Color.LightPink;
layout.Children.Add(emailBox);
layout.Children.Add(passwordBox);
layout.Children.Add(createAccount);
layout.Children.Add(forgotPassword);
Content = layout;
}
}
This works, after much trial and error I only needed to add the image to both .Droid(drawable) and .iOS(resources) folders. I had not added it to the iOS folder previously, hence it kept crashing when I ran on an iOS simulator.
Image image = new Image();
public LoginPage()
{
Title = "Login";
BackgroundImage = "hands426x640.jpg";}

Error when using Children.Add in Xamarin Forms

Im having some problems with the Children.Add() function of a Stack Layout. There is an error in the code below which says " There is no argument that corresponds to the required formal parameter 'value' of 'SettersExtentions.Add(IList, Bindable Property, object)' ". The error is a curly red underline under all the .Add words.
public class LoginPage : ContentPage
{
public string email { get; set; }
public string password { get; set; }
Entry emailBox = new Entry();
Entry passwordBox = new Entry();
Button createAccount = new Button();
Button forgotPassword = new Button();
Layout layout = new StackLayout();
public LoginPage()
{
Title = "Login";
emailBox.Placeholder = "email";
passwordBox.Placeholder = "password";
passwordBox.IsPassword = true;
createAccount.Text = "Create Account";
createAccount.Font = Font.SystemFontOfSize(NamedSize.Large);
createAccount.BorderWidth = 1;
createAccount.HorizontalOptions = LayoutOptions.Center;
createAccount.VerticalOptions = LayoutOptions.CenterAndExpand;
forgotPassword.Text = "Forgot Password";
forgotPassword.Font = Font.SystemFontOfSize(NamedSize.Large);
forgotPassword.BorderWidth = 1;
forgotPassword.HorizontalOptions = LayoutOptions.Center;
forgotPassword.VerticalOptions = LayoutOptions.CenterAndExpand;
layout.VerticalOptions = LayoutOptions.CenterAndExpand;
layout.Children.Add(emailBox);
layout.Children.Add(passwordBox);
layout.Children.Add(createAccount);
layout.Children.Add(forgotPassword);
Content = layout;
}
public void Check(string email, string password, string cpassword)
{
}
}
Layout.Children is a readonly IReadOnlyList<Element> so you wont be able to add to it.
However StackLayout.Children is IList<T> which will allow you to add children
change
Layout layout = new StackLayout();
to
StackLayout layout = new StackLayout();
This will allow you to be able to add the child elements to the layout.

DevExpress.XtraEditors.CheckedComboBoxEdit Items are displaying as bold after migration

I have migrated my application to DevExpress 15.2.I am getting my DevExpress.XtraEditors.CheckedComboBoxEdit items are displaying in bold.I have set following properties.Please let me know if I missed any property.I tried with Font property but selected Item is only changing not all the Items.
this.cmbTemplates.Location = new System.Drawing.Point(503, 618);
this.cmbTemplates.Name = "cmbTemplates";
this.cmbTemplates.Properties.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] { new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)});
this.cmbTemplates.Properties.SelectAllItemCaption = "Select All";
this.cmbTemplates.Size = new System.Drawing.Size(259, 20);
this.cmbTemplates.TabIndex = 93;
Try this :
cmbTemplates.Properties.AppearanceDropDown.Font = new
Font(cmbTemplates.Properties.AppearanceDropDown.Font, FontStyle.Regular);

XAML: how to set TextElement.Name property

I am programatically constructing a block of XAML and would like to provide a name x:Name to a TextElement object (an Underline in this case) but even though the Name prop provides a setter, it can not be set per this MSDN article:
MSDN: TextElement.Name
Gets or sets a unique identification for the object. Name can only be set from initial parsing of XAML.
Here is my code:
public void AddLink(string token, string text, string uri)
{
var elem = new Underline();
elem.Name.Name = token; // <-- I would expect this would work...
elem.Inlines.Add();
if (Container == null)
Container = new Paragraph();
Container.Inlines.Add(elem);
}
I am not sure if I get your question right but to me it seems you are asking to register a name to specific namescope in wpf.
This is what you need:
MSDN: FrameworkElement.RegisterName
Here is an example:
myMainPanel = new StackPanel();
myMainPanel.Background = Brushes.Orange;
button1 = new Button();
button1.Name = "Button1";
// Register button1's name with myMainPanel.
myMainPanel.RegisterName(button1.Name, button1);
button1.Content = "Button 1";
button1.Click += new RoutedEventHandler(button1Clicked);
myMainPanel.Children.Add(button1);
button2 = new Button();
button2.Name = "Button2";
// Register button2's name with myMainPanel.
myMainPanel.RegisterName(button2.Name, button2);
button2.Content = "Button 2";
button2.Click += new RoutedEventHandler(button2Clicked);
myMainPanel.Children.Add(button2);

command binding for ribbon control

I'm trying to use the Microsoft ribbon control programatically using C#. Everything is fine but I'm unable to bind the command through the RibbonCommand. Can anyone give me an example of how to do this? My actual code is:
Ribbon rbn = new Ribbon();
RibbonTab file = new RibbonTab();
file.Name = "file";
file.Label = "FILE";
RibbonTab edit = new RibbonTab();
edit.Name = "edit";
edit.Label = "Edit";
RibbonGroupPanel rbgp = new RibbonGroupPanel();
RibbonGroup rbg = new RibbonGroup();
RibbonButton rbtn = new RibbonButton();
rbtn.Content = "New";
RibbonCommand rcomnd = new RibbonCommand();
rcomnd.LabelTitle = "NEW";
rcomnd.ToolTipDescription = "THIS IS NEW";
rcomnd.LargeImageSource = imgSource;
rcomnd.Execute(rbtn, rbtn);
rbtn.IsEnabled = true;
//rcomnd.SmallImageSource = imgSource;
rcomnd.CanExecute +=new CanExecuteRoutedEventHandler(rcomnd_CanExecute);
rcomnd.Executed +=new ExecutedRoutedEventHandler(rcomnd_Executed);
CommandBinding cmdb = new CommandBinding(ApplicationCommands.New);
cmdb.Command = ApplicationCommands.New;
cmdb.Executed +=new ExecutedRoutedEventHandler(cmdb_Executed);
CommandBind.Add(cmdb);
//rcomnd.Executed += new ExecutedRoutedEventHandler(OnAddNewEntry);*/
rbtn.Click +=new System.Windows.RoutedEventHandler(rbtn_Click);
rbtn.Command = rcomnd;
But the bindings are not working and the button is not enabled.
Check this tutorial from the "Adding Commands" section. It may be good to read it from the start.

Categories