How do you access Ribbon controls (eg. RibbonEditBox) from outside class? - c#

I have a Ribbon1 class which was created by the Visual Studio.
public Microsoft.Office.Tools.Ribbon.RibbonEditBox IDBox;
I would like to access one of the ribbon controls from within a different class.
using MyRibbon = ExcelAddIn1.Ribbon1;
xlTextID = MyRibbon.IDBox.Text;
I got the error message.
An object reference is required for non-static field, method,
property.
I can't make IDBox static because it's initialized as an instance of a class via 'InitializeComponent()' method.
this.IDBox = this.Factory.CreateRibbonEditBox();
I have also tried to create a property.
private Microsoft.Office.Tools.Ribbon.RibbonEditBox IDBox;
public Microsoft.Office.Tools.Ribbon.RibbonEditBox IDBoxProperty
{
get { return IDBox; }
set { IDBox = value; }
}
Doing this I have seen exactly the same error.
How can I keep IDBox non-static and still access it from outside class?
I can found the answer - see it below.

Instances of the all Ribbon controls derived from Microsoft.Office.Tools.Ribbon can be accessed via Globals.Ribbons.Ribbon1.
Therefore, in order to access public Microsoft.Office.Tools.Ribbon.RibbonEditBox IDBox; which is created by InitializeComponent()method, you would do Globals.Ribbons.Ribbon1.IDBox.
More information about accessing Ribbon Controls at Run-Time:
https://msdn.microsoft.com/en-us/library/bb772088.aspx

Related

Get right instance of VS2017 for self developed extension

I'm developing a Visual Studio Extension to replace text in the current active .cs file using a custom command that is invoked from the right click context menu in the Code Window.
Accessing the document works so far, but if I start more than one instance of VS2017, then changes which I expect to be done in the new instance are made in the first opened instance.
Is there a possibility to get the right instance to access only the current active Document no matter how many instances are open?
At the moment I get the instance with following code:
dte = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal
.GetActiveObject("VisualStudio.DTE.15.0");
Does anybody have an idea how to solve this?
You need to use in the class of your package (that inherits from the AsyncPackage base class):
EnvDTE.DTE dte = (EnvDTE.DTE) base.GetService(typeof(Microsoft.VisualStudio.Shell.Interop.SDTE));
The code that you were using returns some DTE instance running on your system, not necessarily the one where your extension is hosted.
As Carlos Quintero already said, you should get the DTE Object by using his example.
Lets say your extension name is YourExtension:
In my case, I added a Property in my YourExtension.cs
public EnvDTE.DTE DTEObject { get; set; }
Then in YourExtensionPackage.cs you can get the desired DTEObject right after your package got initialized:
protected override void Initialize ()
{
YourExtension.Initialize (this);
base.Initialize ();
YourExtension.Instance.DTEObject = (EnvDTE.DTE)base.GetService (typeof (Microsoft.VisualStudio.Shell.Interop.SDTE));
}
Now you can work with the DTEObject within your extension and get any Object via GetObject. In My case for example I'm getting the current instance of the VersionControlEx.

Access properties in App.xaml.cs from different project UWP

Like what I said in the title, how can we access the property in App in a different project? I want to access it from normal class like a service. Not in viewmodel. Hope we can do something like Application as App.
Access properties in App.xaml.cs from different project UWP
You may try to use one App.xaml.cs for the two projects. For example, if the second project wants to access App.xaml.cs in the first project without reference the first project, you may consider remove the App.xaml.cs which is belonged to the second project, and Add-ExitingItem to add the App.xaml.cs from the first project. In that case, the two projects will share the same App.xam.cs and then you can directly access the properties as Marian Dolinský mentioned.
Otherwise, the two projects may not be able to communicate with each other directly. If the above method is not suit for you, please detail why you need this feature and we may need to consider other ways without accessing the App.xaml.cs.
Method 1
You could cast Application.Current to App:
App app = (App)Application.Current;
app.YourProperty = something;
Method 2
Create some static property holding the reference of App. In my projects I do it by creating a new property called Current as follows:
// in App.xaml.cs
public static new App Current { get; private set; }
public App()
{
Current = this;
// Another code
}

Application.Current <- how does it work?

I am going through some WPF example I found.
I have a class here which is inherited from Application:
public partial class DataBindingLabApp : Application
{
private ObservableCollection<AuctionItem> auctionItems = new ObservableCollection<AuctionItem>();
public ObservableCollection<AuctionItem> AuctionItems
{
get { return this.auctionItems; }
set { this.auctionItems = value; }
}
}
As you can see this class have a property called AuctionItems.
Because it inherits from Application it also contains property called 'Current' which provides access to the Application instance (according to MSDN).
Then in the code I have:
((DataBindingLabApp)Application.Current).AuctionItems.Add(item);
I do not understand it.
Since we can have many classes which may inherit from Application then how we know that Application.Current actually contains object of class 'DataBindingLabApp'?
Thank you!
Because Visual Studio generates entry point in the partial generated class of custom application type(DataBindingLabApp in your case) by default (You can find it by searching in the root directory of solution).
[System.STAThreadAttribute()]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public static void Main() {
DataBindingLabApp app = new DataBindingLabApp();
app.InitializeComponent();
app.Run();
}
And after application has been ran Application.Current contains instanse of DataBindingLabApp.
Since we can have many classes which may inherit from Application
That isn't relevant. What matters is that there is only ever one instance of the Application class. The one-and-only application that's running. Be sure to distinguish types from objects.

Obtaining Solution Name in ToolWindow of VSpackage

I'm new to Visual Studio Extensibility Framework to use VSPackage Extension. I want to obtain a DTE Object inside the user-control which is called inside MyToolWindow class.
I tried all the below possibilities:
1.EnvDTE80.DTE2 dte2;
dte2 = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.10.0");
string solutionPath=dte2.Solution.FullName;
The above trial gives only SolutionPath of the 1st instance of the visual studio which is opened.
For EX: If Math1.sln and Math2.sln is opened,only Math1.sln path is assigned to solutionPath.
From the MSDN link, "this.GetService" cannot be recognized in the button1.click (Am I missing any thing here)
Once, DTE object is obtained I shall be easily be able to obtain solution Name of the current instance.
I must be able to obtain appropriate EnvDTE80.DTE object inside the click of the button which is inside User-control. This User-Control is called inside MyToolWindow.cs
Any help on this would be greatly appreciated.
First of all, Marshal.GetActiveObject("VisualStudio.DTE.10.0") is problematic because it will just give you a DTE of a random Visual Studio instance. You can obtain correct instance of DTE through the GetService call.
Then just enumerate the DTE.Solution.Properties, one of the properties should be the solution name. Probably DTE.Solution.Properties.Item("Name"), though I cannot check this at the moment.
As for your "GetService cannot be recognized", method is defined on an IServiceProvider which ToolWindowPane implements. If you want to use that in your control, you should pass the tool window instance to your control. Example:
public class MyToolWindow: ToolWindowPane {
void SomeMethod() {
var myControl = new MyControl(this);
}
}
public class MyControl: UserControl {
public MyControl(IServiceProvider serviceProvider) {
// Now you can call serviceProvider.GetService
}
}
I have an abstract VsPackage class I use within all my home-brewed extensions; and I obtain the application instance this way...
using EnvDTE80;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
public abstract class VsPackage : Package
{
...
protected DTE2 GetApplication()
{
return this.GetService(typeof(SDTE)) as DTE2;
}
...
}

Accessing HttpApplication.Application variables from a class

I set up various global parameters in Global.asax, as such:
Application["PagePolicies"] = "~/Lab/Policies.aspx";
Application["PageShare"] = "/Share.aspx";
Application["FileSearchQueries"] = Server.MapPath("~/Resources/SearchQueries.xml");
...
I have no problem accessing these variables form .ascx.cs or .aspx.cs file -- ie. files that are part of the Web content. However, I can't seem to access 'Application' from basic class objects (ie. standalone .cs files). I read somewhere to use a slight variations in .cs files, as follows, but it always comes throws an exception when in use:
String file = (String)System.Web.HttpContext.Current.Application["FileSearchQueries"];
While it's true that you can use HttpContext.Current from any class you must still be processing an HTTP request when you call it - otherwise there is no current context. I presume that's the reason you're getting an exception, but posting the actual exception would help clarify matters.
to share your variable across app, and to be able to access it from standalone class, you can use static variable of a class, instead of using HttpApplication variable.
public MyClass{
public static int sharedVar;
}
//and than you can write somwhere in app:
MyClass.sharedVar= 1;
//and in another location:
int localVar = MyClass.sharedVar;

Categories

Resources