visual studio – how to format whole project files

formatting one single file is simple, find Edit.FormatDocument command in visual studio and click to apply for it. or Ctrl+K Ctrl+D.

customizing C# indent, for example, going Tools | Options | Text Editor | C# | Tabs, on UI user could customize formatting style they want.

However, it is not easy to indent whole project.

In visual studio 2010, before VS macro is removed. we could use following script.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text
 
Public Module Formatting
 
 Dim allowed As List(Of String) = New List(Of String)
 Dim processed As Integer = 0
 Dim ignored As Integer = 0
 Dim errors As StringBuilder = New StringBuilder()
 Dim skippedExtensions As List(Of String) = New List(Of String)
 
 Public Sub FormatProject()
  allowed.Add(".master")
  allowed.Add(".aspx")
  allowed.Add(".ascx")
  allowed.Add(".asmx")
  allowed.Add(".cs")
  allowed.Add(".vb")
  allowed.Add(".config")
  allowed.Add(".css")
  allowed.Add(".htm")
  allowed.Add(".html")
  allowed.Add(".js")
  Try
   recurseSolution(AddressOf processItem)
  Catch ex As Exception
   Debug.Print("error in main loop: " + ex.ToString())
  End Try
  Debug.Print("processed items: " + processed.ToString())
  Debug.Print("ignored items: " + ignored.ToString())
  Debug.Print("ignored extensions: " + String.Join(" ", skippedExtensions.ToArray()))
  Debug.Print(errors.ToString())
 End Sub
 
 Private Sub processItem(ByVal Item As ProjectItem)
  If Not Item.Name.Contains(".") Then
   'Debug.Print("no file extension. ignoring.")
   ignored += 1
   Return
  End If
  Dim ext As String
  ext = Item.Name.Substring(Item.Name.LastIndexOf(".")) 'get file extension
  If allowed.Contains(ext) Then
   formatItem(Item)
   processed += 1
  Else
   'Debug.Print("ignoring file with extension: " + ext)
   If Not skippedExtensions.Contains(ext) Then
    skippedExtensions.Add(ext)
   End If
   ignored += 1
  End If
 End Sub
 
 Private Sub formatItem(ByVal Item As ProjectItem)
  Debug.Print("processing file " + Item.Name)
  Try
   Dim window As EnvDTE.Window
   window = Item.Open()
   window.Activate()
   DTE.ExecuteCommand("Edit.FormatDocument", "")
   window.Document.Save()
   window.Close()
  Catch ex As Exception
   Debug.Print("error processing file." + ex.ToString())
   errors.Append("error processing file " + Item.Name + "  " + ex.ToString())
  End Try
 End Sub
 
 Private Delegate Sub task(ByVal Item As ProjectItem)
 
 Private Sub recurseSolution(ByVal taskRoutine As task)
  For Each Proj As Project In DTE.Solution.Projects
   Debug.Print("project " + Proj.Name)
   For Each Item As ProjectItem In Proj.ProjectItems
    recurseItems(Item, 0, taskRoutine)
   Next
  Next
 End Sub
 
 Private Sub recurseItems(ByVal Item As ProjectItem, ByVal depth As Integer, ByVal taskRoutine As task)
  Dim indent As String = New String("-", depth)
  Debug.Print(indent + " " + Item.Name)
  If Not Item.ProjectItems Is Nothing Then
   For Each Child As ProjectItem In Item.ProjectItems
    taskRoutine(Child)
    recurseItems(Child, depth + 1, taskRoutine)
   Next
  End If
 End Sub
 
End Module

for visual studio 2015, I recommend plugin
https://visualstudiogallery.msdn.microsoft.com/68076712-aea1-4317-ba71-ecf987da415f/view/Reviews

Posted in Uncategorized | Comments Off on visual studio – how to format whole project files

WPF – add key short cut for windows

there is two ways to add wpf key short cut.

1
2
3
4
5
6
7
8
9
        this.KeyUp += MainWindow_KeyUp;
 
        private void MainWindow_KeyUp(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.D1)
            {
                MessageBox.Show("1 is press");
            }
        }

but for the most normal way to do is to wpf resource.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
    <Window.Resources>
        <RoutedUICommand x:Key="ClickCommand" Text="Text" />
    </Window.Resources>
    <Window.InputBindings>
        <KeyBinding Command="{Binding Path=button_Click}" 
                Key="O" 
                Modifiers="Control"/>
        <KeyBinding Key="C" Command="{StaticResource ClickCommand}" />
    </Window.InputBindings>
 
    <Window.CommandBindings>
        <CommandBinding Command="{StaticResource ClickCommand}" 
                    Executed="button_Click" />
    </Window.CommandBindings>
 
    <Grid>
        <Button x:Name="button1" Content="1" HorizontalAlignment="Left" Margin="101,140,0,0" VerticalAlignment="Top" Width="75" Click="button1_Click" Command="{StaticResource ClickCommand}"/>
        <Button x:Name="button" Content="2" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="181,140,0,0" Click="button_Click" Command="{StaticResource ClickCommand}"/>
    </Grid>
Posted in WPF | Comments Off on WPF – add key short cut for windows

xamarin debug android on device and release to apk

this example code could be find on following url
https://github.com/emacslisp/XNA2DGame/tree/dev/Android/PlatformReading

1. How to build apk from visual studio
<1> change build mode from ‘debug’ to ‘release’
<2> select project properties and change option on ‘Application’, ‘Android Manifest’, ‘Android Options’

Android_Setting1

2. How to debug android with xamarin C# code
debug on physical device is the best way to go, simulator is very slow.

choose device and press F5

PlatformReading_Android

Posted in Uncategorized | Comments Off on xamarin debug android on device and release to apk

MonoGame – Personal Project and step by step tutorial

XNA Framework continued to developed by MonoGame.

I migrated my xna game to MonoGame using OpenGL
https://github.com/emacslisp/XNA2DGame

for ‘dev’ is Game using MonoGame with visual studio 2015
in ‘master’ branch is XNA 4.0 using visual studio 2010.

ExtremePong

game_1

Posted in MonoGame | Comments Off on MonoGame – Personal Project and step by step tutorial

c# – NHibernate.MappingException: No persister for: XYZ

nhibernate

as screen dump show, build action of XYZ.hbm.xml should be ‘Embedded Resource’.

Posted in Uncategorized | Comments Off on c# – NHibernate.MappingException: No persister for: XYZ

autofac build

get source code via git clone https://github.com/autofac/Autofac

open then solution Autofac.sln and build.

However, if you look at Reference of Autofac.xproj, it has two References, one is .net 4.5 and another one is .net core 1.0.

the output dll will be two as well, in bin folder, you could see two folders with name of two versions, one dll is for .net 4.5, another one is for .net core 1.0. (very important).
autofac-output

if you add some code (for example, System.Diagnostics.Trace.WriteLine) only supported by .net 4.5, then you could build .net 4.5 dll only.

Posted in autofac | Comments Off on autofac build

.net core – source code analysis starting

.NET Core is a cross-platform free and open-source managed software framework similar to .NET Framework. It consists of CoreCLR, a complete cross-platform runtime implementation of CLR, the virtual machine that manages the execution of .NET programs.

the Major code is developed by C++.

dotnet/cli – for CLI tools and questions
dotnet/corefx – for API issues and questions
dotnet/coreclr – for runtime issues
nuget/home – for NuGet questions and issues
aspnet/home – for ASP.NET Core questions and issues.

It is open source. and developer could investigate deeply under to every detail of C# running environment.

Building corefx and coreclr are almost same.

Step 1: open visual studio 2015 x64 command
Step 2: set cmake to PATH (install cmake if system don’t have)
Step 3: run build.cmd command.

bulid-net_core_cil

Posted in .net core | Comments Off on .net core – source code analysis starting

Visual Studio 2015 with Update 2 Short cut changes

jumping between pair “#region #endregion” short cut is Ctrl+] originally.

However, Now in Visual studio 2015 jump between the pair are changed to Ctrl+Shift+Up/Down.

Ctrl+Shift+8 is not working any more, Ctrl+- has to be used.

I will update more short cut changed for VS2015 comparing to old version such VS2010

Posted in C# | Comments Off on Visual Studio 2015 with Update 2 Short cut changes

C# Reflection – set object enum property value

using C# Reflection can convert DataTable to Object easily.
However, it may be have some problems when converting enum which is special, we don’t know enum type and enum type should get from reflection.

Here is code sample for c# enum property covert.

1
2
3
4
5
6
7
8
var a =  new YourObject(); 
PropertyInfo propertyInfo = a.GetType().GetProperty(s);
if (propertyInfo.PropertyType.IsEnum)
  {
    propertyInfo.SetValue(a, Convert.ChangeType(Enum.Parse(propertyInfo.PropertyType, dr[index++].ToString()), propertyInfo.PropertyType), null);
  }
 else
    propertyInfo.SetValue(a, Convert.ChangeType(dr[index++].ToString(), propertyInfo.PropertyType), null);
Posted in C# | Comments Off on C# Reflection – set object enum property value

some tricky in ng-repeat update model in ionic

ng-repeat is to loop thought object list and auto generated html.

for example
in controller, we declare
$scope.workspaces.

Now $scope.workspaces = getDataFromDatabase();

1
2
3
<li class="workspace-object" ng-repeat="w in workspaces | filter:searchQuery" ng-click="selectWorkspace(w)">
    <a href="" class="workspace-link">{{w.name | titleCase }}</a>
</li>

It will not updated.

Make following change it will work.

1
2
$scope.work={}; //very important, declare object first.
$scope.work.workspaces = getDataFromDatabase(); //workspaces object should under work object.

change ng-repeat to be ng-repeat=”w in work.workspaces | filter:searchQuery”

Posted in Angular.js | Comments Off on some tricky in ng-repeat update model in ionic