Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - How do I get the MainWindow instance?

Tags:

wpf

xaml

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.

like image 405
Larry Holze Avatar asked Oct 29 '13 00:10

Larry Holze


People also ask

How do I run a specific window in WPF?

Just delete the StartupUri="MainWindow. xaml" attribute in App. xaml , Add a Program class to your project containing a Main method, and then go to the project properties and set the startup object to YourAssemblyName.

How do I change the startup window in WPF?

If you look at App. xaml class of your WPF application, you will see the following XAML code. Here the StartupUri sets the startup Window of an application. If you want to change the Startup window to some other window, just change this value.

Where does the execution start in a WPF application?

For a WPF standalone application that is generated in Visual Studio using the New Project wizard, the entry point for the application is the Main function, defined in App. g. cs (generated code). In the default project, this is the public static void App.

What is the main window in Visual Basic?

The Main window is the main way to access other windows, load and save files, control trajectory playback, change various global program settings, access help, and to quit the program. Many of these actions can also be performed with the menu shortcut keys described in Table 5.3.


1 Answers

You can access your field with

Application.Current.MainWindow._frameCounter 
like image 176
J R B Avatar answered Sep 23 '22 11:09

J R B