31

I have this in MainWindow.xaml.cs:

public partial class MainWindow : Window
{
    public double _frameCounter = 0;\;

Very new to WPF and C#, but the below (MainWindow.xaml) appears to me to be where this class is being instantiated:

<Window x:Class="CompositionTargetSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Composition Target Rendering Sample"
        Width="768"
        Height="512">

Back in MainWindow.xaml.cs, outside of the MainWindow class, I want to reference the _frameCounter field of the object instantiated in MainWindow.xaml, but don't know how to address that MainWindow object.

wonea
  • 4,783
  • 17
  • 86
  • 139
Larry Holze
  • 365
  • 1
  • 4
  • 7

3 Answers3

46

You can access your field with

Application.Current.MainWindow._frameCounter
Jim Balter
  • 16,163
  • 3
  • 43
  • 66
J R B
  • 2,069
  • 4
  • 17
  • 32
13

Try this: Application.Current.MainWindow._frameCounter

Application.MainWindow MSDN

SeeSharp
  • 1,730
  • 11
  • 31
3

This works for me

Window.GetWindow(App.Current.MainWindow) as MainWindow