qxshower
qxshower
Reputation Top 1%
Xiao Qu
87 Snippets  (6th place)
Published
4 Channels
Created
1 Channel
Following
10566 points  (9th place)
Reputation
Top 1% Publisher
Junior Code Generator
Serious Code Generator
Senior Code Generator
Master Code Generator
Guru Code Generator
Junior Publisher
Senior Publisher
Master Publisher
Junior Topic Creator
Junior Topic Hub
Serious Topic Hub
Junior Trend Maker
Serious Trend Maker
Senior Trend Maker
Master Trend Maker
Junior Popular Coder
Serious Popular Coder
Master Popular Coder
Guru Popular Coder
Junior Autobiographer
Senior Autobiographer
Master Famous Coder
Guru Famous Coder
Serious Wise Coder

Recent Snippets See all snippets by qxshower

public by qxshower  1721  2  7  1

GetEmbeddedStringResource

Gets the embedded file identified by the resource name, and converts the file into a string.
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VSSDK.Tools.VsIdeTesting;
using SteveCadwallader.CodeMaid.Helpers;
using System;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;

#region Methods: Handling embedded resources

/// <summary>
/// Gets the embedded file identified by the resource name, and converts the file into a string.
/// </summary>
/// <param name="assembly"></param>
/// <param name="resourceName">In VS, is DefaultNamespace.FileName?</param>
/// <returns></returns>
public static string GetEmbeddedStringResource(Assembly assembly, string resourceName)
{
    string result = null;

    // Use the .NET procedure for loading a file embedded in the assembly
    Stream stream = assembly.GetManifestResourceStream(resourceName);
    if (stream != null)
    {
        // Convert bytes to string
        byte[] fileContentsAsBytes = new byte[stream.Length];
        stream.Read(fileContentsAsBytes, 0, (int)stream.Length);
        result = Encoding.Default.GetString(fileContentsAsBytes);
    }
    else
    {
        // Embedded resource not found - list available resources
        Debug.WriteLine("Unable to find the embedded resource file '" + resourceName + "'.");
        Debug.WriteLine("  Available resources:");
        foreach (string aResourceName in assembly.GetManifestResourceNames())
        {
            Debug.WriteLine("    " + aResourceName);
        }
    }

    return result;
}                                    
;

public by qxshower  1875  2  5  0

FindTreeViewItemRecursively

Attempts to find a TreeViewItem recursively that is the container for the specified content to find. Source: http://blogs.msdn.com/b/wpfsdk/archive/2010/02/23/finding-an-object-treeviewitem.aspx.
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using System.Windows.Media;

/// <summary>
/// Attempts to find a TreeViewItem recursively that is the container for the specified
/// content to find.
/// </summary>
/// <remarks>
///
/// Source: http://blogs.msdn.com/b/wpfsdk/archive/2010/02/23/finding-an-object-treeviewitem.aspx.
/// </remarks>
/// <param name="itemsControl">The items control.</param>
/// <param name="contentToFind">The content to find.</param>
/// <returns>The matching TreeViewItem, otherwise null.</returns>
private static TreeViewItem FindTreeViewItemRecursively(ItemsControl itemsControl, object contentToFind)
{
    if (itemsControl == null)
    {
        return null;
    }

    if (itemsControl.DataContext == contentToFind)
    {
        return itemsControl as TreeViewItem;
    }

    ForceItemsControlToGenerateContainers(itemsControl);

    for (int i = 0; i < itemsControl.Items.Count; i++)
    {
        var childItemsControl = itemsControl.ItemContainerGenerator.ContainerFromIndex(i) as ItemsControl;
        var result = FindTreeViewItemRecursively(childItemsControl, contentToFind);
        if (result != null)
        {
            return result;
        }
    }

    return null;
}            
;

public by qxshower  1392  0  6  0

HandleDragScrolling

Handles scrolling the specified scroll viewer if the drag event indicates the drag operation is nearing the scroll viewers top or bottom boundaries.
using SteveCadwallader.CodeMaid.Helpers;
using SteveCadwallader.CodeMaid.Integration;
using SteveCadwallader.CodeMaid.Logic.Reorganizing;
using SteveCadwallader.CodeMaid.Model.CodeItems;
using SteveCadwallader.CodeMaid.Properties;
using SteveCadwallader.CodeMaid.UI.Enumerations;
using System;
using System.ComponentModel.Design;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

/// <summary>
/// Handles scrolling the specified scroll viewer if the drag event indicates the drag
/// operation is nearing the scroll viewers top or bottom boundaries.
/// </summary>
/// <param name="scrollViewer">The scroll viewer.</param>
/// <param name="e">
/// The <see cref="System.Windows.DragEventArgs" /> instance containing the event data.
/// </param>
private static void HandleDragScrolling(ScrollViewer scrollViewer, DragEventArgs e)
{
    const int threshold = 20;
    const int offset = 10;

    var mousePoint = e.GetPosition(scrollViewer);

    if (mousePoint.Y < threshold)
    {
        scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - offset);
    }
    else if (mousePoint.Y > (scrollViewer.ActualHeight - threshold))
    {
        scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + offset);
    }
}            
;

public by qxshower  1539  0  5  0

GetInstance

Gets an instance of a class from its hosting package.
using EnvDTE;
using System.Linq;

/// <summary>
/// The singleton instance of the <see cref="CommandHelper" /> class.
/// </summary>
private static CommandHelper _instance;

/// <summary>
/// Gets an instance of the <see cref="CommandHelper" /> class.
/// </summary>
/// <param name="package">The hosting package.</param>
/// <returns>An instance of the <see cref="CommandHelper" /> class.</returns>
internal static CommandHelper GetInstance(CodeMaidPackage package)
{
    return _instance ?? (_instance = new CommandHelper(package));
}            
;

public by qxshower  1265  0  5  0

IsExternal

Determines if the specified project item is external to the solution.
using EnvDTE;
using System;
using System.Linq;

/// <summary>
/// Determines if the specified project item is external to the solution.
/// </summary>
/// <param name="projectItem">The project item.</param>
/// <returns>True if the project item is external, otherwise false.</returns>
internal static bool IsExternal(this ProjectItem projectItem)
{
    try
    {
        if (projectItem.Collection == null || !projectItem.IsPhysicalFile())
        {
            return true;
        }

        return projectItem.Collection.OfType<ProjectItem>().All(x => x.Object != projectItem.Object);
    }
    catch (Exception ex)
    {
        OutputWindowHelper.ExceptionWriteLine("Unable to determine if ProjectItem is external", ex);
        return true;
    }
}            
;