秒表访问cs文件之间的变量 [英] Stopwatch access variables between cs files

查看:56
本文介绍了秒表访问cs文件之间的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个名为MainWindow和Window1的窗口。有一些方法可以启动(按钮点击(btn_Start_Click)之后)并在MainWindow中的文本块(display_1)内显示计时器(点击btn_Stop_Click之后)。我在Window1中有停止按钮。我想最后显示这个时间(在window1中单击停止按钮后)并在Window1中显示它。但是,当我在Window1中单击btn_Stop_Click时,它不起作用。如何在Window1中最终显示时间(点击停止按钮后)?谢谢。

以下代码如下:



我尝试过:



MainWindow.xaml.cs:

I have two windows called MainWindow and Window1. There are methods which starts(after button click(btn_Start_Click)) and displays timer(after click btn_Stop_Click) inside the textblock(display_1) in MainWindow. And I have stop button in Window1. I want to display this finally time(after stop button clicked in window1) and display this in Window1. But, when I click btn_Stop_Click in Window1 it doesnt work. And how can I display finally time(after click stop button) in Window1? Thank you.
Here is the following code:

What I have tried:

MainWindow.xaml.cs:

namespace WpfApplication9
{
    public partial class MainWindow : Window
    {
        DispatcherTimer dt = new DispatcherTimer();
        Stopwatch sw = new Stopwatch();
        string currentTime = string.Empty;
 
        public MainWindow()
        {
            InitializeComponent();
            dt.Tick += new EventHandler(dt_Tick);
            dt.Interval = new TimeSpan(0,0,0,0,1);
        }
 
        public void dt_Tick(object sender, EventArgs e)
             {
                if (sw.IsRunning)
                {
                    TimeSpan ts = sw.Elapsed;
                    currentTime = String.Format("{0:00}:{1:00}:{2:00}",
                    ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
                    display_1.Text = currentTime;
                }
            }
 
        public void Stop_time()
        {
            if (sw.IsRunning)





Window1.xaml.cs:



Window1.xaml.cs:

namespace WpfApplication9
{
 
    public partial class Window1 : Window
    {
 
        public Window1()
        {
            InitializeComponent();
        }
 
        private void btn_Stop_Click(object sender, RoutedEventArgs e)
        {
            MainWindow ob = new MainWindow();
            ob.Stop_time();
        }
 
        private void btn_Close_Click(object sender, RoutedEventArgs e)
        {
            App.Current.Shutdown();
        }
 
        private void btn_show_Click(object sender, RoutedEventArgs e)
        {
 
        }
 

    }





谢谢!



Thank You!

推荐答案

嗯,从哪里开始?

First让我们回顾一下对象和实例。

执行此操作时:

Um, where to start?
First let's review objects and instances.
When you do this:
MainWindow ob = new MainWindow();

您创建一个新的MainWindow的品牌打屁股,未使用的实例。 new 关键字是一个很大的线索。这与启动计时器的实例不同。就像汽车一样:如果你将手机放在汽车的手套箱中,然后出去买一辆新车,你会期望在它的手套箱里找到你的手机吗?不 - 它在你的旧车上,而不是新车!

所以当你这样做时:

You create a brand spanking, unused instance of a new MainWindow. The new keyword is a big clue here. This is not the same as the instance that started the timer. It's like cars: if you put your mobile in the glove box of your car, then go out and buy a new vehicle, would you expect to find your mobile in it's glove box? No - it's in your old car, not the new!
So when you then do this:

ob.Stop_time();

你要停止的计时器是在MainWindow的另一个实例中(在另一辆车的手套箱中),所以停止计时器没有任何用处 - 它从来没有开始!

要做到这一点,你需要访问启动计时器的MainWindow的实际实例,并且(可能)创建并显示Window1。



这使我们整齐地回到第二个坏主意!你要做的是将两个窗口联系在一起 - 如果它工作,你不能对MainWindow进行任何更改而不考虑它是否会影响Window1,反之亦然。那很糟。从关注点的分离,从OOP的角度,从可重用性的角度,以及从一般的能够维持的吸盘的角度来看,这是不好的。不要这样做!永远不要通过属性访问其他表单上的控件或其他任何内容,并且永远不要认为创建您的窗口是特定类型。

而是在WindowW中创建一个MainWindow处理的事件,它停止计时器本身。

这听起来很难,但事实并非如此 - 实际上非常非常简单。

看看这里:在两个表格之间传递信息,第2部分:孩子到父母 [ ^ ]它是WinForms,而不是WPF,但技术(甚至代码)是相同的 - 你这次不需要属性。

The timer you are stopping is in a different instance of MainWindow (in the glovebox of a different car) so stopping the timer does nothing useful - it was never started in the first place!
To do that, you would need to access the actual instance of MainWindow that started the timer and (presumably) created and displayed the Window1.

And that leads us neatly to the second bad idea! What you are trying to do ties the two windows together - if it worked, you couldn't make any changes to MainWindow without considering if it will impact Window1 and vice versa. That's bad. It's bad from a separation of concerns point of view, from an OOPs point of view, from a reusability point of view, and from a general being-able-to-maintain-the-sucker point of view. Don't do it! Never access controls or anything else on another form except via a property, and never assume that the window that created you is of a specific type.
Instead, create an event in Window1 that MainWindow handles, and it stops the timer itself.
That sounds difficult, but it isn't, not really - it actually very, very simple to do.
Have a look here: Transferring information between two forms, Part 2: Child to Parent[^] it's WinForms, not WPF, but the technique (and even the code) is the same - you just don't need the property this time.


这篇关于秒表访问cs文件之间的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆