无法从代码隐藏访问 Metro XAML 自定义控件
-
13-12-2019 - |
题
尝试使用该解决方案 这 问题给了我一个奇怪的问题。(为方便起见,复制于此)
这是一个问题的解决方案 ListView
吞掉右键单击事件并阻止AppBar打开 - 继承的类 ListView
并覆盖 OnRightTapped
的事件 ListViewItem
:
public class MyListView : ListView
{
protected override DependencyObject GetContainerForItemOverride()
{
return new MyListViewItem();
}
}
public class MyListViewItem : ListViewItem
{
protected override void OnRightTapped(Windows.UI.Xaml.Input.RightTappedRoutedEventArgs e)
{
base.OnRightTapped(e);
e.Handled = false; // Stop 'swallowing' the event
}
}
<CustomControls:MyListView
x:Name="ItemsList"
...
SelectionMode="Single"
RightTapped="MyListView_RightTapped">
</CustomControls:MyListView>
我已经按照指定的方式在名为 CustomControls 的新命名空间中实现了自定义控件,与所描述的完全一样。我已将该命名空间添加到 MainPage.xaml 中
xmlns:CustomControls="using:CustomControls"
当我尝试在后面的代码中引用“ItemsList”时,出现编译错误
The name 'ItemsList' does not exist in the current context
我尝试过构建、重建、清理解决方案、关闭并重新打开解决方案、将类放在主项目命名空间下,但都无济于事。
总而言之,MainPage.cs 在 MainPage.xaml 上看不到自定义控件
更新2:重新表述问题以删除不相关的问题。我还更改了标题以反映真正的问题。
解决方案
我正在使用 Name
但应该一直在使用 x:Name
. 。显然自定义控件和用户控件需要使用 x:Name
而不是 Name
在后面的代码中可以看到。
有关差异的更多信息请参见此处:
不隶属于 StackOverflow