2017-05-07 32 views
0

我正在建立一個字典WPF應用程序,在這裏你寫一個單詞,而不是應用程序告訴你這個單詞是什麼語言,並且還給你一個單詞的描述。使用MVVM。 下面是它的外觀:dictionaryApp,MVVM,WPF,從文本文件中讀取文件

enter image description here

我有找出如何讓語言和文字從一個文本文件中的描述,並把它們在文本框中的問題,我不意味着綁定,但獲取信息的確切方法。 這裏是我的模型:

using System; 
using System.Collections.ObjectModel; 
using System.Collections.Generic; 

namespace dictionary 
{ 
    public class dictionaryModel 
    { 
     private string word; 
     private string language; 
     private string description; 

     public string Word 
     { 
      get 
      { 
       return word; 
      } 
      set 
      { 
       word = value; 
      } 
     } 

      public string Language 
     { 
      get 
      { 
       return language; 
      } 
      set 
      { 
       language = value; 
      } 

     } 

      public string Description 
     { 
      get 
      { 
       return description; 
      } 
      set 
      { 
       description = value; 
      } 

     } 

     public dictionaryModel(string describedWord, string WordLanguage, string WordDescription) 
     { 
      this.word = describedWord; 
      this.language = WordLanguage; 
      this.description = WordDescription; 
     } 

     public dictionaryModel() 
     { } 

    } 







and here is my View Model, so far: 
namespace dictionary 
{ 
    using System; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.ComponentModel; 
using System.IO; 
using System.Linq; 
using System.Reflection; 
using System.Runtime.CompilerServices; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Data; 
using System.Windows.Input; 
using System.Windows.Markup; 



    public class dictionaryViewModel : INotifyPropertyChanged 
    { 
     private ICommand _getAnswer; 
     private dictionaryModel m; 
     private string w; 
     private string retLanguage; 
     private string retDescription; 
     private bool _canExecute; 

     public dictionaryViewModel() 
     { 
      _canExecute = true; 
     } 


     public string retLang 
     { 
      get 
      { 
       return retLanguage; 
      } 
      set 
      { 
       retLanguage = value; 
       NotifyPropertyChanged(); 
      } 

     } 

     public string retDescr 
     { 
      get 
      { 
       return retDescription; 
      } 
      set 
      { 
       retDescription = value; 
       NotifyPropertyChanged(); 

      } 
     } 

     public string word 
     { 
      get 
      { 
       return w; 
      } 
      set 
      { 
       w = value; 
       NotifyPropertyChanged(); 
      } 
     } 

     public dictionaryModel model 
     { 
      get 
      { 
       return m; 
      } 
      set 
      { 
       m = value; 
       NotifyPropertyChanged(); 
      } 
     } 

     public ICommand getAnswer 
     { 
      get 
      { 
       return _getAnswer ?? (_getAnswer = new RelayCommand(() => getWholeAnswer(word), _canExecute)); 
      } 
     } 

     public dictionaryViewModel(dictionaryModel model, string word, string retLang, string retDescr, 
      ICommand getAnswer) 
     { 
      m = model; 
      w = word; 
      retLang = retLanguage; 
      retDescr = retDescription; 
      _getAnswer = getAnswer; 

     } 


     public ObservableCollection<dictionaryModel> readTxtFile() 
     { 
      ObservableCollection<dictionaryModel> dictObj = new ObservableCollection<dictionaryModel>(); 
      string word; 
      string language; 
      string description; 

      var file = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "dictionary.txt"); 
      StreamReader read = new StreamReader(file); 
      string line; 

      string[] item; 
      while((line=read.ReadLine())!=null) 
      { 
       item = line.Split(';'); 
       word = item[0]; 
       language = item[1]; 
       description = item[2]; 
       dictionaryModel object1 = new dictionaryModel(word, language, description); 

       dictObj.Add(object1); 

      } 
      read.Close(); 
      return dictObj; 
     } 

     public void getWholeAnswer(string w) 
     { 

      string descr, lang; 
      dictionaryModel obj = null; 
      ObservableCollection<dictionaryModel> rdF = readTxtFile(); 
      try 
      { 
       foreach(dictionaryModel a in rdF) 
       { 
        if(w.Equals(a)) 
        { 
         descr = retDescr; 
         lang = retLang; 
         obj = a; 
        } 
        obj.Language = retLang; 
        obj.Description = retDescription; 
       } 
      }catch(Exception e) 
      { 
       ExceptionInfo(); 
      } 

     } 

     private void ExceptionInfo() 
     { 
      throw new NotImplementedException(); 
     } 

     private void NotifyPropertyChanged() 
     { 
      throw new NotImplementedException(); 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 
    } 
} 

的方法是getWholeAnswer()。你看我試過了什麼,但沒有成功。如果您有任何意見,請幫我...

+0

爲什麼每次調用'getWholeAnswer'時都要從文件中重載字典? –

回答

0

,我發現我的錯誤,我張貼的方法的解決方案,它可幫助別人:

公共無效getWholeAnswer (字符串w) {

 dictionaryModel obj = null; 
     ObservableCollection<dictionaryModel> rdF = readTxtFile(); 
     bool find = false; 
     try 
     { 
      foreach(dictionaryModel a in rdF) 
      { 
       if(w.Equals(a.Word)) 
       { 

        obj = a; 
        retLang = a.Language; 
        retDescr = a.Description; 
        find = true; 
        break; 
       } 
      } 
      if(false == find) 
      { 
       AskTheQuestion(); 
      } 
     }catch(Exception e) 
     { 
      AskTheQuestion(); 
     } 

    }