【问题标题】:Calling a method of a parent page from UserController从 UserController 调用父页面的方法
【发布时间】:2013-01-17 05:05:01
【问题描述】:

我已关注this 问题并尝试构建我的解决方案。问题是“UserControlButtonClicked”似乎为空!所以 if 中的 'UserControlButtonClicked(this, EventArgs.Empty)' 不会运行,并且父页面中的方法 'addStepContent' 永远不会被调用。

UserControl 'StepsBar'

public sealed partial class StepsBar : UserControl
    {

        public event EventHandler UserControlAddStepContent;

        [...]

    public StepsBar()
    {
        this.InitializeComponent();
                    Image step_1 = new Image();

        ButtonInfo step_1Info = new ButtonInfo();
        step_1Info.Add((int)stepNumber.one, (int)stepStatus.normal);
        step_1.Tag = step_1Info;

        step_1.Source = setBackground((int)stepStatus.normal);
        step_1.Tapped += stepTapped;

        [...]
    }

public void stepTapped(Object sender, RoutedEventArgs e)
    {
        [...]

        if (step != null)
        {

           [...]

            firePageEvent(); 

        }

    }

    public void firePageEvent() 
    {
        if (UserControlAddStepContent != null)
        {
            UserControlAddStepContent(this, EventArgs.Empty); 
        }
    }

父页面

public Violation()
    {

        this.InitializeComponent();

        StepsBar stepsBar = new StepsBar();

        stepsBar.UserControlAddStepContent += new EventHandler(addStepContent); 


    }

    private void addStepContent(object sender, EventArgs e) 
    {

        CheckBox check_1 = new CheckBox();
        check_1.Content = "Check me!";
        bodyStackPanel.Children.Add(check_1);

    }

【问题讨论】:

    标签: c# methods user-controls


    【解决方案1】:

    这假设您想要使用现有的委托而不是自己创建委托,并且您没有通过事件参数传递任何特定于父页面的内容。

    在用户控件的代码隐藏中(如果不使用代码隐藏或 C#,则根据需要进行调整):

       public partial class MyUserControl : System.Web.UI.UserControl
        {
            public event EventHandler UserControlButtonClicked;
    
    
        private void OnUserControlButtonClick()
        {
            if (UserControlButtonClicked != null)
            {
                UserControlButtonClicked(this, EventArgs.Empty);
            }
        }
    
        protected void TheButton_Click(object sender, EventArgs e)
        {
            // .... do stuff then fire off the event
            OnUserControlButtonClick();
        }
    
        // .... other code for the user control beyond this point
    }
    

    在页面本身中,您可以通过以下方式订阅事件:

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // hook up event handler for exposed user control event
            MyUserControl.UserControlButtonClicked += new  
                        EventHandler(MyUserControl_UserControlButtonClicked);
        }
        private void MyUserControl_UserControlButtonClicked(object sender, EventArgs e)
        {
            // ... do something when event is fired
        }
    
    }
    

    【讨论】:

    • 正如我在问题中所写,我使用您粘贴的确切示例,但没有工作:UserControlButtonClicked' 似乎为空!所以 if 中的 'UserControlButtonClicked(this, EventArgs.Empty)' 不会运行。
    • 我只为您提供了一个按预期工作的示例,并且可以访问父页面上的方法。
    【解决方案2】:

    解决了。问题是这个,在父页面上。

    StepsBar stepsBar = new StepsBar();
    
        stepsBar.UserControlAddStepContent += new EventHandler(addStepContent); 
    

    StepsBar 的 istance 没有添加到页面中。哦! 所以这就是我所做的:

        stepsBar.UserControlAddStepContent += new EventHandler(addStepContent); 
    

    在父页面的xaml上:

    <local:StepsBar x:Name="stepsBar"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多