Add parameter to Button click event

前端 未结 4 963
抹茶落季
抹茶落季 2020-11-29 04:17

I have a wpf button like this:


<         


        
4条回答
  •  再見小時候
    2020-11-29 04:58

    Using Xaml and DataContext

    
    
    
        
            
            
        
    
    

    And MainPage Code Behing, this code example is to navigate to another page using the page type as argument, you need to make "YourPageTypeHere" and reference page here.

    Then implement code Behind.

    using System;
    using System.Windows.Input;
    using Xamarin.Forms;
    
    namespace DataAndCloudServices
    {
        public partial class MainPage : ContentPage
        {
            public MainPage()
            {
                InitializeComponent();
    
                NavigateCommand = new Command(
                  async (Type pageType) =>
                  {
                      Page page = (Page)Activator.CreateInstance(pageType);
                      await Navigation.PushAsync(page);
                  });
    
                this.BindingContext = this;
            }
            public ICommand NavigateCommand { private set; get; }
        }
    }
    

    Also in your App class need a Instance of NavigationPage in MainPage to Navigate (For this example)

    public App ()
            {
                InitializeComponent();
    
                MainPage = new NavigationPage(new MainPage());
            }
    

    It is for xamarin forms, But It is similar for WPF Projects.

    Command could be changed with for WPF and Xamarin: "https://stackoverflow.com/a/47887715/8210755"

提交回复
热议问题