VS 2010 addin: getting selected text in the editor - visual-studio-2010

Coders, I am developing an add in for VS2010 and I am trying to get the selected text in the code editor. so far, i have been searching many webpages and thy all seems to use DTE.ActiveDocument which causes an error in my code. I have written two versions of a method that suppose to return a selected text in the editor but I still get the same error over and over:
the error is: An object reference is required for the non-static field, method, or property 'EnvDTE._DTE.ActiveDocument.get'
and here are my two versions of the method (only relevant code is showen):
using EnvDTE;
private string getSelectedText_V1()
{
string selectedText = string.Empty;
/*PROBLEM HERE: An object reference is required for the non-static field, method, or property 'EnvDTE._DTE.ActiveDocument.get'*/
Document doc = DTE.ActiveDocument;
return selectedText;
}
private string getSelectedText_V2()
{
string selectedText = string.Empty;
/*PROBLEM HERE: An object reference is required for the non-static field, method, or property 'EnvDTE._DTE.ActiveDocument.get'*/
EnvDTE.TextSelection TxtSelection = DTE.ActiveDocument.Selection;
return selectedText;
}
Please help me figure out what i did wrong in my code?

If you have access to GetService() method in your addin, you could add:
DTE dte = this.GetService(typeof(DTE)) as DTE;
Then your code would become:
private string getSelectedText_V1()
{
string selectedText = string.Empty;
DTE dte = this.GetService(typeof(DTE)) as DTE;
Document doc = dte.ActiveDocument;
return doc.Selection.Text;
}

Related

VSTO Outlook: How to get the StoreID from a Outlook.MailItem

I have several accounts configured in Outlook. Now I am trying to get the StoreID of an Outlook mailItem. For example if I select a message from the inbox folder of an account, let's say, account1, I want to get its StoreID, and if I select a message from the inbox folder of another Outlook account, let's say, account2, I want to get the its corresponding StoreID.
I have created an extension method to get the StoreID:
public static string GetStoreID(this Outlook.MailItem omi)
{
Outlook.Folder folder = null;
Outlook.Store store = null;
string storeID = null;
try
{
folder = (Outlook.Folder)omi.Parent;
store = folder.Store;
storeID = store.StoreID;
}
catch (Exception ex)
{
Log.Error("GetStoreID: An error occurred while getting mail item storeID. " + ex.ToString());
}
finally
{
folder = null;
store = null;
}
return storeID;
}
Is it correct or is there any other way which is better?
Your method is fine. Or you can make it even faster - simply retrieve the PR_STORE_ENTRYID MAPI property (DASL name "http://schemas.microsoft.com/mapi/proptag/0x0FFB0102") using MailItem.PropertyAccessor.GetProperty and convert it to a string using MailItem.PropertyAccessor.BinaryToString.
Your code works, so it appears you are asking for any other approach you could take to get the StoreID string.
Whether one approach is "better" than any other approach is purely subjective and entirely context specific.
Another way you could do this, would be to instead get the StoreID from the Explorer object (rather than the MailItem). If you have an "Explorer Wrapper" class in your add-in, you could have something like the following for your backing-fields and constructor:
private Outlook.Explorer _myExplorer;
private Outlook.Folder _myFolder;
private Outlook.Store _myStore;
private string _myStoreID;
public OutlookExplorerWrapper(Outlook.Explorer explorer)
{
_myExplorer = explorer;
_myFolder = (Outlook.Folder)_myExplorer.CurrentFolder;
_myStore = _myFolder.Store;
_myStoreID = _myStore.StoreID;
}

In Xamarin.Forms Shell How do I add menuitems programatically

In Xamarin Forms v4.0.0.394984 pre10 using Visual Studio 2017 I was able to add MenuItems to the Shell programatically in the constructor of the Shell using the MenuItemsCollection MenuItems.
Here is the code that works in v4.0.0.394984 pre10 (simplified it for this question to add a single hard-coded menuitem and function LoadView is not shown here)
public partial class Shell : Xamarin.Forms.Shell
{
public ICommand cmdLoadView { get; } = new Command(LoadView);
public Shell()
{
InitializeComponent();
BindingContext = this;
MenuItem mi = new MenuItem();
mi.Command = cmdLoadView;
mi.CommandParameter = "myCommand";
mi.Text = "sampleName";
MenuItems.Add(mi);
}
...
}
In all subsequent versions, this code does not work. Intellisense indicates that MenuItems is still part of the Shell, but I get a compilation error saying:
error CS0103: The name 'MenuItems' does not exist in the current context.
When I reference as this.MenuItems I get the compilation error:
error CS1061: 'Shell' does not contain a definition for 'MenuItems' and no accessible extension method 'MenuItems' accepting a first argument of type 'Shell' could be found (are you missing a using directive or an assembly reference?)
Is this possible in the current version of Xamarin.Forms? I've tried with each of the releases and pre-releases since v4 pre10 and none have worked.
Thanks is advance for any help!
You need to add the MenuItem objects to the Current.Items property in your AppShell implementation. Example:
// factory method to create MenuItem objects
private static MenuItem CreateMenuItem(string title, ICommand cmd)
{
var menuItem = new MenuItem();
menuItem.Text = title;
menuItem.Command = cmd;
return menuItem;
}
public AppShell()
{
...
// you can place this code in any method in the AppShell class
Current.Items.Add(CreateMenuItem("AppInfo", new Command(async () =>
{
ShellNavigationState state = Shell.Current.CurrentState;
await Shell.Current.Navigation.PushAsync(new AppInfoPage());
Shell.Current.FlyoutIsPresented = false;
})));
...
}

XPathDocument Error Will not write to file c#

private void SetValueToDocumentByXPath(XPathDocument doc, string xpath, string value)
{
var nav = doc.CreateNavigator();
var it = nav.Select(xpath, nameSpaceManager_);
if (it.MoveNext())
{
it.Current.SetValue(value);
}
}
Since I have changed the document coming in as a parameter I am getting this error:
Additional information: Specified method is not supported.
I would like to Edit the doc coming into the function

Passing LINQ element as parameter to another .xaml wp7

Is it possible to send the result of a LINQ query as parameter to another .xaml file in WP7.
If yes then can you please explain via an example.
Thanks in advance
Here is my code
XDocument xml = XDocument.Load("VideoContent.xml");
var content = from query in xml.Descendants("Video") where (string)query.Element("Clip") == parameter
select new Video() { File = (string)query.Element("File") }
Now I need to pass the string in File to another .xaml using NAvigationService.
P.S I am very new to WP7 and LINQ
If you want to pass the string value, you can pass it as the Navigation parameter.
NavigationService.Navigate(new Uri("/NewPageName.xaml?file="+content.First().File, UriKind.Relative));
and then in the OnNavigatedTo handler of the New Page, get the 'file' string value like this
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
string file; //declare this before the page constructor
NavigationContext.QueryString.TryGetValue("file", out file);
}

How to detect in code if property is decorated with HiddenInput

I have a view where I need to detect if a property is decorated with hidden input.
My property is defined as:
[HiddenInput(DisplayValue = false)]
public string UserName{ get; set; }
My attempt so far has been:
var column.Member = "UserName";
if (ViewData.ModelMetadata.HideSurroundingHtml == true &&
ViewData.Values.Contains(column.Member))
{
column.Visible = false;
}
I have read that I might be able to use "HideSurroundingHtml" to determine if the property should not be displayed.
Any ideas how to detect this?
You can use reflection to see if a specific property has an attribute.
Look at this question.
In the various answers a user also posted a snippet to create an extension method to check if a property has a specific attribute or not. Hope it helps
My solution to this problem is as follows:
I have created html helper that gives me array of names with properties that has been decorated with the "HiddenInput" attribute.
public static string[] GetListOfHiddenPropertiesFor<T>(this HtmlHelper htmlHelper)
{
Type t = typeof(T);
var propertyInfos = t.GetProperties()
.Where(x => Attribute.IsDefined(x, typeof(HiddenInputAttribute)))
.Select(x => x.Name).ToArray();
return propertyInfos;
}
this is all i needed

Resources