Joe
Joe

Reputation: 1336

Access External Object From An Event Handler

Can anyone tell me how I can access an external object from an event handler?

The code below provides an example of what I'm trying to do. The references to externalClass in the event handler generate the following error message "The name 'externalClass' does not exist in the current context".

I've set the Assembly Output Type to Console Application so that it prints to the console.

Can anyone tell me how best to access the externalClass object from within the event handler? The code is below: XAML

<Window x:Class="AccessObjectFromEventHandler.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:AccessObjectFromEventHandler"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="10" />
        <RowDefinition Height="auto" />
        <RowDefinition Height="30" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="10" />
    </Grid.RowDefinitions>

    <Grid Grid.Row="1" ShowGridLines="True">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="20" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="auto" />
            <ColumnDefinition Width="20" />
        </Grid.ColumnDefinitions>
        <Button Grid.Row="1" Grid.Column="2"  Width="100" Height=" 30" Content="Click to Fire Event" Click="Button_Click"/>
    </Grid>
</Grid>

C#

using System;
using System.Windows;
namespace AccessObjectFromEventHandler
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            ExternalClass externalClass = new ExternalClass();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Console.WriteLine($"Button Click Event Fired.");
            externalClass.Name = "Some Name";
            externalClass.ExternalClassMethod();
        }
    }
    public partial class ExternalClass
    {
        public string Name { get; set; }
        //  The access modifier is "public" to enable access from external types.
        public void ExternalClassMethod()
        {
            Console.WriteLine($"ExternalClassMethod executed.  Name = {Name}");
        }
     }
}

Upvotes: 0

Views: 104

Answers (1)

Washington A. Ramos
Washington A. Ramos

Reputation: 924

Try this:

using System;
using System.Windows;
namespace AccessObjectFromEventHandler
{
    public partial class MainWindow : Window
    {

        ExternalClass externalClass;

        public MainWindow()
        {
            InitializeComponent();
            externalClass = new ExternalClass();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Console.WriteLine($"Button Click Event Fired.");
            externalClass.Name = "Some Name";
            externalClass.ExternalClassMethod();
        }
    }
    public partial class ExternalClass
    {
        public string Name { get; set; }
        //  The access modifier is "public" to enable access from external types.
        public void ExternalClassMethod()
        {
            Console.WriteLine($"ExternalClassMethod executed.  Name = {Name}");
        }
     }
}

Upvotes: 1

Related Questions