2013-10-24 87 views
0

在我的windows phone 8應用程序中,在嘗試創建依賴項屬性時,我總是收到此異常。我做錯了什麼,請指導我。無法通過文本'Button_Click'創建'System.Windows.RoutedEventHandler'

{System.Windows.Markup.XamlParseException:無法創建文本 'Button_Click' 一個 'System.Windows.RoutedEventHandler'。 System.Windows.Application.LoadComponent(Object component,Uri resourceLocator)at com.sap.View.HomePage.InitializeComponent()
at com.sap.View.HomePage .. [0126]構造函數()}

部首

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(); 
     } 

回答

0

雖然不是一個答案,但我找到了一個替代的解決方案。

添加的事件到用戶控件(報頭)的代碼隱藏

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); 
      } 
     } 

將其綁定在用戶控件:

Click="Button_Click" 
在頁

然後:

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

和在代碼隱藏:

public void Menu_Button_Click(object sender, RoutedEventArgs e) 
    { 

    }