gpt4 book ai didi

c# - 无法使用我从 MSDN 复制的代码覆盖继承的成员 SelectTemplate...

转载 作者:太空狗 更新时间:2023-10-30 00:20:16 24 4
gpt4 key购买 nike

我有一个覆盖问题。我知道您不能覆盖继承的成员,除非它们被标记为虚拟、抽象或覆盖,但是...

我有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

我花了几个小时寻找原因,但无法弄清楚。有什么想法吗?

最佳答案

您引用的示例适用于 WPF ( System.Windows.Controls.DataTemplateSelector )。

在 Windows 应用商店应用中,您使用的是 Windows.UI.Xaml.Controls.DataTemplateSelector有不同的语义,SelectTemplate确实不可覆盖,但是 SelectTemplateCore是。

关于c# - 无法使用我从 MSDN 复制的代码覆盖继承的成员 SelectTemplate...,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12811064/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com