2016-11-28 63 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

相關問題