2018年7月26日 星期四

Edit column in tableView used View Based

https://stackoverflow.com/questions/28281045/view-based-nstableview-editing

viewbased 沒有辦法使用

setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item

必須綁定每個column,自行產生Action,並且在其函式中判斷table selected index來取得對應物件進行修改

2018年4月30日 星期一

NSThread, GCD

NSThread

有三種方式可以開Thread
  1. NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(test:) object:nil];
  2. [NSThread detachNewThreadSelector:@selector(test:) toTarget:self withObject:@"分离子线程"];
    [self performSelectorInBackground:@selector(test:) withObject:@"后台线程"];

第一種方式可以取得該線程的instance,也可以設定Thread Priority,但要手動啟動線程
第二三種方式直接開啟線程,但無法取得instance
performSelector是比較早期的API,跟NSThreadㄧ樣呼叫一次就開一個線程 
目前Object-C大多使用GCD或是NSOperationQueue,這兩個API會自行管理線程數量



GCD

分作兩種模式
  1. dispatch_async :非同步
  2. dispatch_sync:同步
同步模式下會先等待Block裡面的動作執行完,才繼續往下跑
個人感覺有點類似C#的async Task 搭配 await語法,會把工作丟給別的Thread去做,但會等工作做完才繼續動作

實際上使用dispatch_sync後,發現動作並沒有在其他Thread執行,而是使用main thread,爬文之後發現有人在討論這個問題



也就是說因為main queue是serial queue,呼叫dispatch_sync必定導致線程block住,無所謂執行block內工作的線程,所以程式會直接使用main queue執行,節省開線程的資源

如果今天是在背景呼叫dispatch_sync把工作丟給main queue,就會有線程切換的動作

簡單來說 dispatch_sync 不會開新線程,但在特定情況下,會有線程切換

另外使用dispatch_async也不一定呼叫幾次就開啟多少線程,程式會自行控制
推測應該跟C#一樣使用thread pool的概念
有文章說線程上限66個,serial 與 concurrent共用這數量


使用GCD要小心Block的問題,千萬不要在同步模式(dispatch_sync) 下丟一個Block給自己的線程,自己等自己會卡住

dispatch_queue 可以分為兩種,serial queue與concurrent queue
main_queue:serial queue
global_queue:於concurrent queue
用dispatch_queue_t可以自行宣告,自行決定類型

GCD使用方法



當函式使用非同步模式,又必須回傳其運作結果時,需要使用dispatch_semaphore_t
類似於async await,之後會詳細描述

http://www.iosxxx.com/blog/2016-06-02-GCD那些事.html

2018年4月26日 星期四

Object-C Serialization

Object-C的Serialization不像C#一樣方便
需要在原本的class中寫Encode和Decode方法
寫法如下:

Object-C Extensions

Extension 可以把一個很大的Class拆分,有點類似Category

Extension與Category的差別

  1. Extension中可以定義變數.屬性.方法,Category只能定義方法
  2. Extension不能按照功能或層級來自定名稱(匿名),Category可以在Class後的小括弧內定義
  3. Extension定義出來的Method必須在原Class的Implement中實作 ,Category有自己的.m檔

Extension大部分使用在宣告私有的屬性跟方法,因為Method在.h檔都可以被外部呼叫,如果想宣告一個私有方法,一個方式是只寫Implement,另一個方式就是在.m檔底下使用Extension

Extension使用方法如下:



2018年4月25日 星期三

Cocoa control - Menu

以下解說Cocoa Menu綁定Method的方法





在StoryBoard產生Menu物件之後 ,將Menu點擊動作與其ViewController綁定的流程如下:
1.先在ViewController中寫好要執行的IBAction


2.在Menu選單上按下右鍵,拖曳至上方橘色方塊( First Responder),會出現所有事件的清單

    選擇剛剛寫好的IBAction,就成功綁定了,當Menu按下後會執行Menu的IBAction,再呼叫原本的Method

    First Responder表示當前Window的第一回應對象,其對象必須是NSView或其子集,概念有點像WPF裡面keyBinding,會依當前Focus對象不同,執行不同的動作

    NSView要執行鍵盤事件,一定要先成為First Responder

2017年3月26日 星期日

瀏覽器攝影機權限問題(永遠允許分享攝影機功能)


可針對瀏覽器設定,直接允許分享攝影機功能,之後就不會再詢問

Chrome:


  1. 開啟 Chrome。
  2. 依序按一下右上角的「更多」圖示 更多 接著 [設定]。
  3. 按一下 [顯示進階設定]。
  4. 在「隱私權」部分中,點選 [內容設定]
  5. 在「媒體」部分中,點選下列其中一個選項。
    • 當網站需要使用您的攝影機與麥克風時,必須先詢問您:如果網站要求使用攝影機和麥克風,Chrome 一律會通知您。
    • 不允許任何網站使用您的攝影機和麥克風:自動拒絕所有網站提出的攝影機和麥克風使用要求。
  6. 如要移除您已授予網站的權限,請點選 [管理例外情況]。
---------------------------------------------------------------------------------------------------------------------
Firefox:


1.在搜尋欄輸入about:config 進入瀏覽器服務
               2.搜尋media.navigator.permission.disabled 功能
               3.把此功能打開(預設是disable 切換成 Enable)

2016年11月10日 星期四

MVC網站使用多個資料庫(Code First)

    下命令時需指定使用哪個DataContext
    -Verbose 可以觀察migration流程

  1. enable-migrations -ContextTypeName MultiDataContextMigrations.Models.DataContext -MigrationsDirectory:DataContextMigrations

  2. Add-Migration -configuration MultiDataContextMigrations.DataContextMigrations.Configuration Initial

  3. Update-Database -configuration MultiDataContextMigrations.DataContextMigrations.Configuration -Verbose

2016年11月9日 星期三

MVC用EF存取MYSQL資料庫時產生的不明問題解決方式

以現有資料庫產生EF架構,有時候會發現,在寫入資料時會有不明原因的報錯

錯誤訊息 Error “You have an error in your sql syntax entity framework” with Entity Framework


目前查到的解決方法如下:

1. 右鍵點擊產生的edmx檔,用有編碼的xml打開
2. 搜尋DefiningQuery 這個Tag,然後把它砍了
3. 之後就一切正常了


參考網頁
What happened is that at the moment of executing db.SaveChanges() (after an update), the .edmx tried to update the rows in the 'C' table, but as this table only had two foreign keys to A and B, the update operation wasn't supported. This is the solution that I found in other post in StackOverflow, and it worked for me:

  1. Right click on the edmx file, select Open with, XML editor
  2. Remove the DefiningQuery entirely
  3. Rename the store:Schema="dbo" to Schema="dbo (remove the "store:")
  4. Remove the store:Name=... property (entirely)

2016年10月20日 星期四

比較兩個List , List Compare, var a = SequenceEqual & Equal ignore order

使用linq函式 1.完全符合 var a = ints1.SequenceEqual(ints2); 2.無視順序 To ignore order, use SetEquals: var a = new HashSet(ints1).SetEquals(ints2);

2016年10月5日 星期三

EF with MySQL syntax error 解決辦法

mvc網站對mySql建立entity之後,在新增時出錯 錯誤內容: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near: {SQL COMMAND} 1.Right click on the edmx file, select Open with, XML editor 2.Remove the DefiningQuery entirely 3.Rename the store:Schema="dbo" to Schema="dbo (remove the "store:") 4.Remove the store:Name=... property (entirely)

2016年6月19日 星期日

UserControl 自我綁定

在套用UserControl時,宣告DataContext為 DataContext="{Binding RelativeSource={RelativeSource Self}}" 否則會與套用者綁定

2015年11月23日 星期一

避免children control 的事件觸發parent的事件

今天寫了一個滑鼠移入與點擊時觸發的選單,裡面放了幾個按鈕,發現按鈕的mouseenter事件,會連帶觸發容器的mouseenter事件 找了一下發現可以用以下方式來避免 $(document).ready(function(){ $(".header").click(function(){ $(this).children(".children").toggle(); }); $(".header a").click(function(e) { e.stopPropagation(); }); });

2015年9月1日 星期二

影像處理-提取像素

這幾天寫到一些簡單的影像處理,本來是用Bitmap.GetPixel 卻發現寫出來的程式佔用很多的cpu,還超慢 上網查到一篇有用的網誌,為了避免該網誌掛掉,我這邊也貼一份 我參考的網誌: http://mermerism.blogspot.tw/2014/04/c-bitmap.html 出處:http://blog.csdn.net/ma_jiang/article/details/7725458 一.Bitmap類別 Bitmap對象封裝了GDI+中的一個位元圖,此位元圖由圖形圖像及其屬性的像素數據組成. 見維基百科:http://zh.wikipedia.org/wiki/BMP 即對每一個單一像素在一個位元圖上的處理。 因此Bitmap是用於處理由像素數據定義的圖像的對象.該類的主要方法和屬性如下: 1. GetPixel方法和SetPixel方法:和設置一個圖像的指定像素的顏色. 2. PixelFormat屬性:返回圖像的像素格式. 3. Palette屬性:獲取和設置圖像所使用的顏色調色板. 4. Height Width屬性:返回圖像的高度和寬度. 5. LockBits方法和UnlockBits方法:分別鎖定和解鎖系統內存中的位圖像素. 在基於像素點的圖像處理方法中使用LockBits和UnlockBits是一個很好的方式, 這兩種方法可以使我們指定像素的範圍來控制位圖的任意一部分, 從而消除了通過循環對位圖的像素逐個進行處理,每用LockBits之後都應該調用一次UnlockBits. 二.BitmapData類別 BitmapData對象指定了位元圖的屬性,指定點陣圖影像的屬性 (Attribute)。 BitmapData 類別是由 Bitmap 類別的 LockBits 和 UnlockBits 方法所使用。 無法被繼承。 1. Height屬性:被鎖定位圖的高度. 2. Width屬性:被鎖定位圖的高度. 3. PixelFormat屬性:數據的實際像素格式. 4. Scan0屬性:被鎖定數組的首字節地址,如果整個圖像被鎖定,則是圖像的第一個字節地址. 5. Stride屬性:步幅,也稱為掃描寬度. 如上圖所示,數組的長度並不一定等於圖像像素數組的長度,還有一部分未用區域, 這涉及到位圖的本身結構,系統要保證每行的字節數必須為4的倍數. 否則上圖右側灰色區域的未使用空間(offset)就是一排中非4的倍數中的畸零地 三.Graphics類別 Graphics對像是GDI+的關鍵所在,許多對像都是由Graphics類表示的,該類別定義了繪製和填充圖形對象的方法和屬性,一個應用程序只要需要進行繪製或著色,它就必須使用Graphics對象. 就是在一張圖上要做些甚麼繪製的動作時,就是使用這個類別 四.Image類別 這個類提供了位元圖和位元文件操作的函數. Image類被聲明為abstract,也就是說Image類不能實例化對象,而只能做為一個基本類 1.FromFile方法:從指定的檔案建立 Image 物件。 它根據輸入的文件名產生一個Image對象,它有兩種函數形式: public static Image FromFile(string filename); //從指定的檔案建立 Image 物件。 public static Image FromFile(string filename, bool useEmbeddedColorManagement); //使用指定之檔案中的內嵌色彩管理資訊,從該檔案建立 Image 2.FromHBitmap方法:從 Windows 控制代碼建立 Bitmap。 它從一個windows句柄​​處創建一個bitmap對象,它也包括兩種函數形式: public static bitmap fromhbitmap(intptr hbitmap); //從 GDI 點陣圖控制代碼建立 Bitmap。 public static bitmap fromhbitmap(intptr hbitmap, intptr hpalette); //從 GDI 點陣圖控制代碼和 GDI 調色盤控制代碼建立 Bitmap 3. FromStream方法:從指定的資料流建立 Image。 從一個數據流中創建一個image對象,它包含三種函數形式: public static image fromstream(stream stream); //從指定的資料流建立 Image。 public static image fromstream(stream stream, bool useembeddedcolormanagement); //選擇性地使用指定之資料流中的內嵌色彩管理資訊,從該資料流建立 Image。 fromstream(stream stream, bool useembeddedcolormanagement, bool validateimagedata); //選擇性地使用内嵌色彩管理資訊並驗證影像資料,從指定的資料流建立 Image。 有了上面了理解,在看下面的部分 一. 開檔、存檔、讀檔、寫檔 開檔、存檔、讀檔、寫檔 分別有其代表之意義。 開檔:是打開檔案顯示在銀幕上 存檔:是將做好的檔案儲存在電腦的資料夾 讀檔:是將檔案中的資料讀成Byte或Bit的檔案,讓電腦看得懂 寫檔:是將檔案讀出來之後,使用者才做更改、修正 所以在檔案處理的順序來說,應該是要 開檔-->讀檔-->寫檔-->存檔 private Bitmap srcBitmap = null;//原始的Bitmap private Bitmap showBitmap = null;//顯示用的Bitmap //打開文件 private void menuFileOpen_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = @"Bitmap文件(*.bmp)|*.bmp|Jpeg文件(*.jpg)|*.jpg|所有合適文件 (*.bmp,*.jpg)|*.bmp;*.jpg "; ofd.FilterIndex = 3; ofd.RestoreDirectory = true; if (DialogResult.OK == openFileDialog.ShowDialog()) { srcBitmap = (Bitmap)Bitmap.FromFile(ofd.FileName, false); showBitmap = srcBitmap; this.AutoScroll = true; this.AutoScrollMinSize =newSize((int)(showBitmap.Width), (int)(showBitmap.Height)); this.Invalidate(); } } //保存圖像文件 private void menuFileSave_Click(object sender, EventArgs e) { if (showBitmap != null) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter =@"Bitmap文件(*.bmp)|*.bmp|Jpeg文件(*.jpg)|*.jpg|所有合適文件 (*.bmp,*.jpg)|*.bmp;*.jpg"; sfd.FilterIndex = 3; sfd.RestoreDirectory = true; if (DialogResult.OK == sfd.ShowDialog()) { ImageFormat format = ImageFormat.Jpeg; switch (Path.GetExtension(sfd.FileName).ToLower()) { case".jpg": format = ImageFormat.Jpeg; break; case".bmp": format = ImageFormat.Bmp; break; default: MessageBox.Show(this,"Unsupported image format was specified","Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { showBitmap.Save(sfd.FileName,format ); } catch (Exception) { MessageBox.Show(this, "Failed writing image file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } c#中將bitmap或者image保存為清晰的gif 在c#中默認可以講bitmap保存為gif等格式,但是這種保存方法保存的gif會嚴重失真,正常情況下的代碼: System.Drawing.Bitmap b = new System.Drawing.Bitmap(“c://original_image.gif“); System.Drawing.Image thmbnail = b.GetThumbnailImage(100,75,null,new IntPtr()); thmbnail.Save(“c://thumnail.gif“, System.Drawing.Imaging.ImageFormat.Gif); 一個批量處理圖片的軟件,包括各種處理方式,處理效果,但是在保存為gif的時候出現了問題,在網上查了很久也沒有發現一個可用的改善gif圖片質量的方法,找到了一個解決辦法,保存出來的gif容量大減,但是效果基本符合常規這中方法就是就是“Octree“ 算法。 “Octree“ 算法允許我們插入自己的算法來量子化我們的圖像。 一個好的“顏色量子化”算法應該考慮在兩個像素顆粒之間填充與這兩個像素顏色相近的過渡顏色,提供更多可視顏色空間。 Morgan Skinner提供了很好的“Octree“ 算法代碼,大家可以下載參考使用。 使用OctreeQuantizer很方便:  System.Drawing.Bitmap b = new System.Drawing.Bitmap(“c://original_image.gif“);  System.Drawing.Image thmbnail = b.GetThumbnailImage(100,75,null,new IntPtr());  OctreeQuantizer quantizer = new OctreeQuantizer ( 255 , 8 ) ;  using ( Bitmap quantized = quantizer.Quantize ( thmbnail ) )  {    quantized.Save(“c://thumnail.gif“, System.Drawing.Imaging.ImageFormat.Gif);  }  OctreeQuantizer grayquantizer = new GrayscaleQuantizer ( ) ;  using ( Bitmap quantized = grayquantizer.Quantize ( thmbnail ) )  {    quantized.Save(“c://thumnail.gif“, System.Drawing.Imaging.ImageFormat.Gif); } 你可以點擊這裡下載類的文件(項目文件 ),根據我的試用,只需要兩個類文件(OctreeQuantizer.cs,Quantizer.cs)即可運行,將這兩個類文件的namespace改成 你項目的名稱就行,還有,需要在不安全編譯的方式下編譯,右擊項目名稱,在生成選項卡里選擇"允許不安全代碼"即可 //窗口重繪,在窗體上顯示圖像,重載Paint privatevoid frmMain_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { if (showBitmap != null) { Graphics g = e.Graphics; g.DrawImage(showBitmap, newRectangle(this.AutoScrollPosition.X, this.AutoScrollPosition.Y , (int)(showBitmap.Width), (int)(showBitmap.Height))); } } //灰度化 privatevoid menu2Gray_Click(object sender, EventArgs e) { if (showBitmap == null) return; showBitmap = RGB2Gray(showBitmap);//下面都以RGB2Gray為例 this.Invalidate(); } 二. 提取像素法(超級無敵慢) 即用C#中的getPixel和setPixel 來做取得和設定像素。 這種方法簡單易懂,但相當耗時,完全不可取. public staticBitmap RGB2Gray(Bitmap srcBitmap) { Color srcColor; int wide = srcBitmap.Width; int height = srcBitmap.Height; for (int y = 0; y < height; y++) for (int x = 0; x < wide; x++) { //獲取像素的RGB顏色值 srcColor = srcBitmap.GetPixel(x, y); byte temp = (byte)(srcColor.R * .299 + srcColor.G * .587 + srcColor.B * .114); //設置像素的RGB顏色值 srcBitmap.SetPixel(x, y, Color.FromArgb(temp, temp, temp)); } return srcBitmap ; } 三. 內存法(速度較快) 這是比較常用的方法,即http://softwarebydefault.com/2013/05/18/image-median-filter/所用之法 大概的步驟如下: 先建立一個rect,這是用來放圖的框框 public static Bitmap RGB2Gray(Bitmap srcBitmap) { int wide = srcBitmap.Width; int height = srcBitmap.Height; Rectangle rect = newRectangle(0, 0, wide, height); //將srcBitmap鎖定到系統內的記憶體的某個區塊中,並將這個結果交給BitmapData類別的srcBimap BitmapData srcBmData = srcBitmap.LockBits(rect,ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); //將CreateGrayscaleImage灰階影像,並將這個結果交給Bitmap類別的dstBimap Bitmap dstBitmap = CreateGrayscaleImage(wide, height);//這個函數在後面有定義 //將dstBitmap鎖定到系統內的記憶體的某個區塊中,並將這個結果交給BitmapData類別的dstBimap BitmapData dstBmData = dstBitmap.LockBits(rect,ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed); //位元圖中第一個像素數據的地址。它也可以看成是位圖中的第一個掃描行 //目的是設兩個起始旗標srcPtr、dstPtr,為srcBmData、dstBmData的掃描行的開始位置 System.IntPtr srcPtr = srcBmData.Scan0; System.IntPtr dstPtr = dstBmData.Scan0; //將Bitmap對象的訊息存放到byte中 int src_bytes = srcBmData.Stride * height; byte[] srcValues​​ = new byte[src_bytes]; int dst_bytes = dstBmData.Stride * height; byte[] dstValues​​ = new byte[dst_bytes]; //複製GRB信息到byte中 System.Runtime.InteropServices.Marshal.Copy(srcPtr, srcValues​​, 0, src_bytes); System.Runtime.InteropServices.Marshal.Copy(dstPtr, dstValues​​, 0, dst_bytes); //根據Y=0.299*R+0.114*G+0.587B,Y為亮度 for (int i = 0; i < height; i++) for (int j = 0; j < wide; j++) { //只處理每行中圖像像素數據,捨棄未用空間 //注意位圖結構中RGB按BGR的順序存儲 int k = 3 * j; byte temp = (byte) (srcValues​​[i * srcBmData.Stride + k + 2] * .299 + srcValues​​[i * srcBmData.Stride + k + 1] * .587+ srcValues​​[i * srcBmData.Stride + k] * .114); dstValues​​[i * dstBmData.Stride + j] = temp; } System.Runtime.InteropServices.Marshal.Copy(dstValues​​, 0, dstPtr, dst_bytes); //解鎖位圖 srcBitmap.UnlockBits(srcBmData); dstBitmap.UnlockBits(dstBmData); return dstBitmap; } 四 指標法(速度最快) C/C++的習慣,不是C#的特點,但是效率奇高 public static Bitmap RGB2Gray(Bitmap srcBitmap) { int wide = srcBitmap.Width; int height = srcBitmap.Height ; Rectangle rect = newRectangle(0, 0, wide, height); //將srcBitmap鎖定到系統內的記憶體的某個區塊中,並將這個結果交給BitmapData類別的srcBimap BitmapData srcBmData = srcBitmap.LockBits(rect,ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); //將CreateGrayscaleImage灰階影像,並將這個結果交給Bitmap類別的dstBimap Bitmap dstBitmap = CreateGrayscaleImage(wide, height); //將dstBitmap鎖定到系統內的記憶體的某個區塊中,並將這個結果交給BitmapData類別的dstBimap BitmapData dstBmData = dstBitmap.LockBits(rect,ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed); //位元圖中第一個像素數據的地址。它也可以看成是位圖中的第一個掃描行 //目的是設兩個起始旗標srcPtr、dstPtr,為srcBmData、dstBmData的掃描行的開始位置 System.IntPtr srcScan = srcBmData.Scan0; System.IntPtr dstScan = dstBmData.Scan0; Unsafe //啟動不安全代碼 { byte* srcP = (byte*)(void*) srcScan; byte* dstP = (byte*)(void*) dstScan; int srcOffset = srcBmData.Stride - wide * 3; int dstOffset = dstBmData.Stride - wide ; byte red, green, blue; for (int y = 0; y < height; y++) { for (int x = 0; x

2015年8月23日 星期日

[常用]將所有dll檔封裝進exe檔(WPF)

一般我們寫程式的時候都會用到許多dll
但是把程式交出去的時候如果有一堆dll會很難管理,也不是很好看

這時候就需要把dll封裝進程式執行檔裡面

1.修改Project.csproj


2.右鍵點選參考底下的dll,把複製到本機這個屬性改為False,該dll就不會一起複製到程式執行的資料夾內。
   但此動作會使得程式參考不到該dll檔

3.將dll檔加進Resources內,建置方式選擇內嵌資源,就跟一般放影像檔、音效檔一樣,會把dll封裝進exe執行檔內。
   但程式呼叫到該dll時不會自動找到Resources內的dll檔,必須自行對應

4.新建一支Program.cs檔,取代App.xaml.cs成為專案起始檔(右鍵點專案可以設定起始檔)


完成以上步驟後,再建置程式,就會發現dll檔都不見了,而exe檔明顯變肥



2015年7月17日 星期五

超實用 強制更新Listview的Itemsource

ICollectionView view = CollectionViewSource.GetDefaultView(DeviceSeries.Instance.dic_device);
               
view.Refresh();




2015年4月22日 星期三

DisplayTemplates & EditorTemplates

在mvc中,我們可以為特定型別定義模板

當在razor語法中,用某些語法呼叫該型別時,會自動對應到Shared資料夾下的模板來顯示


@Html.DisplayFor ->Shared/DisplayTemplates/型別名稱
@Html.EditorFor->Shared/EditorTemplates/型別名稱

例如,當我們在razor中使用@Html.DisplayFor(m=>m.myText),myText是string型別時,會使用Shared/DisplayTemplates/String.cshtml來取代該字串的顯示

以上的模板是針對網站上所有該型別的資料,如果只是想要讓特定幾筆資料使用模板,可以將Shared/DisplayTemplates/下的String.cshtml改名,然後再到model使用的型別上定義要使用的模板


如String.cshtml->MyString.cshtml

model class

[UIHint("MyString")]
public string myText{ get; set;}



2015年4月20日 星期一

MVC migration command

在Package Manager Console(nuget套件管理員->套件管理器主控台)下輸入
Enable-Migrations - ContextTypeName 開啟該資料表的migration
這個指令會產生 Migrations 資料夾,其中包含 Configuration.cs 檔案
Configuration.cs中的Seed function可以輸入進行migration時要建立的資料
add-migration Initial 重新產生資料表
add-migration 欄位名稱 新建欄位
最後輸入update database即可更新資料表,此時會連帶建立Seed()中定義的資料 add-migration DataAnnotations 可更新資料欄位的定義(格式 長度限制等)

2015年4月19日 星期日

MVC 日期顯示方式(Model中的日期參數)

DateTime在View中使用Model.DateTime來綁定的話,預設會顯示yyyy/MM/dd hh/mm/ss 想要修改顯示方式的話,可以在參數上加上這行 [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]