Reputation: 31
I'm totally new to Xamarin.Forms and c#, so forgive me if this is a stupid question. I have to make a page with some entry and a picker and use the entered values to set the properties of the object Cover. Here is my Cover Object:
public class Cover
{
public enum Categories { Brasses, Percussions, Keyboards, Strings, Voices, Woodwinds, Other };
public User administrator { get; private set; }
public List<Track> tracks { get; private set; }
public String title { get; private set; }
public String author { get; private set; }
public String note { get; private set; }
public Boolean collaborative;
public Categories category;
public Cover(User administrator, String title, String author, String note, Categories category, Boolean collaborative, List<Track> tracks = null)
{
this.administrator = administrator;
this.tracks = tracks == null ? new List<Track>() : tracks;
this.title = title;
this.author=author;
this.category = category;
this.collaborative = collaborative;
}
public Cover() { }
And here is my page:
public createCover()
{
var userTest = new User("SmartPerson", "", null, null);
var entryTitle = new Entry
{
Placeholder = " ",
Keyboard = Keyboard.Text,
};
var entryAuthor = new Entry
{
Placeholder = " ",
Keyboard = Keyboard.Text,
};
var editorNote = new Editor
{
Text = "Ex: Metal Cover of the Super Mario main theme!",
FontSize = 10,
Keyboard = Keyboard.Text,
};
var pickerCategory = new Picker
{
Title = "Category",
VerticalOptions = LayoutOptions.Center
};
var categories = new List<string> { "Blues", "Country", "Dance", "Hip-Hop", "Jazz", "Metal", "Pop", "Rap", "Reggae", "Rock", "Classical", "Soundtracks", "Videogames", "Instrumental", "Anime", "Dubstep"};
foreach (string categoryName in categories)
{
pickerCategory.Items.Add(categoryName);
}
var collaborateSwitch = new Switch
{
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.CenterAndExpand
};
var collaborate = new Label
{
Text = "Do you want other musicians to collaborate with you?",
FontSize = 15,
HorizontalOptions = LayoutOptions.Start,
};
var ok = new Button
{
Text = "Ok",
FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Button)),
HorizontalOptions = LayoutOptions.Center,
};
ok.Clicked += OnOkClicked;
var stack = new StackLayout
{
Children =
{
entryTitle,
entryAuthor,
editorNote,
pickerCategory,
collaborate,
collaborateSwitch,
ok,
},
};
var scrollView = new ScrollView
{
VerticalOptions = LayoutOptions.FillAndExpand,
Content = stack
};
Content = scrollView;
this.Padding = new Thickness(20);
this.BindingContext = new Cover();
}
private void OnOkClicked(object sender, EventArgs e)
{
///???
}
}
}
What I want is that when the Ok Button is clicked the object Cover is created with all those entry values as properties (so the entryTitle.TextProperty should fill the Title property of cover, and so on) but I just don't find a way to do it. I need the object to be created only when Ok si clicked. Thanks for the help
Upvotes: 0
Views: 423
Reputation: 657
No questions are ever stupid!!
Your Cover object is fine.
But your approach of using the page's code behind for your business logic is not. It's a rabbit hole that has many issues down the road as your program develops.
The Model-View-ViewModel pattern is a highly recommended approach for Xamarin Forms development. Might I suggest that you download and read Petzold's free book at https://blogs.msdn.microsoft.com/microsoft_press/2016/03/31/free-ebook-creating-mobile-apps-with-xamarin-forms/
I believe if you adopt the MVVM approach the answer to your question will become clearer to you.
Upvotes: 1