简体   繁体   中英

Accessing classes instantiated during MainWindow() constructor

I'm pretty new to C# and WPF and I'm having a bit of trouble accessing a class that was instantiated during the MainWindow() constructor.

Here's my code:

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);
        }
    }
}

And here's my 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>

My window has a ComboBox that, when the selection is changed, I want to call a function from the WrenchViewModel class that will populate some variables (that I will later databind to the TextBlocks.

However, my call to wrench.GetWrench results in a "The name wrench does not exist in the current context" build error.

Floating my mouse cursor over the the instance of "wrench" in the MainWindow constructor shows that it's a local variable.

How can I get this to be accessible to at least the MainWindow class?

You can move the variable declaration one scope up into the class, so it's available not only inside the constructor (local scope) but in the whole class:

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);
    }
}

The curly brackets brackets in C# denote scope, the tell you where a method, class, namespace, or other scopes end. You cannot access anything outside it's own scope, or from another scope.

What you need to do is move the declaration of your variable outside the scope of the constructor. You still assign your view model in side the constructor, but the declaration is in a scope that the other method can see.

Like so:

public partial class MainWindow : Window
{
    WrenchViewModel wrench;

    public MainWindow()
    {
        InitializeComponent();
        wrench = new WrenchViewModel();

Because you have defined a local variable:

 public MainWindow()
    {
        InitializeComponent();
        WrenchViewModel wrench = new WrenchViewModel();
        MeasurementViewModel measurements = new MeasurementViewModel();
    }

Inside of constructor. You should change it to this private member:

 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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM