跳到主要內容

發表文章

目前顯示的是有「UI」標籤的文章

[Vue.js] 如何在頁面上顯示JSON

假設我們有一筆資料如下想要以JSON的格式顯示在網頁上 var data = {      'name' : 'andy'      'age': 30 } 我們可以使用 JSON.stringify(data, null, 2) 來將資料轉成相對應的字串, 其中第三個參數設為 2 的目的是用來控制用來做縮排的空白數目, 再來, 使用 <pre> 標籤來顯示文字並保留縮排 <pre> {{JSON.stringify( data, null, 2)}} </pre>

[Vue.js] 如何在Vue套用 font awesome

前言 基本上在開發前端網頁時, 若是要豐富網頁的內容, 一定少不了使用一些圖示來增添美感,  為了能夠自由放大縮小圖片且不失真, 通常我們會使用SVG格式的圖片來美化UI 。  這類的圖我們可以自己做或是從font awesome下載 。  以下是介紹如何在Vue.js 的框架裡套用 font awesome 透過npm下載 npm install vue-awesome npm install vue-awesome 註冊到 main.js import 'vue-awesome/icons' import Icon from 'vue-awesome/components/Icon' Vue.component('icon',Icon) 使用(以按鈕為例) <b-button> <icon name='save'></icon> Save </b-button>

[Vue.js] 資料響應, 資料改變且重新渲染

假設我們有一個購物網站, 需要在頁面上顯示購物車裡的商品數量 <p> {{goods.count}} </p> 而這筆資料會在使用者操做 Add 按鈕後,產生變化 要實作這個功能很簡單, 將按鈕綁定我們addCount()方法, 並在方法內修改goods.count就能達到我們目的  <button @click="addCount()"> Add </button> ... export default      data() {   return {   goods: { count: 0 }   }   }      methods: {           addCount(){                this.goods.count ++;           }      } 基本上Vue會把定義在data()裡的屬性(property)轉換成getter/setter  當setter被觸發時會告知組件相對應的watcher進行重新渲染 這也就是為什麼資料一旦更變, 頁面就會自動被更新的原因 但如果屬性是動態地被加上去的話,  <p> {{goods. cost }} </p> ... export default   data() {   return {   goods: { count: 0 }   }   }   methods: {   addCount(){...

Vue.js 組件數據傳遞 Pass Data To Another Component

在Vue的世界裡 我們如何把數據從組件A傳到組件B的 方法很簡單,如下 假設我有一個自訂義的組件  Header.vue, 我想在 App.vue 裡把數據 foodList傳給它 首先在 Header.vue 組件上, 我們使用必須開一個接口  foodList  去接收數據, 新增props屬性foodList <script>      export default{           props:{                 foodList :{                     type: Array,                     default: () => []                }           }      } </script> 接下來, 當我們在App.vue裡使用時Header.vue組件時 就可以順勢地把資料船過去 App.vue <template>      <MyHeader : foodList =' foodList '>   </MyHeader> </template> <script>      import MyHeader from './compone...

使用v-for出現錯誤訊息

在組件b-nav-item裡使用v-for產生導航列表時 <b-nav-item v-for="item in navItems"> {{ item.title }} </b-nav-item > 遇到以下的錯誤訊息 (Emitted value instead of an instance of Error) <b-nav-item v-for="item in navItems">: component lists rendered with v-for should have explicit keys. See https://vuejs.org/guide/list.html#key for more info. 原因是在 Vue 2.2 版之後, 當我們在組件(component)內使用v-for產生組件列表時, 必須明指定Key值, 讓Vue 用以區分每個元素, 藉以確保能正確的更新所有元素 解決方法:  使用v-bind來指定key值 <b-nav-item v-for="item in navItems" v-bind:key="item.titil"> Ref: https://cn.vuejs.org/v2/guide/list.html

[WPF]如何在程式中加入Hot key event

假設今天我想要讓使用者Hot key(如Alt) trigger程式做事情的時候 我們可以編寫UI的xaml 加入   PreviewKeyDown  事件的handler 並實作他 Step 1. Add event handler < Window         xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns : x ="http://schemas.microsoft.com/winfx/2006/xaml"         xmlns : d ="http://schemas.microsoft.com/expression/blend/2008"         xmlns : mc ="http://schemas.openxmlformats.org/markup-compatibility/2006" mc : Ignorable ="d"         xmlns : conv ="clr-namespace:Solution.Converter"         x : Class ="Solution.MainWindow"          WindowState ="Maximized" WindowStyle ="None"         SnapsToDevicePixels ="True"         d : D...

[MFC] 使用Timer更新UI上的資訊

宣告 timer 資源 #define IDC_Timer   2948 宣告訊息映射函數 BEGIN_MESSAGE_MAP( WaitProceDlg , CDialog) ON_WM_TIMER() END_MESSAGE_MAP() 實作回呼函式 onTimer void WaitProceDlg::OnTimer(UINT_PTR nIDEvent) {          // TODO: Add your message handler code here and/or call default                 CDialog::OnTimer(nIDEvent); } 啟動 timer SetTimer(IDC_Timer,500,NULL);

[MFC] 修改字體大小(font size)

假設客戶的需求是希望UI的字體大小能在 runtime 時決定, 此時我們有兩種方式來修改font size: (這裡使用TextControl IDC_STATIC_1) 為例子 Way 1. 取得UI上的控件, 並呼叫SetFont() CFont m_font; / /設定大小為 4 m_ font.CreatePointFont (40 , L"Arial" ); //取得UI上的控件, 並呼叫SetFont() GetDlgItem ( IDC_STATIC_1 )->SetFont(m_font); Way 2. 透過 息映射(message mapping)函數  ON_WM_CTLCOLOR    Step 1.宣告訊息映射(message mapping)函數   BEGIN_MESSAGE_MAP(CTestDlg, CDialog)          ...           ON_WM_CTLCOLOR( )          ...      END_MESSAGE_MAP() Step 2. 實作回呼函式  OnCtlColor HBRUSH CTestDlg :: OnCtlColor ( CDC *  pDC, CWnd *  pWnd, UINT nCtlColor ) {         HBRUSH hbr  =  CDialog :: OnCtlColor (  pDC, pWnd, nCtlColor  ) ;          if ( p...

[WPF] UI can't drag or move

由於 Window 設定  WindowStyle ="None" 因此會使UI變成不能拖拉的狀況 解決方法是註冊 MouseDown 的event handle 如下: < Window x : Class ="APPUI.MainWindow"         xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns : x ="http://schemas.microsoft.com/winfx/2006/xaml"              Title ="MainWindow" Height ="540" Width ="764" ResizeMode ="NoResize"         WindowStyle ="None"         MouseDown ="Window_MouseDown_1"         > ... 並且實作  Window_MouseDown_1 private void Window_MouseDown_1( object sender, MouseButtonEventArgs e)         {             if (e.ChangedButton == MouseButton .Left) ...

[WPF] 如何在程式UI秀出來之前先作一些事情

如何在程式UI秀出來之前先作一些事情 首先我們必須知道 App.xaml 可以用來決定程式的起始點(starting point) 如下: <Application   x:Class = "WpfTutorialSamples.App"               xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"               xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"                StartupUri = "MainWindow.xaml" > 預設 StartupUri="MainWindow.xaml" 因此程式一開始會直接秀出 MainWindow 但是有時候我們會想在MainWindow出來前先做一些事情 此時我們就必須選用Startup的屬性來開始程式 並註冊Startup event 的event handle 讓程式起來的時候會先處理我們指定的事情 如下: Step 1. 實作Startup event的event handle   public   partial   class   App   :   Application           {                   private   void   Application_Startup ( object  sender ,   StartupEventArgs  e )      ...

[WPF][Error] Must create DependencySource on same Thread as the DependencyObject

今天使用程式產生了一張BitmapImage的圖片 當要將圖片assign 給Form 的成員變數時卻遇到下面的錯誤訊息 Exception: Must create DependencySource on same Thread as the DependencyObject. 由於圖片是透過 worker thread  產生的, 上網爬文後才知道這麼做其實很危險 最保險的方法還是從   resource    裡把圖片load 進來 另一個解法: BitmapImage  有一個 function   Freezable()    ,我們可以藉由呼叫它將圖片變成 read-only 的狀態,  一旦圖片的屬性是read-only , 就可以放心的在其他執行緒中使用了  

[WPF]為什麼無法在WINPE裡顯示圖片

當我們使用WPF的image cotrol來顯示圖片時   LoadingImage.Source = new BitmapImage ( new Uri ( "H:\\Images\\img_loading_" + count.ToString() + ".png" )); 如果要將程式跑在WINPE 下 WINPE環境裡必須有以下兩隻DLL - mscms.dll  - windowscodecsExt.dll 若缺少的話, 程式在執行期間拋出以下的錯誤訊息  unable to load DLL mscms.dll  unable to load DLL windowscodecsExt.dll  

如何透過非 UI thread 去存取WPF control

如果把產生資料的邏輯交給UI thread去處理 則有可能會因為處理的資料量太大造成UI hang on的問題 因此我們可以創建另一個thread去幫我們做這件事情 System.Threading. Thread thread = new System.Threading. Thread (() => GenerateDataAndUpdateUI( this )); thread.Start(); 可是在update UI control 時 必須使用 Dispatcher 物件來通知 UI thread 去做更新   GenerateDataAndUpdateUI(){                               //generate data ...                  Dispatcher.BeginInvoke( new Action (() => {                                                          //update UI ...                 })); } 否則可能會遇到以下的runtime exception The calling thread cannot access this object because a...

WPF當游標經過目標圖片時自動放大圖片的功能

以 DoubleAnimation 的動畫特效來實現: 當游標經過目標圖片時自動放大圖片的功能 以下面簡單的UI當作範例 < Window   x   : Class ="MouseoverAndScaleUIImage.MainWindow"           xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"           xmlns : x   ="http://schemas.microsoft.com/winfx/2006/xaml"           Title ="MainWindow"   Height   ="350"   Width ="525">       < Grid   >           < Image   Name   ="IconImage"   Source ="icon.jpg"   HorizontalAlignment ="Left"   Height ="100"   Margin ="180,118,0,0"   VerticalAlignment ="Top"   Width   ="100">         </ Window > 實作方法: 一.  首先建立 DoubleAnimation 和 Storyboard 若要將動畫套用在物件上, 須建立  Storyboard,...