How to implement a navigation button in shared application resources?

后端 未结 1 1510
旧时难觅i
旧时难觅i 2020-11-28 14:05

I\'m currently trying to create a simple app that pulls data from an API I made and displays it in a list. You are then supposed to be able to click the list items to be nav

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 14:45

    This is an Universal App solution. It's can view as basically a tutorial on Model, View,and ViewModel.

    I don't know what UI Element you want to use, but for this I going to pick a one that is supported in both Windows 8.1 and WP 8.1. The ListView, so in both Project's MainPage.xaml lets define that.


    
        
            
                
                    
                    
                    

    As you can see, I databinded the Button with a Command and a CommandParameter. The command is the function in the ViewModel that I want to execute when the user clicks the button. The CommandParameter is a object you want to pass to your Command function. In your case, your TAG.


    Namespaces

    using System.Collections.ObjectModel;              // ObservableCollection
    using System.Windows.Input;                        // ICommand
    

    Now lets defined a Command (Should be part of the Shared Project)

    public class MySimpleCommand : ICommand
    {
        public void Execute(object parameter)
        {
            // do stuff based off your tags
            string tag = parameter as string;
            if(tag == "WHATEVER_YOU_WANT")
            {
            }
    
            // void of the trigger libraries, lets make this simple
            // navigate to your page
            Frame rootFrame = Window.Current.Content as Frame;
            rootFrame.Navigate(typeof(YOUR_PAGE));
    
        }
        public bool CanExecute(object parameter)
        {
            return true;
        }
        public event EventHandler CanExecuteChanged;
    }
    

    Now lets setup a simple model (Should be part of the Shared Project)

    public class sample_model
    {
        public sample_model(string artist, string song, string extra = "")
        {
            this.Artist = artist;
            this.Song = song;
            this.Extra = extra;
        }
    
        public string Artist { get; set; }
        public string Song { get; set; }
        public string Extra { get; set; }         // this is your tag
    }
    

    Now lets setup a ViewModel for our ListView(s) to use. (Should be part of the Shared Project)

    public class sample_viewmodel
    {
        public sample_viewmodel()
        {
            this.DataSource = CreateData();
    
            this.SimpleCommand = new MySimpleCommand();  // create the command
        }
    
        // create a static list for our demo
        private ObservableCollection CreateData()
        {
            ObservableCollection my_list = new ObservableCollection();
            my_list.Add(new sample_model("Faith + 1", "Body of Christ", "A Track"));
            my_list.Add(new sample_model("Faith + 1", "Christ Again", "A Track"));
            my_list.Add(new sample_model("Faith + 1", "A Night With the Lord", "A Track"));
            my_list.Add(new sample_model("Faith + 1", "Touch Me Jesus", "A Track"));
            my_list.Add(new sample_model("Faith + 1", "I Found Jesus (With Someone Else)", "A Track"));
            my_list.Add(new sample_model("Faith + 1", "Savior Self", "A Track"));
            my_list.Add(new sample_model("Faith + 1", "Christ What a Day", "A Track"));
            my_list.Add(new sample_model("Faith + 1", "Three Times My Savior", "A Track"));
            my_list.Add(new sample_model("Faith + 1", "Jesus Touched Me", "A Track"));
            my_list.Add(new sample_model("Faith + 1", "Lord is my Savior", "A Track"));
            my_list.Add(new sample_model("Faith + 1", "I Wasn't Born Again Yesterday", "A Track"));
            my_list.Add(new sample_model("Faith + 1", "Pleasing Jesus", "A Track"));
            my_list.Add(new sample_model("Faith + 1", "Jesus (Looks Kinda Hot)", "A Track"));
            my_list.Add(new sample_model("Butters", "What What", "B Track"));
            return my_list;            
        }
    
        public ICommand SimpleCommand { get; set; }
        public ObservableCollection DataSource{ get; set; }
    
    }
    

    And finally, lets link the ListView to our ViewModel which should bind the "Artist, Song, and Button (Command & CommandParamters)". I usually do this in each Page's Load function.

    private void Page_Loaded(object sender, RoutedEventArgs e)
    {
        sample_viewmodel viewmodel = new sample_viewmodel();  // create the view model
        myListView.DataContext = viewmodel;                   // set the datacontext (this will link the commands)
        myListView.ItemsSource = viewmodel.DataSource;        // set the ItemsSource, this will link the Artist,Songs
    }
    

    There you have it, each time the user clicks on the button no matter what platform it will perform the Command function.


    Sample Screenshot enter image description here

    0 讨论(0)
提交回复
热议问题