访问在MainWindow()构造函数中实例化的类
本文关键字:实例化 构造函数 MainWindow 访问 | 更新日期: 2023-09-27 18:06:41
我是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,当选择被改变时,我想从鹪chviewmodel类调用一个函数,它将填充一些变量(我稍后将绑定到TextBlocks)。
然而,我的电话扳手。GetWrench会导致" name扳手在当前上下文中不存在"构建错误。
将鼠标光标移到MainWindow构造函数中的"扳手"实例上,表明它是一个局部变量。
我怎样才能使至少 MainWindow类可以访问它?
您可以将变量声明向上移动一个作用域到类中,这样它不仅在构造函数(局部作用域)中可用,而且在整个类中可用:
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);
}
}
c#中的花括号表示作用域,它告诉您方法、类、命名空间或其他作用域在哪里结束。您不能访问其自身作用域之外的任何内容,也不能从另一个作用域访问。
你需要做的是将变量的声明移到构造函数的作用域之外。您仍然在构造函数中为视图模型赋值,但是声明位于其他方法可以看到的范围内。
一样:
public partial class MainWindow : Window
{
WrenchViewModel wrench;
public MainWindow()
{
InitializeComponent();
wrench = new WrenchViewModel();
因为您已经定义了一个局部变量:
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);
}
}