Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
FileSystemWatcher事件未触发C#_C#_Wpf_Winapi_Filesystemwatcher - Fatal编程技术网

FileSystemWatcher事件未触发C#

FileSystemWatcher事件未触发C#,c#,wpf,winapi,filesystemwatcher,C#,Wpf,Winapi,Filesystemwatcher,我将尝试遵循本教程: 但我的FileSystemWatcher事件似乎没有触发。知道为什么吗 另外,我对C#和Windows API的了解非常原始-请在回答时考虑到这一点 Window1.xaml.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using Syst

我将尝试遵循本教程:

但我的FileSystemWatcher事件似乎没有触发。知道为什么吗

另外,我对C#和Windows API的了解非常原始-请在回答时考虑到这一点

Window1.xaml.cs

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 System.IO;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            string watchedFolder = System.IO.Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "Pictures");

            FileSystemWatcher fsw = new FileSystemWatcher(watchedFolder);

            MainWindow.Title = "Monitoring: " + fsw.Path;

            fsw.IncludeSubdirectories = true;
            fsw.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;

            fsw.Changed += new FileSystemEventHandler(fsw_Changed);

            fsw.EnableRaisingEvents = true;
        }

        void fsw_Changed(object sender, FileSystemEventArgs e)
        {
            updatedImage.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                new Action(
                    delegate ()
                    {
                        ImageSourceConverter isc = new ImageSourceConverter();
                        updatedImage.Source = (ImageSource)isc.ConvertFromString(e.FullPath);
                    }
                )
            );
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用System.IO;
命名空间WpfApplication1
{
/// 
///Window1.xaml的交互逻辑
/// 
公共部分类Window1:Window
{
公共窗口1()
{
初始化组件();
}
已加载私有void主窗口(对象发送器、路由目标)
{
string watchedFolder=System.IO.Path.Combine(Environment.GetEnvironmentVariable(“USERPROFILE”),“Pictures”);
FileSystemWatcher fsw=新的FileSystemWatcher(watchedFolder);
MainWindow.Title=“监控:”+fsw.Path;
fsw.IncludeSubdirectories=true;
fsw.NotifyFilter=NotifyFilters.FileName | NotifyFilters.LastWrite;
fsw.Changed+=新的文件系统管理员(fsw\u已更改);
fsw.EnableRaisingEvents=true;
}
无效fsw_已更改(对象发送方、文件系统目标)
{
updateImage.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
新行动(
代表()
{
ImageSourceConverter isc=新的ImageSourceConverter();
updateImage.Source=(ImageSource)isc.ConvertFromString(e.FullPath);
}
)
);
}
}
}
Window1.xaml

<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="Window1" Height="350" Width="525"
        Name="MainWindow">
    <Grid>
        <Image Name="updatedImage" />

    </Grid>
</Window>

您的XAML似乎缺少一个关键部分。
MainWindow_加载的事件处理程序的绑定。
没有代码绑定到该事件,因此不会创建和初始化任何FileSystemWatcher

<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="Window1" Height="350" Width="525"
        Name="MainWindow"
        Loaded="MainWindow_Loaded">