gpt4 book ai didi

c# - 从类编辑 MainWindow 上的 ListBox

转载 作者:太空宇宙 更新时间:2023-11-03 14:17:44 24 4
gpt4 key购买 nike

我有一个 WPF 应用程序,我创建了一个名为 Agent 的新类。在我的 WPF 应用程序窗口中,我有 ListBox。我从 MainWIndow.xaml.cs 调用 Agent

Agent 类运行 FileSystemWatcher,现在当 OnChanged 事件被引发时,我想将消息添加到 ListBox 事件被引发.

主窗口.xaml

<Window x:Class="MachineLogAgentGUI.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="411" Width="515">
<Grid Background="#0A000000">
<Button Content="Run Agent" Height="26" HorizontalAlignment="Left" Margin="397,12,0,0" Name="runAgent" VerticalAlignment="Top" Width="86" Click="runAgent_Click" />
<ListBox Margin="12,66,12,12" Name="messageBox" />
<TextBox Height="26" HorizontalAlignment="Left" Margin="12,12,0,0" Name="observedDirectory" VerticalAlignment="Top" Width="261" />
<Button Content="Browse" Height="26" HorizontalAlignment="Left" Margin="271,12,0,0" Name="browse" VerticalAlignment="Top" Width="75" Click="browse_Click" />
<CheckBox Content="Include Subfolders" Height="16" HorizontalAlignment="Left" Margin="12,44,0,0" Name="includeSubfolders" VerticalAlignment="Top" />
</Grid>

在 MainWindow.xaml.cs 中我有:

    Agent agent = null;

private void runAgent_Click(object sender, RoutedEventArgs e)
{
if (agent == null || !agent._running)
{
agent = new Agent(@"W:\MindWare.SVN\CardMax2\trunk\ProcessEngine\TSPHelper.Producer\bin\Debug\GeneratedLogs");
runAgent.Content = "Stop Agent";
}
else if (agent._running)
{
agent.StopAgent();
runAgent.Content = "Run Agent";
}
}

和代理类:

public class Agent
{
private string Path { get; set; }
public bool _running { get; set; }

FileSystemWatcher watcher = new FileSystemWatcher();

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public Agent(string path)
{
Path = path;
watcher.Path = path;
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Filter = "*.txt";
watcher.IncludeSubdirectories = true;

watcher.Changed += new FileSystemEventHandler(OnChanged);

watcher.EnableRaisingEvents = true;
_running = true;
}

// Stop Agent
public void StopAgent()
{
watcher.EnableRaisingEvents = false;
_running = false;
}

// Define the event handlers.
private void OnChanged(object source, FileSystemEventArgs e)
{
try
{
watcher.EnableRaisingEvents = false;
_running = false;

// DO SOMETHING HERE
// Add item to ListBox on MainWindow somehow
}
finally
{
watcher.EnableRaisingEvents = true;
_running = true;
}
}

我该怎么做?

最佳答案

我想我明白你的意思了。

您可以在 Agent 类中创建静态 MainWindow 对象:

public class Agent
{
public static MainWindow mainWindow;

....
}

然后在 MainWindow.cs 中,您可以将窗口添加到变量

private void Window_Loaded(object sender, RoutedEventArgs e)
{
Agent.mainWindow = this;
....
}

现在,当您需要访问它时,您可以引用 mainWindow:

public class Agent
{
public static MainWindow mainWindow;

public void AddToList(string value)
{
mainWindow.listBox1.items.add(value);
}

编辑:线程错误更新

听起来 FileSystemWatcher 在 UI 的单独线程上运行。您可以通过将工作传递给 UI 线程来管理它

// DO SOMETHING HERE
// Add item to ListBox on MainWindow somehow
if (!mainWindow.Dispatcher.CheckAccess())
{
mainWindow.Dispatcher.BeginInvoke(
System.Windows.Threading.DispatcherPriority.Normal,
new Action(
delegate()
{
//Code to make changes to the mainWindow if the thread does not have access:
mainWindow.listBox1.Items.Add("hello");
}));
}
else
{
//Access allowed make changes normally.
mainWindow.listBox1.Items.Add("hello");
}

马丁

关于c# - 从类编辑 MainWindow 上的 ListBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6220827/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com