我是C#和WPF的新手,我在访问在MainWindow()构造函数中实例化的类时遇到了一些麻烦。
这是我的代码:
namespace PivotBlockPicker.View
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
WrenchViewModel wrench = new WrenchViewModel();
MeasurementViewModel measurements = new MeasurementViewModel();
}
private void modelSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
wrench.GetWrench(modelSelector.SelectedItem);
}
}
}
这是我的XAML:
<Window x:Class="PivotBlockPicker.View.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:PivotBlockPicker.View"
xmlns:vm="clr-namespace:PivotBlockPicker.ViewModel"
xmlns:m="clr-namespace:PivotBlockPicker.Model"
mc:Ignorable="d"
Title="MainWindow" Height="347" Width="288.666"
DataContext="vm:WrenchViewModel">
<Grid>
<GroupBox x:Name="wrenchBox" Header="Torque Wrench" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="262" Width="250">
<Grid>
<Grid Margin="0,0,0,140.667" Height="88" VerticalAlignment="Bottom">
<TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="10,3,0,0" TextWrapping="Wrap" Text="Model:" VerticalAlignment="Top"/>
<ComboBox x:Name="modelSelector" HorizontalAlignment="Left" Margin="52,0,0,0" VerticalAlignment="Top" Width="120" SelectionChanged="modelSelector_SelectionChanged">
<ComboBoxItem>QD2R200</ComboBoxItem>
<ComboBoxItem>QD2R50</ComboBoxItem>
</ComboBox>
<Grid HorizontalAlignment="Left" Width="228" Margin="0,27,0,0">
<TextBlock x:Name="blockFirstTP" Text="TextBlock" HorizontalAlignment="Left" Margin="10,0,0,0" TextWrapping="Wrap" VerticalAlignment="Top"/>
<TextBlock x:Name="blockSecondTP" Text="TextBlock" HorizontalAlignment="Left" Margin="10,21,0,0" TextWrapping="Wrap" VerticalAlignment="Top"/>
<TextBlock x:Name="blockThirdTP" Text="TextBlock" HorizontalAlignment="Left" Margin="10,42,0,0" TextWrapping="Wrap" VerticalAlignment="Top"/>
</Grid>
</Grid>
<GroupBox x:Name="blockBox" Header="Pivot Block Dimensions" HorizontalAlignment="Left" Margin="10,100,0,0" VerticalAlignment="Top" Height="86">
<Grid>
<TextBlock x:Name="blockTextHorizontal" HorizontalAlignment="Left" Margin="51,12,0,0" TextWrapping="Wrap" Text="Vertical" VerticalAlignment="Top" Height="16" Width="40"/>
<TextBox x:Name="blockHorizontal" Text="" HorizontalAlignment="Left" Height="20" Margin="10,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="36" MaxLength="5"/>
<TextBlock x:Name="blockTextVertical" HorizontalAlignment="Left" Margin="51,37,0,0" TextWrapping="Wrap" Text="Horizontal" VerticalAlignment="Top" Height="16" Width="58"/>
<TextBox x:Name="blockVertical" Text="" HorizontalAlignment="Left" Height="20" Margin="10,35,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="36" MaxLength="5"/>
</Grid>
</GroupBox>
</Grid>
</GroupBox>
<Button x:Name="button" Content="Get Wrench Info" HorizontalAlignment="Left" Margin="185,277,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
</Window>
我的窗口有一个ComboBox,当选择被更改时,我想从WrenchViewModel类调用一个函数来填充一些变量(我稍后将数据绑定到TextBlocks。
但是,我对wrench.GetWrench的调用会导致“当前上下文中不存在名称扳手”构建错误。
将我的鼠标光标悬停在MainWindow构造函数中的“wrench”实例上,表明它是一个局部变量。
我怎样才能让至少使用MainWindow类?
答案 0 :(得分:1)
您可以将变量声明一个范围移到类中,因此它不仅可以在构造函数(本地范围)中使用,而且可以在整个类中使用:
public partial class MainWindow : Window
{
private WrenchViewModel wrench;
private MeasurementViewModel measurements;
public MainWindow()
{
InitializeComponent();
wrench = new WrenchViewModel();
measurements = new MeasurementViewModel();
}
private void modelSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
wrench.GetWrench(modelSelector.SelectedItem);
}
}
答案 1 :(得分:0)
C#中的花括号括号表示范围,告诉您方法,类,命名空间或其他范围的结束位置。您无法访问其自身范围之外的任何内容,也无法访问其他范围内的任何内容。
您需要做的是将变量的声明移到构造函数的范围之外。您仍然在构造函数的一侧分配视图模型,但声明在另一个方法可以看到的范围内。
像这样:
public partial class MainWindow : Window
{
WrenchViewModel wrench;
public MainWindow()
{
InitializeComponent();
wrench = new WrenchViewModel();
答案 2 :(得分:0)
因为您已经定义了一个局部变量:
public MainWindow()
{
InitializeComponent();
WrenchViewModel wrench = new WrenchViewModel();
MeasurementViewModel measurements = new MeasurementViewModel();
}
构造函数内部。 您应该将其更改为此私人成员:
public partial class MainWindow : Window
{
private WrenchViewModel _wrench = new WrenchViewModel();
private MeasurementViewModel _measurements = new MeasurementViewModel();
public MainWindow()
{
InitializeComponent();
}
private void modelSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
_wrench.GetWrench(modelSelector.SelectedItem);
}
}