Xamarin.Forms将文本绑定到对象属性

时间:2016-11-28 17:28:19

标签: c# data-binding xamarin.forms

我对Xamarin.Forms和c#都是全新的,如果这是一个愚蠢的问题,请原谅我。 我必须创建一个包含一些条目和一个选择器的页面,并使用输入的值来设置对象Cover的属性。 这是我的封面对象:

  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按钮时,创建的对象Cover将所有这些条目值作为属性创建(因此entryTitle.TextProperty应填充封面的Title属性,依此类推)但我只是&#39找到一种方法来做到这一点。我只需要在单击Ok si时创建对象。 谢谢你的帮助

1 个答案:

答案 0 :(得分:1)

没有任何问题是愚蠢的!

你的Cover对象很好。

但是,您使用页面代码隐藏业务逻辑的方法并非如此。这是一个兔子洞,随着你的计划的发展,它会有很多问题。

Model-View-ViewModel模式是Xamarin Forms开发的一种强烈推荐的方法。我建议您在https://blogs.msdn.microsoft.com/microsoft_press/2016/03/31/free-ebook-creating-mobile-apps-with-xamarin-forms/

下载并阅读Petzold的免费书籍

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