wpf의 루프 카운터 값을 실시간으로 나타내는 텍스트 상자를 원한다면 어떻게해야합니까?실시간 루프 카운터 출력 wpf
추기 작업 솔루션 : 여기
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private delegate void UpdateTextBox(DependencyProperty dp, Object value);
...
private void MyMethod()
{
...
int iMax=...;
...
MyClass iMyClass = new MyClass(arguments);
this.DataContext = iMyClass;
UpdateTextBox updateTBox = new UpdateTextBox(textBlock1.SetValue);
for (int i = 1; i <= iMax; i++)
{
iMyClass.MyClassMethod(i);
Dispatcher.Invoke(updateTBox, System.Windows.Threading.DispatcherPriority.Background, new object[] { MyClass.MyPropertyProperty, iMyClass.myProperty });
}
나는 당신의 제안에 따라 시도 코드이지만, 일, 내가 "0"텍스트 상자에 기록 얻을 나던, 그래서 바인딩은 OK입니다 생각하지만, 루프가 작동하지 않습니다. 루프 내에서 textbox2.text = "a"에 의해 다른 텍스트 상자에 직접 쓰는 루프를 만들었지 만 작업을하지는 못했습니다.
/// <summary>
/// Interaction logic for Window1.xaml
/// DOESNT WORK PROPERLY
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
TestClass tTest = new TestClass();
this.DataContext = tTest ;
tTest.StartLoop();
}
}
public class TestClass : DependencyObject
{
public TestClass()
{
bwLoop = new BackgroundWorker();
bwLoop.DoWork += (sender, args) =>
{
// do your loop here -- this happens in a separate thread
for (int i = 0; i < 10000; i++)
{
LoopCounter=i;
}
};
}
BackgroundWorker bwLoop;
public int LoopCounter
{
get { return (int)GetValue(LoopCounterProperty); }
set { SetValue(LoopCounterProperty, value); }
}
public static readonly DependencyProperty LoopCounterProperty = DependencyProperty.Register("LoopCounter", typeof(int), typeof(TestClass));
public void StartLoop()
{
bwLoop.RunWorkerAsync();
}
}
}
내가 DependancyProperty 및 디스패처와 함께 할 관리 .Invoke. 첫 번째 가능성에 대해 더 자세히 설명해 주시겠습니까? 백그라운드 프로세스에서 루프를 실행하고 종속성 속성에 루프 카운터를 작성하십시오. 내가 한 짓과 다른거야? – butaro
코드 예제를 추가하여 코드와 비교할 수 있습니다. 질문에 코드를 추가하면 그에 대한 의견을 말할 수 있습니다. – Heinzi
그랬지, 내가 한 일이 분명 할까? – butaro