2012年11月23日 星期五

http://www.codeproject.com/Articles/43025/A-LINQ-Tutorial-Mapping-Tables-to-Objects

http://www.codeproject.com/Articles/43025/A-LINQ-Tutorial-Mapping-Tables-to-Objects

2012年11月22日 星期四

MessageBox for confirmation

Code snipplet:


 var messageBoxResult = MessageBox.Show(AppResources.TextDeleteConfrimation,  
             AppResources.TextDelete, MessageBoxButton.OKCancel);  
 if (messageBoxResult == MessageBoxResult.OK)  
 {  
   DeleteSomething();  
 }  


2012年11月21日 星期三

my common git commands


git checkout master
git pull --rebase
// switch the working branch to master branch
// then, pull the things back.
// these things includes: the commits on master by your colleagues and
// other branch name created by your colleagues
// Then, update master branch to the latest commit
// combo 技
// 切換到 master, 把其他人做的更動拉回來
// 然後快轉到 master 最新的 commit 狀態
// 拉回來的東西包括了: 其他人在 master 上的 commit 跟其他人創立的 branch name

git checkout -b newBranchName
// 創立一個 local的新的branch 叫做 newBranchName
// 然後切換過去

git branch --track workingLocalBranchName remote/remoteBranchName
// creates a local working branch name, called "workingLocalBranchName"
// which is tracking the remote branch name called "remoteBranchName".
// the remote branch name can be found by "git branch -a"


/*
通常早上做完這兩個指令以後 
就開始寫 code
到下午, code改完了, bug修掉了以後 ...
*/

git gui
// it is better to do the commit and push here
// 最好在這裡做 commit 跟 push

gitk
// see the history of commits of the current branch
// each commit is identifed with unique SHA string, which can be found in this window
// 看目前 branch 的歷史紀錄
// 每一次 commit 都有一個 SHA id 對應
// 在這個畫面裡面可以看到每次 commit 的SHA id

/*
做 patch 了, 大家來 review
*/

git format-patch -1 --full-index
// if your modification only requires 1 commit
// this command is quicker
// 如果你只 commit 了一次
// 這個指令會做出這個 commit 的 patch 檔

git diff --full-index oldSHA..newSHA > patchFileName.patch
// diff all the commits from oldSHA to newSHA into the patchFileName.patch
// 把從 oldSHA 到  newSHA 的所有更動放到  patchFileName.patch

git diff --full-index master..localBranchName > xxx.patch
// diff between two branches
// 兩個 branch 做diff

/*
merge 到 master
*/

git cherry-pick SHA
// SHA represents a commit, no matter which branch it is on.
// this command grabs the commit identified by that SHA id,
// and put it on top of the local working branch.
// 把SHA所代表的那個 commit 拉過來, 丟到你打指令的這個 branch

git merge --squash localBranchName
// merge the working branch with localBranchNamebranch
// 把 localBranchName這個 branch 拉過來, merge到你打指令的這個 branch

git merge --squash origin/remoteBranchName
// merge with remoteBranchName branch
// 與遠端的 branch 做 merge

/*
如果 merge 有conflict... 請小心, 沒有指令可以幫你處理好conflict
*/

git reset --hard SHA
// 說對不起, 我想清楚再merge
// 回到SHA這個commit, 在這之後的commit都會不見

git reset --soft SHA
// 說對不起, 我想清楚再merge
// 回到SHA這個commit, 在這之後的commit都會變成已修改狀態

git gui
// 如果修好了conflict 記得要再 commit 一次

/*
其他指令
*/

git checkout localBranchName
// change the working branch to localBranchName branch
// ex: git checkout master
// Before checking out to another branch, there should not be any 
// editting modifications in the working branch.
// Make sure you stash or commit everything you've modified
// 更改你目前的打指令的這個 branch 到 localBranchName 這個 branch
// 更改 branch 之前, 必須確定目前的 branch 是乾淨的
// 所以, 所有的更動請先放到stash或是commit

git checkout -b newLocalBranchName origin/remoteBranchName
// Grab the remoteBranchName into newLocalBranchName
// 把遠端 remoteBranchName 整串拉回來, 放到 newLocalBranchName 


git status
// see the current status of tracked files, 
// or any new files that are not tracked.
// 看目前git 追蹤的檔案裡面, 那些做過更改
// 或是那些檔案沒有被tracked

git stash
// put the current modifications into the stash area. 
// it will 'clean' the current modifications and restore to 
// the lastest commit
// 把目前所做的更動放到stash, 會把目前的更動清掉,
// 恢復到最新的commit的狀態

git stash apply -1
// apply the first stash item from the stash area to the current editting files.
// 把stash裡面第一個東西拉出來, 對目前的檔案做更動

git stash clear
// clear the stash area
// 把 stash 清掉

git push origin:remoteBranchName
git push origin --delete remoteBranchName
// Remove remoteBranchName on the remote side.
// 把遠端的一個叫做 remoteBranchName 的 branch 殺掉

git branch -D localBranchName
// remove the localBranchName branch
// 把本地端的 localBranchName 這個 branch 殺掉




// 新的gerrit server有內建code review的功能
/*
以前都是做patch
丟上review board
過了以後
再cherry-pick到master branch

這個對我來說的新功能是
remote的branch有兩個同名同姓的
只是在不同的refs的HEAD
要先push到HEAD:refs/for/<remoteBranchName>
commit就會出現在review board上了
review過了以後
這個 commit就會跑到 remote/origin/<remoteBranchName>了

*/
git push origin HEAD:refs/for/<remoteBranchName>



git apply --stat XXX.patch   // see the stats of the patch files
git apply --check XXX.patch 
git apply XXX.patch  

// git ignore mode changes:
git config core.filemode false

// list files changed between two SHAs:
git diff --name-only SHA1 SHA2





2012年11月20日 星期二

Share something by email on windows phone 8

In xaml file, let's say we have a button like this:

     <Button x:Name="tellAFriendButton" Width="352" Height="47"   
         Click="OnClickTellAFriendButton"  
         Style="{StaticResource TellAFriendButtonStyle}" Margin="174,241,274,0"   
         Template="{StaticResource TellAFriendControlTemplate}">  
     </Button>  


In c#:

     private void OnClickTellAFriendButton(object aSender,  
         System.Windows.RoutedEventArgs aRoutedEventArgs)  
     {  
       var emailAddressChooserTask = new EmailAddressChooserTask();  
       emailAddressChooserTask.Show();  
       emailAddressChooserTask.Completed +=  
                     EmailAddressChooserTaskCompleted;  
     }  
     void EmailAddressChooserTaskCompleted(object aSender,  
                     EmailResult aEmailResult)  
     {  
       var emailComposeTask = new EmailComposeTask();  
       emailComposeTask.To = aEmailResult.Email;  
       emailComposeTask.Body = ComposeEmailBody();  
       emailComposeTask.Show();  
     }  



Where composeEmailBody() is a function that returns a string, you can implement it in your own needs.


Estimate an App

T-shirt size.



Tasks                            |         Estimate
---------------------------------------------
Pre-development
Task A
Task B
..
..
Development
Task C
Task D
..
Post-development
Task E
Task F
..
------------------------------------------------
Total days

2012年11月11日 星期日

How to load a string from Resource.resw, and add a item to settings panel on Windows 8

The keyword is SettingsPane.

 In OnLaunched() in App.xaml.cs:
 SettingsPane.GetForCurrentView().CommandsRequested += CommandsRequested;  


Then implement CommandsRequested() itself.


     private void CommandsRequested(SettingsPane aSender, SettingsPaneCommandsRequestedEventArgs aEventArgs)  
     {  
       // Retrieve all the strings first  
       var loader = new Windows.ApplicationModel.Resources.ResourceLoader();  
       string settingsCommandId = loader.GetString("aText");  
       string label = loader.GetString("aText");  
       //add the commands to the setting panel  
       aEventArgs.Request.ApplicationCommands.Add(  
         new SettingsCommand(settingsCommandId, label, aMethod));  
 }  



code style generator

http://codeformatter.blogspot.tw/

2012年11月8日 星期四

Pivot item on Windows phone 8


        <phone:Pivot Title="A_title">
            <phone:PivotItem Header="History">
               
            </phone:PivotItem>

            <phone:PivotItem Header="Favorites">

            </phone:PivotItem>

            <phone:PivotItem Header="Scan">

            </phone:PivotItem>

            <phone:PivotItem Header="Creator">

            </phone:PivotItem>

            <phone:PivotItem Header="Settings">

            </phone:PivotItem>
        </phone:Pivot>

2012年11月7日 星期三

Use ZXing library to decode/encode 2D barcode


Using ZXing in c#

How to decode a 2D barcode into text

The code snippet:
try
{
    // Get the width and height of the bitmap, 
    //and change it to a WriteableBitmap object
    int width = mBitmap.PixelWidth; 
    int height = mBitmap.PixelHeight;
    WriteableBitmap writableBitmap = new WriteableBitmap(mBitmap);

    // use RGBLuminanceSource and HybridBinarizer class to
    // process the WriteableBitmap into a BinaryBitmap
    var lsource = new RGBLuminanceSource(writableBitmap, width, height);
    Binarizer zer = new HybridBinarizer(lsource);
    BinaryBitmap binaryBitmap = new BinaryBitmap(zer);

    // Decode the binaryBitmap useing the QRCodeReader object
    QRCodeReader qrReader = new QRCodeReader();
    Result result = qrReader.decode(binaryBitmap);

    // the decoded text in place in the Result object.
    Title.Text = result.Text;
}
catch (Exception ex) // catch the exception here!!!
{
    System.Diagnostics.Debug.WriteLine(ex);
}

How to encode a text into a 2D barcode

The library will encode the text into ZXing-defined ByteMatrix. Here is the code snippet:

string encodeText = TextToEncode.Text;
if (!String.IsNullOrEmpty(encodeText))
{
    // use MultiFormatWriter class to encode.
    var qrCodeWriter = new MultiFormatWriter();
    var matrix = qrCodeWriter.encode(TextToEncode.Text, 
                                     BarcodeFormat.QR_CODE, 400, 400);

    var bitmap = ToBitmap(matrix);
    resultImage.Source = bitmap;
}

Then, ToBitmap() function is used to get the BitmapImage object from the ByteMatrix:

private WriteableBitmap ToBitmap(ByteMatrix aMatrix)
{
    int width = aMatrix.Width;
    int height = aMatrix.Height;
    var bitmap = new WriteableBitmap(width, height);
    for (int x = 0; x < width; x++)
    {
        for (int y = 0; y < height; y++)
        {
            if (aMatrix.get_Renamed(x, y) == -1)
            {
                bitmap.Pixels[y * width + x] =
                   BitConverter.ToInt32(BitConverter.GetBytes(0xffffffff), 0);
            }else
            {
                bitmap.Pixels[y * width + x] =
                   BitConverter.ToInt32(BitConverter.GetBytes(0x00000000), 0);
            }
        }
    }
    return bitmap;
}

2012年10月30日 星期二

windows phone - opening a html file using webBrowser

1:        IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();  
2:        if (isoStore.FileExists(mHtmlFilename))  
3:        {  
4:          Uri fileUri;  
5:          fileUri = new Uri(mHtmlFilename, UriKind.Relative);  
6:          webBrowser.Navigate(fileUri);  
7:        }  

2012年10月11日 星期四

Joining the domain after installing windows 8

1. add your pc into the company's domain.
a. Control panel -> System and Security -> system ->
b. In the row of "Computer name, domain, and workgroup settings -> change settings
c. in System properties dialog, click on Change.. button.
d. enter your pc name and domain name
e. enter your account name and password to that domain.Then you are done! :)

2. Log- in with the user in your company's domain.
a. in log in screen, click on the arrow, (indicating you are loggin in using the user of the domain.)
b. enter the username/ password of the domain.


2012年9月20日 星期四

windows 8 set focus

It seems that there is no setFocus() method anymore. Instead, windows 8 includes the Windows.UI.Xaml.FocusState for different focus state, like: pointer, keyboard, etc.

The usage would be like:
ViewObjectName.Focus(Windows.UI.Xaml.FocusState.Pointer);

2012年9月10日 星期一

Run a program as administrator

http://www.intowindows.com/run-program-as-administrator-in-windows-8/


1. press windows to go to start screen
2. open search screen to search the program that you want to run. (i.e. cmd.exe)
3. the search result will be shown on the left hand side.
4. right click on it (tile of cmd.exe)
5. click on advanced
6. check run as adminstrator

2012年9月3日 星期一

Launch Browser with an URI from a win8 app

http://zubairahmed.net/?p=266

Here is the code snippet:
 Uri justUri = new Uri("http://www.google.com");  
 await Windows.System.Launcher.LaunchUriAsync(justUri);  

2012年8月12日 星期日

metro style -- switch to a new page

In metro style, when switching from a page to another page.
If the new page is a blank page when you add it into your project.


We usually do like this:


 this.Frame.Navigate(typeof(NewsPage), itemId);  



On the opening page, we need to implement onNavigateTo() method to do the corresponding jobs. Most cases, it is crucial to use the parameter that is passing in, i.e. itemId. here is an example of how to do it.



 protected override void OnNavigatedTo(NavigationEventArgs e)  
     {  
       base.OnNavigatedTo(e);  
       Story article = e.Parameter as Story;  
       BitmapImage bmpimg= getCoverImage(article);  
       if (null == bmpimg)  
       {  
         // todo: show empty image...  
       }  
       else  
       {  
         picture.Source = getCoverImage(article);  
       }  
 }  



or if it is a detail page when adding it,

put the code in LoadState(){}

2012年6月7日 星期四

Cannot find _ASSERTE

The reason for this is, I have a crtdbg.h in my project. So, #include crtdbg.h goes to my own crtdbg.h.
Make sure the include path is correct!

2012年5月27日 星期日

2012年5月22日 星期二

Visual Studio改compile flag...

This is for my own:

When doing porting, i.e. from WinCE to Win32, some Macro flags will not be found. When this happens:
1. Find the .vcproj file
2. in the file, find "InheritedPropertySheets"
3. open those .vsprops files indicated in step 2.
4. modify those suspecious flags in the files found step 3.

2012年5月16日 星期三

Compiling flags used in porting from wince to win32

Today I was porting the code from WinCE platform to Win32 environment.
I used the following flag, since I am lazy and not sure which flag is correct for Win32:

 #ifdef WIN32 || __WIN32 || __WIN32__  
 #include <windows.h>                                                                           // Mod: 2007.11.05 S.Ota  
 #endif  


Things like that...

jni and javah

1. javah usage:

    type this command:
javah -jni com.XXX.XXX.className
    It will generate a .h file. Using this .h file, we can start to edit the corresponding .cpp file.


2. JNI unsatisfied link error

    When writing JNI programs, sometimes we see this errors. It is hard and troublesome to find which part of the code actually went wrong. My experience is to check the following:

  • check .h and .cpp files, and see if they are consistent.
  • focus on the parameters passed from java side. Check if the parameter type and number of those parameters are the same.
  • Check the function name in .h/.cpp files. It should be like this:

Java_packageName_className_functionName()




2012年5月10日 星期四

building cross platform apps - environment and SDKs

A web server:
XAMPP - http://www.apachefriends.org/zh_tw/xampp.html

Sencha touch 2 framework:
http://www.sencha.com/products/touch/

IDE for html/javascript/...etc
http://www.aptana.com/



Download those files, then you can begin to write the cross-platform apps using javascript and html5.

cheers

2012年5月8日 星期二

XAMPP -- see only "it works"

When that happens, please check if IIS is also installed.
If you didnt install IIS, please kill all the httpd.exe from windows task manager.
Then, it should work.

At least, it worked on my PC. :)

2012年4月11日 星期三

Android JNI debugging...


1. Add debug symbols when compiling...
add "LOCAL_CFLAGS := -g" in /jni/Android.mk file -


2. Set .apk file debuggable ...
in AndroidManifest.xml, set Debuggable = true













3. In cygwin, add adb into PATH variable (if you're using windows) ...
Modify /home//.bash_profile
example:

4. Clean the project ...
1) in eclipse -
2) in cygwin, ndk-build clean

5. compile the .c files

6. In windows cmd.exe, type: "android update project -p . -s"

7. in eclipse IDE, press the debug button to start debugging ...
Debug configurations must be set properly first.
8. When the application is up and running, type ndk-gdb in cygwin shell...
You will see the following...

Reference:







2012年4月5日 星期四

out of memory exception:

Well, "outofMemoryExceptions" should not be handled, because technically, they can't.
Instead of catching these kind exceptions at low level in the code, they should be thrown up the higher layer, i.e. UI level, so that UI layer is aware of the problem, and user is informed that the app is about to exit.


public class LowLevelClass
{
public void complicatedMethod() throws OutOfMemeoryException
{
throw new OutOfMemeoryException("huh, not so good.");
}
}

public class MiddleLevelClass
{
public void doSomethingNotSoComplicated() throws OutOfMemeoryException
{
LowLevelClass low = new LowLevelClass();
try
{
low.complicatedMethod();
}
catch(OutOfMemeoryException oom)
{
doSomethingNecessary();
throw oom;
}
}
}

public class UILevelClass
{
public void showView throws OutOfMemeoryException
{
MiddleLevelClass mid = new MiddleLevelClass();
try
{
mid.doSomethingNotSoComplicated();
}
catch(OutOfMemeoryException oom)
{
App.reportException(oom);
showDialog("Not enough memory! This App is about to close. Please restart.", oom.getMessage());
}
}
}

[Java] Null pointer handlings

In my previous project, I used to do this when handling the null pointers -
if (obj != null)
{
doSomething();
}


But I was told to do something like this to have a better result -

try
{
doSomething();
}
catch(NullPointerException npe)
{
App.reportException(npe);
}

Unless, the object is allow to have null as a legitimate return value, we can do this -

Bitmap bitmap = ResourceLoader.get(file);
if (bitmap == null)
{
loadBitmapInAnotherWay(file);
}

Well, all these code are conceptually working. You will have to work it out in your own way, according to your architecture. Copying and Pasting these code into yours will just bring you compiling errors. :)