2017-04-18 36 views
-1

我是WPF應用程序中的初學者,所以要仁慈。WPF如何在文本框中給出一個無效方法

我試圖重寫一個控制檯應用程序到WPF。要點是,如何在wpf中編寫方法,這是一種無效的方法。

我對代碼發表評論,我覺得它更易於理解。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using Easy2AcquireCom; 
using System.ComponentModel; 

namespace WPF_Polling 
{ 
    /// <summary> 
    /// Interaktionslogik für MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 

     IDeviceManager DeviceManager = new DeviceManager(); 
     IDevice TheDevice = new Device(); 


     Initialize initialisieren = new Initialize(); 


     public MainWindow() 
     { 
      InitializeComponent(); 

      this.DataContext = initialisieren; 
      initialisieren.InitializeDeviceManager(TheDevice, DeviceManager); 
      ListPrefix(); //???? Do I have to give the parameters? 

     } 

     private void ListPrefix(object sender,  DependencyPropertyChangedEventArgs e) // that was a helper DataContextChanged helper 
     { 
      initialisieren.ListDevices(TheDevice,"device"); 
     } 
    } 
    public class Initialize 
    { 

     public void InitializeDeviceManager(IDevice thedevice, DeviceManager devicemanager) 
     { 
      try 
      { 
       devicemanager.Initialize(""); 
       int result = devicemanager.Setup(0); 
       thedevice = devicemanager.GetCurrentDevice(); 
       thedevice.ClearComponentFilter(); 
      } 
      catch (Exception error) 
      { 
      } 
     } 
     public void ListDevices(IDevice theDevice, string prefix) 
     { 
      string[] Tags = (string[]) theDevice.GetTagsByPrefix(prefix); 
      int Index = 1; 

      foreach (string Tag in Tags) 
      { //I want to give out These Things in a TextBox or block 
       string Name = theDevice.GetTagName(Tag); 
       //Console.Write(" "); 
       //Console.Write(Index); 
       //Console.Write(" "); 
       //Console.Write(Tag); 
       //Console.Write(" ("); 
       //Console.Write(Name); 
       //Console.WriteLine(")"); 

       Index = Index + 1; 
      } 

     } 

    } 
} 
+0

請參閱我已編輯的答案。 – mm8

回答

0

在XAML標記定義TextBlock

<TextBlock x:Name="txt" /> 

...和文本追加到其Text屬性:

foreach (string Tag in Tags) 
{ 
    string Name = theDevice.GetTagName(Tag); 
    txt.Text += " "; 
    txt.Text += Index; 
    txt.Text += " "; 
    txt.Text += Tag; 
    txt.Text += " ("; 
    txt.Text += Name; 
    txt.Text += ")"; 
    Index = Index + 1; 
} 

而且你可以直接在調用ListDevices方法窗口的構造函數而不是試圖調用事件處理函數:

public partial class MainWindow : Window 
{ 
    IDeviceManager DeviceManager = new DeviceManager(); 
    IDevice TheDevice = new Device(); 
    Initialize initialisieren = new Initialize(); 

    public MainWindow() 
    { 
     InitializeComponent(); 

     this.DataContext = initialisieren; 
     initialisieren.InitializeDeviceManager(TheDevice, DeviceManager); 
     initialisieren.ListDevices(TheDevice, "device"); 
    } 

    private void ListPrefix(object sender, DependencyPropertyChangedEventArgs e) 
    { 
     initialisieren.ListDevices(TheDevice, "device"); 
    } 
} 

編輯:Initialize類不能訪問在MainWindow類中定義的TextBlock,除非你以某種方式傳遞一個參考MainWindow類的Initialize類:

public partial class MainWindow : Window 
{ 
    Initialize Initialize; 

    public MainWindow() 
    { 
     InitializeComponent(); 
     Initialize = new Initialize(this); 
     Initialize.ListDevices("devices"); 
    } 
} 

public class Initialize 
{ 
    private readonly MainWindow _window; 
    public Initialize(MainWindow window) 
    { 
     _window = window; 
    } 
    public void ListDevices(string prefix) 
    { 
     string[] Tags = { "hallo", "devices", "Test" }; 
     int Index = 1; 


     foreach (string Tag in Tags) 
     { 
      _window.Txt.Text += " "; 
      _window.Txt.Text += Index; 
      _window.Txt.Text += " "; 
      _window.Txt.Text += Tag; 
      _window.Txt.Text += " ("; 
      _window.Txt.Text += ")"; 
      Index = Index + 1; 
     } 
    } 
} 
+0

Visual Studio說「無法解析Symbol'txt。*我在我的公共MainWindow中調用了方法ListDevices,並且在我粘貼瞭解決方案的方法中以及我給了TextBox Name txt – Kleinstein11

+0

您是否添加了x:Name =」txt 「到您的MainWindow.xaml文件中的TextBlock,正如我所建議的那樣? – mm8

+0

是的,我做過了,也許我必須在主窗口中初始化txt作爲Objekt? – Kleinstein11

0

我想補充一個RichTextBox從設計器並從代碼追加文本到它。

​​
相關問題