2012-10-10 100 views
4

複製的代碼我有一個覆蓋問題。我知道你不能覆蓋繼承成員,除非它們被標記虛擬,抽象或重寫,但...無法覆蓋繼承成員SelectTemplate ...與我從MSDN

this code從MSDN:

using System.Windows; 
using System.Windows.Controls; 

namespace SDKSample 
{ 
    public class TaskListDataTemplateSelector : DataTemplateSelector 
    { 
     public override DataTemplate 
      SelectTemplate(object item, DependencyObject container) 
     { 
      FrameworkElement element = container as FrameworkElement; 

      if (element != null && item != null && item is Task) 
      { 
       Task taskitem = item as Task; 

       if (taskitem.Priority == 1) 
        return 
         element.FindResource("importantTaskTemplate") as DataTemplate; 
       else 
        return 
         element.FindResource("myTaskTemplate") as DataTemplate; 
      } 

      return null; 
     } 
    } 
} 

我加入這個代碼:

public class PostTemplateSelector : DataTemplateSelector 
{ 
    public override DataTemplate SelectTemplate(object item, DependencyObject container) 
    { 
     ListViewItem lvi = item as ListViewItem; 

     AppNetClient.PostClass key = lvi.DataContext as AppNetClient.PostClass; 

     if (key.postTemplate == "Post") 
     { 
      return lvi.FindName("PostDataTemplate") as DataTemplate; 
     } 
     else 
     { 
      return lvi.FindName("SelectedPostDataTemplate") as DataTemplate; 
     } 
    } 
} 

而且我得到這個錯誤:

Error 3 'oneSocialLife.MainPage.PostTemplateSelector.SelectTemplate(object, Windows.UI.Xaml.DependencyObject)': cannot override inherited member 'Windows.UI.Xaml.Controls.DataTemplateSelector.SelectTemplate(object, Windows.UI.Xaml.DependencyObject)' because it is not marked virtual, abstract, or override C:\Users\kcone\documents\visual studio 2012\Projects\HttpClientApp\HttpClientApp\MainPage.xaml.cs 339 42 oneSocialLife 

我一直在尋找WH花了幾個小時Ÿ,但無法弄清楚。有任何想法嗎?

回答

4

,結束了工作的代碼:從 :http://msdn.microsoft.com/en-us/library/ms742521.aspx 也:​​

public class PostTemplateSelector : DataTemplateSelector 
{ 
    public DataTemplate PostTemplate { get; set; } 
    public DataTemplate SelectedPostTemplate { get; set; } 

    protected override DataTemplate SelectTemplateCore(object item, DependencyObject container) 
    { 
     //ListViewItem lvi = item as ListViewItem; 
     //AppNetClient.PostClass key = lvi.DataContext as AppNetClient.PostClass; 

     AppNetClient.PostClass key = item as AppNetClient.PostClass; 

     if (key.postTemplate == "Post") 
     { 
      return PostTemplate; 
     } 
     else 
     { 
      return SelectedPostTemplate; 
     } 
    } 
}