2016-11-28 67 views
0

我对Xamarin.Forms和c#完全陌生,所以如果这是一个愚蠢的问题,请原谅我。 我必须创建一个页面,其中包含一些条目和一个选择器,并使用输入的值来设置对象封面的属性。 这里是我的覆盖目标:Xamarin.Forms将文本绑定到对象属性

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() { } 

这里是我的网页:

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) 
     { 
      ///??? 
     } 

    } 
} 

我想的是,当点击OK按钮时,对象覆盖与所有的项值作为属性创建(所以entryTitle.TextProperty应该填充封面的Title属性,等等),但我只是没有找到办法做到这一点。我需要在Ok si点击时创建对象。 感谢您的帮助

回答

1

没有问题是愚蠢的!

您的封面对象没问题。

但是您的业务逻辑使用页面代码的方法不是。随着程序的发展,这是一个兔子洞,随着许多问题的出现。

对于Xamarin Forms开发,Model-View-ViewModel模式是强烈推荐的方法。我建议你下载并阅读Petzold的免费书籍https://blogs.msdn.microsoft.com/microsoft_press/2016/03/31/free-ebook-creating-mobile-apps-with-xamarin-forms/

我相信如果你采用MVVM方法,你的问题的答案会变得更加清晰。

+0

你能否帮我理解一下这个问题? 我知道我必须学习如何克服这一点,但这是一个大学项目,我正在努力学习如何在xD的道路上做到这一点 – SnowyNe

相关问题