2

TextBoxC# で WPF フォームのテキストを更新する際に問題があります。プログラムで新しいフォームを作成し、 oneLabelと oneを追加TextBoxしました。以前に作成したバッファからの文字列を表す変数 temp があります。しかし、テキストをラベルまたはテキスト ボックスに設定しようとしても、何も起こりませんでした。しかし、ウィンドウのタイトルを新しい形で変更できます。

私のコードは次のとおりです。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
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;

namespace BufferProba
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        BufferStream bfStream = new BufferStream();

        private static Action EmptyDelegate = delegate() { };

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            Thread t = new Thread(SetText);
            t.SetApartmentState(ApartmentState.STA);
            t.IsBackground = true;
            t.Start();

        }


        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            bfStream.put(tBox.Text.Trim());
            tBox.Text = "";
        }


        public void SetText()
        {
            Thread.Sleep(5000);

            Window myWindow = new Window();
            StackPanel stackPanel = new StackPanel { Orientation = Orientation.Vertical };
            TextBox tboxForm = new TextBox();
            Label szzr = new Label { Content = "" };
            stackPanel.Children.Add(szzr);
            stackPanel.Children.Add(tboxForm);
            myWindow.Content = stackPanel;
            List<String> listaStringova = new List<String>();

            while (true)
            {
                Thread.Sleep(5000);

                String temp = bfStream.get();
                listaStringova.Add(temp);

                if (temp != "0")
                {
                    //Console.WriteLine(temp);
                   myWindow.Title = temp;
                   szzr.Content = temp;
                   szzr.Background = new SolidColorBrush(Colors.Orange);
                   szzr.UpdateLayout();
                   tboxForm.Text = temp;
                   myWindow.Show();

                }
                else { 
                    MessageBox.Show("Jebiga");
                }

            }
        }


    }
}
4

2 に答える 2

0

メインスレッドで実行する必要があります。Dispatcher を使用して同じ例を実行できます。

System.Windows.Application.Current.Dispatcher.Invoke(new Action(SetText()))
于 2013-03-18T08:45:47.233 に答える
0

TextBox にアクセスしているのは UI スレッドであることを確認する必要があります。Dispatcher.CheckAccess メソッドを使用し、false が返された場合は、UI スレッドを呼び出します。この情報を使用して、ウェブ上で多くの例を見つけることができるはずです。

于 2013-03-18T08:35:53.523 に答える