28
votes

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.

3
May i know your need of having main-window object?Kumareshan

3 Answers

42
votes

You can access your field with

Application.Current.MainWindow._frameCounter
12
votes

Try this: Application.Current.MainWindow._frameCounter

Application.MainWindow MSDN

0
votes

This works for me

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