为什么在 xaml 中设置的用户控件在 cs 中无法识别?
Why user control which was set in xaml is not recognized in cs?
我创建了一个简单的用户控件并在 xaml 文件 (mainWindow.xaml
) 中使用了它。
我将用户控件的实例命名为 VolumeMeter
,然后尝试在 cs 文件中的函数中使用它 (mainWindow.cs
) 但名称无法识别
The name doesn't exist in the current context
这是为什么?我该如何解决?
xaml:
<Window x:Class="testUserControl.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:MyNameSpace="clr-namespace:testUserControl.UserControls"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Button Name="btnShow" Content="Show"></Button>
<StackPanel Name="stkTest" Grid.Row="1" Orientation="Vertical">
<MyNameSpace:UserControl1 Name="VolumeMeter"></MyNameSpace:UserControl1>
</StackPanel>
</Grid>
</Window>
cs文件:
namespace testUserControl
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnShow_Click_1(object sender, RoutedEventArgs e)
{
VolumeMeter.Enable = false;//Compile error
}
}
}
尝试使用 x:Name
而不是 Name
[msdn]:
...
<StackPanel Name="stkTest" Grid.Row="1" Orientation="Vertical">
<MyNameSpace:UserControl1 x:Name="VolumeMeter"></MyNameSpace:UserControl1>
</StackPanel>
...
使用 x:Name 会起作用。请参考In WPF, what are the differences between the x:Name and Name attributes?以获得很好的解释。
您应该使用 x:Name
而不是 Name
看这个 post 看看区别
In WPF, what are the differences between the x:Name and Name attributes?
我创建了一个简单的用户控件并在 xaml 文件 (mainWindow.xaml
) 中使用了它。
我将用户控件的实例命名为 VolumeMeter
,然后尝试在 cs 文件中的函数中使用它 (mainWindow.cs
) 但名称无法识别
The name doesn't exist in the current context
这是为什么?我该如何解决?
xaml:
<Window x:Class="testUserControl.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:MyNameSpace="clr-namespace:testUserControl.UserControls"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Button Name="btnShow" Content="Show"></Button>
<StackPanel Name="stkTest" Grid.Row="1" Orientation="Vertical">
<MyNameSpace:UserControl1 Name="VolumeMeter"></MyNameSpace:UserControl1>
</StackPanel>
</Grid>
</Window>
cs文件:
namespace testUserControl
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnShow_Click_1(object sender, RoutedEventArgs e)
{
VolumeMeter.Enable = false;//Compile error
}
}
}
尝试使用 x:Name
而不是 Name
[msdn]:
...
<StackPanel Name="stkTest" Grid.Row="1" Orientation="Vertical">
<MyNameSpace:UserControl1 x:Name="VolumeMeter"></MyNameSpace:UserControl1>
</StackPanel>
...
使用 x:Name 会起作用。请参考In WPF, what are the differences between the x:Name and Name attributes?以获得很好的解释。
您应该使用 x:Name
而不是 Name
看这个 post 看看区别
In WPF, what are the differences between the x:Name and Name attributes?