无法从文本'Button_Click'创建'System.Windows.RoutedEventHandler'

时间:2013-10-24 09:08:48

标签: c#

在我的Windows Phone 8应用程序中,在尝试创建依赖项属性时,我总是遇到此异常。我做错了什么,请指导我。

  

{System.Windows.Markup.XamlParseException:无法创建   来自文本'Button_Click'的'System.Windows.RoutedEventHandler'。   [线路:108位置:66] at   System.Windows.Application.LoadComponent(Object component,Uri   resourceLocator)在com.sap.View.HomePage.InitializeComponent()
  在com.sap.View.HomePage..ctor()}

这是Header的代码隐藏

public static readonly DependencyProperty MenuClickProperty = DependencyProperty.Register("MenuClick", typeof(RoutedEventHandler), typeof(Header), new PropertyMetadata(OnMenuClickHandlerChanged));

public RoutedEventHandler MenuClick
        {
            get { return (RoutedEventHandler)GetValue(MenuClickProperty); }
            set { SetValue(MenuClickProperty, new RoutedEventHandler(value)); }
        }

        private static void OnMenuClickHandlerChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Header header = d as Header;
            header.OnMenuClickHandlerPropertyChanged(e);
        }
        private void OnMenuClickHandlerPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            MenuButton.Click += MenuClick;
        }

这是我的用户控件(标题)

<Button Click="{Binding Path=MenuClick, Source={RelativeSource Mode=Self}}" />

这就是我在我的网页上包含控件的方式:

<myControls:Header Title="{Binding Title}" MenuClick="Button_Click" />

这是代码隐藏的:

public void Button_Click(object sender, RoutedEventArgs e)
        {
            OpenSettings();
        }

1 个答案:

答案 0 :(得分:0)

虽然不是答案,但我找到了另一种解决方案。

将事件添加到UserControl(Header)的代码隐藏

public delegate void MenuClickEventHandler(object sender, RoutedEventArgs e);
        public event MenuClickEventHandler MenuClicked;
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (MenuClicked != null)
            {
                MenuClicked(this, e);
            }
        }

在usercontrol中绑定它:

Click="Button_Click"

然后在Page:

<myControls:Header Title="{Binding Title}" MenuClicked="Menu_Button_Click" />

和代码隐藏:

public void Menu_Button_Click(object sender, RoutedEventArgs e)
    {

    }