WPF - 如何获取 MainWindow 实例?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19647375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF - How do I get the MainWindow instance?
提问by Larry Holze
I have this in MainWindow.xaml.cs:
我在 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:
WPF 和 C# 非常新,但下面的 (MainWindow.xaml) 在我看来是实例化这个类的地方:
<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.
回到 MainWindow.xaml.cs,在 MainWindow 类之外,我想引用在 MainWindow.xaml 中实例化的对象的 _frameCounter 字段,但不知道如何处理该 MainWindow 对象。
回答by J R B
You can access your field with
您可以访问您的领域
Application.Current.MainWindow._frameCounter
回答by SeeSharp
Try this: Application.Current.MainWindow._frameCounter
尝试这个: Application.Current.MainWindow._frameCounter