2017年7月27日 星期四

C# 找到 呼叫的Function的完整名稱

請參考以下的程式碼
MethodBase method = stackTrace.GetFrame(1).GetMethod();
string methodName = method.Name;
string className = method.ReflectedType.Name;
Console.WriteLine(className + "." + methodName);


How to find FULL name of calling method C#

C# WPF Textbox只能輸入數字

在引用的View.Xaml畫面裡面加入這行,做事件綁定。

這樣就可以做到只能輸入數字了。
DelayStartTimeTextBox.PreviewTextInput += StaticResourceModel.PreviewTextInput;
    public static class StaticResourceModel
    {
        public static void PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            e.Handled = new System.Text.RegularExpressions.Regex("[^0-9]+").IsMatch(e.Text);
        }
    }

C# 泛型List加入到ObservableCollection

怕以後忘記,先寫起來。

首先先New一個ObservableCollection跟List。

並且在ObservableCollection的()內加入到你要放的List。

這樣就可以把List的資料轉移到ObservableCollection。


public ObservableCollection MyString = new ObservableCollection(new List());

C# ICommand 執行多個 Function 或加入參數

在 obj => 加入大括號,如下程式碼。

public ICommand ResetProgramCommand { get { if (_resetProgramCommand == null) { _resetProgramCommand = new RelayCommand(obj => {_event.ResetProgramEvent() , _event.UpdateProgramEvent()}, null); } return _resetProgramCommand; } }


如果是加入參數就變成以下這樣
string value = string.empty;
string result = "Success";

public ICommand ResetProgramCommand { get { if (_resetProgramCommand == null) { _resetProgramCommand = new RelayCommand(obj => {_event.ResetProgramEvent(),value = result;}, null); } return _resetProgramCommand; } }

C# 如何匯入外部DLL及如何傳值運作

今天被主管問到一個問題,.NET的參考元件順序是什麼? 一時答不出來,也沒有這樣的觀念,所以找了一些資料。

.NET參考元件的順序

1.GAC (C:\Windows\assembly)

2.執行exe檔底下同目錄的DLL檔

3.環境變數內的路徑,去尋找該DLL檔

讓我對.NET的參考元件順序有更深的認識

[.NET] 組件安裝與配置 / Assembly Install and Configuration

C# 如何匯入外部DLL及如何傳值運作

2017年7月24日 星期一

xml for linq 讀取

如果我有以下的XML檔案格式,但是想要取得LanguageInfo的參數,可以用以下的xml for linq的程式碼取得
 











首先宣告XDocument的物件,並給它檔案路徑去讀取,然後再new一個ObservableCollection的string泛型,透過linq的語法去取得attribute的參數,並轉成list在轉型放入到ObservableCollection。
XDocument allLanguage = XDocument.Load($@"{Properties.Settings.Default.ConfigPath}");

name = = new ObservableCollection((from data in allLanguage.Descendants("TaskManager").Descendants("Language") select data).Elements("LanguageInfo").Select(args => args.Attribute("name").Value).ToList());

value = new ObservableCollection((from data in allLanguage.Descendants("TaskManager").Descendants("Language") select data).Elements("LanguageInfo").Select(args => args.Attribute("value").Value).ToList());

如果是要放到一個類別裡面也可以這樣宣告

foreach(XElement element in (from data in allLanguage.Descendants("TaskManager").Descendants("Language") select data).Elements("LanguageInfo"))
{
StaticResourceModel.ComboBoxValue.Add(new ComboBoxValue() {Name= element.Attribute("name").Value , Value = element.Attribute("value").Value});
}