需要帮助我的项目 [英] Need help on this for my Project

查看:70
本文介绍了需要帮助我的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private TabItem AddTabItem()
       {
           TabItem tab = new TabItem();



               int count = _tabItems.Count;
               for (int i = 0; i < count; i++)
               {

                   // create new tab item


                   tab.Header = string.Format("Tab {0}", count);
                   tab.Name = string.Format("tab{0}", count);
                   tab.HeaderTemplate = tabDynamic.FindResource("TabHeader") as DataTemplate;

                   WebBrowser wb = new WebBrowser();
                   tab.Content = wb;
                   wb.Source = new Uri("http://www.google.com");


                   // insert tab item right before the last (+) tab item
                   _tabItems.Insert(count - 1, tab);
               }


           return tab;


       }





所以基本上我只需要一种方法如何获取要在此方法之外使用的新实例化wb变量。这行代码适用于支持多个标签的互联网浏览器程序。好消息是,它的新功能是我的浏览器上的每个标签包含相同的链接,我的意思是每个标签都是相同的,如果我在一个标签上登录我的Facebook它将是相同的另一个选项卡,我只需要一种方法将每个选项卡转换为单个选项卡。我已经尝试将WebBroswer实例变量 wb 作为全局变量,但是讨厌导致问题的原因,而且我无法将我的Refresh,Forward和Backward按钮分配给此变量,因为它在此方法中。所以我认为它要么找到一种方法使 wb 变量成为全局变量(如果可能的话),以便我可以分配我的按钮,如刷新等。它在各自的标签上。这是我需要完成工作的最后一步:(请尽快帮助我只剩下一个fwe小时



So basically i just need a way on how to get the new instantiated "wb" variable to be used outside this method. this line of code is for my internet browser program that supports multiple tabs. the good news is that it works the bad new is that each tab on my browser contains the same link, and what i mean by that is every tab is identical like if i log in to my Facebook on one tab it will be the same for the other tab, i just need a way to turn each tab to an individual tab. I have tried making the WebBroswer instance variable wb as a global variable but hats what causes the problem, plus i cant assign my Refresh,Forward and Backward buttons to this variable because its inside this method. So i thought that its either i find a way to make the wb variable to become global (if possible) so that i could assign my buttons like refresh etc,. with it being on its own individual tab. This is the last step i need to finish my work :( please Help ASAP i only got a fwe hours left

推荐答案

你好朋友,

如果要从调用 AddTabItem()的方法访问 wb 对象,那么我建议声明 wb 来自调用方法的对象并将对象作为参数传递给 AddTabItem()函数。



在此方式,您将能够从此方法之外访问 wb

- DD
Hello friend,
If you want to access wb object from the method where AddTabItem() is called, then I would suggest to declare the wb object from calling method and pass the object as parameter to AddTabItem() function.

In this way, you'll be able to access wb from outside of this method.
- DD


C#没有全局变量(虽然静态关键字和Singleton模式确实为您提供了模拟它们的方法) - 通常,您不需要它们。



相反,只需移动声明:

C# doesn't have Global variables (though the static keyword and the Singleton pattern do give you ways to emulate them) - generally, you don't need them.

Instead, just move the declaration:
private WebBrowser wb = new WebBrowser();
private TabItem AddTabItem()
          {
                TabItem tab = new TabItem();
                int count = _tabItems.Count;
                for (int i = 0; i < count; i++)
                {
                     // create new tab item
              tab.Header = string.Format("Tab {0}", count);
                    tab.Name = string.Format("tab{0}", count);
                    tab.HeaderTemplate = tabDynamic.FindResource("TabHeader") as DataTemplate;
 
                    tab.Content = wb;
                    wb.Source = new Uri("http://www.google.com");
                    // insert tab item right before the last (+) tab item
                    _tabItems.Insert(count - 1, tab);
                }
            return tab;
        }

并且 wb 将在整个课程中提供(但不在课堂之外,这是一件好事,老实说!)

And wb will be available throughout the class (but not outside it, which is a good thing, honest!)


你在所有选项卡上都得到了相同的webbrowser,因为这正是你的代码编写的内容。

你从零到你的标签计数,每个步骤在最后位置插入一个新的Tab,其中包含与每个其他选项卡相同的web浏览器。



您应该在AddTabItem()方法的开头添加一个断点,在调试模式下运行它;你会立刻明白发生了什么。



最后,如果你需要保留几个webbrowsers,每个标签一个,你需要声明一个集合他们;你可以使用数组,列表,字典等等,但你需要一个集合。

示例:

You get the same webbrowser on all your tabs because this is exactly what your code is written to do.
You iterate from zero to your tabs count, and at each step insert in last position a new Tab with the same webbrowser than on every other tab.

You should really put a breakpoint at the beginning of your AddTabItem() method and run it in debug mode; you would instantly understand what is going on.

Finally, if you need to keep several webbrowsers, one for each tab, you need to declare a collection of them; you could use an Array, a List, a Dictionary, whatever, but you need a collection.
Examples:
private List<WebBrowser> _webBrowsers = new List<WebBrowser>();
// OR
private Dictionary<int, WebBrowser> _webBrowsers = new Dictionary<int, WebBrowser>();





但是无论如何,这里的优先级是纠正你的AddTabItem()方法。调试它,逐行执行,并在每一步检查变量的值。之后解决方案很容易实现,您将学习开发人员生活中的重要部分:调试。



But anyway the priority here is to correct your AddTabItem() method. Debug it, execute it line by line, and check for the values of your variables at each step. The solution will come easily after that, and you will have learned an important part of a developer's life : debugging.


这篇关于需要帮助我的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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