我有个窗户。
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="GamingClient.UserInterface.MenuPartieRapide"
xmlns:local="clr-namespace:GamingClient.UserInterface">
<local:MapCarrousel />
</Window>这是MapCarrousel用户控件。
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
x:Class="GamingClient.UserInterface.MapCarrousel" d:DesignWidth="1002" d:DesignHeight="122">
</UserControl>下面是userControl背后的代码
public int GetMapId()
{
.... Return some Int
}我试着在我的MapCarrousel窗口中添加一个名字
<local:MapCarrousel Name="Test" />因此,我尝试在我的Window.xaml.cs中调用它。
void function Test()
{
Test.GetMapId();
}然后,我尝试了以下操作:在用户控件中添加x:name,并在我的Window.xaml.cs中的一个函数中调用那个x:name。
以前的结果是一样的。
问题是:如果用户控件不是动态创建的(即新的User()),而是添加了“抛出设计器”,那么如何在用户控件上调用公共函数(它是我所在的名称空间/对象的子函数)?
发布于 2014-03-05 21:12:19
当您试图在您的Name控件上设置MapCarrousel属性时,您应该有一个错误,如下所示:
错误2,因为'MS.Internal.Design.Metadata.ReflectionTypeNode‘是在同一个程序集中实现的,所以必须设置x:Name属性,而不是MS.Internal.Design.Metadata.ReflectionPropertyNode属性。
这告诉你你不能这样做:
<local:MapCarrousel Name="Test" />正如错误告诉您的,您需要这样做:
<local:MapCarrousel x:Name="Test" />然后你就可以打电话:
Test.GetMapId();https://stackoverflow.com/questions/22209133
复制相似问题