开发者

Notification window in case of any operation in C: drive

I have created a sample for Notification window in silverlight4.

The issue i'm facing is that i want to show notification window whenever any operation(say file copied or deleted) is performed in my windows C: drive.

Since System.IO.FileSystemWatcher class (COM+ componenet) cannot be used in silverlight, so i found a way out using an AutomationFactory, it works like charm for me as long as i am using message box to show notification or default notification window but not in case of my own customnotification window.. :(

The code is like this -

new Thread(() =>
            {
                using (dynamic SWbemLocator = AutomationFactory.CreateObject("WbemScripting.SWbemLocator"))
                {
                    SWbemLocator.Security_.ImpersonationLevel = 3;
                    SWbemLocator.Security_.AuthenticationLevel = 4;
                    dynamic IService = SWbemLocator.ConnectServer(".", @"root\cimv2");

                    string fileSystemWatcherQuery =
                        @"SELECT * FROM __InstanceOperationEvent WITHIN 3 WHERE Targetinstance ISA 'CIM_DirectoryContainsFile' and TargetInstance.GroupComponent= 'Win32_Directory.Name=""c:\\\\""'";
                    dynamic monitor = IService.ExecNotificationQuery(fileSystemWatcherQuery);

                    Dispatcher.BeginInvoke(() => MessageBox.Show(@"Now listening to file changes on c:\"));

                    while (true)
                    {
                        dynamic EventObject = monitor.NextEvent();
                        string eventType = EventObject.Path_.Class;
                        string path = EventObject.TargetInstance.PartComponent;
                        Dispatcher.BeginInvoke((Action)delegate
                        {
                            System.Windows.NotificationWindow notify = new System.Windows.NotificationWindow();
                            notify.Height = 74;
                            notify.Width = 329;

                            CustomNotification custom = new CustomNotification();
                            custom.Header = "FileSystemWatcher";
                            custom.Text = eventType + ":" + path;
                            custom.Width = notify.Width;
                            custom.Height = notify.Height;
                            custom.Closed += new EventHandler<EventArgs>(custom_Closed);
                            notify.Content = custom;
                            notify.Show(5000);
                        });
                        Dispatcher.BeginInvoke(() => MessageBox.Show(eventType + ": " + path));
                    }
                }
            }).Start();

Whenever i copied the file or delete a file from my C:, a message box appears and notification window do appears but the content is entirely blank. If i use default notification window in place of my own CustomNotification it works fine.

What i am guessing is, it has something related to threading, not sure though.. ??

CustomNotification is a class which derives from ContentConrol class and have two dependency properties namely "Header" & "Text". The control template for this i have declared in different xaml file like this -

<Style TargetType="local:CustomNotification">
            <Setter.Value>
                <ControlTemplate TargetType="local:CustomNotification">
                    <Border BorderBrush="Black" BorderThickness="1">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="7"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <Rectangle Grid.Row="0">
                                <Rectangle.Fill>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="#FFB9B9CC"/>
                                        <GradientStop Color="#FF9191AB" Offset="1"/>
                                    </LinearGradientBrush>
                                </Rectangle.Fill>
                            </Rectangle>

                            <Grid Grid.Row="1">
                                <Grid.Background>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="#FFEDEDF5" Offset="0"/>
                                        <GradientStop Color="#FFC4C3D7" Offset="1"/>
                                    </LinearGradientBrush>
                                </Grid.Background>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="40"/>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="20"/>
                                </Grid.ColumnDefinitions>
                                <Button x:Name="closeButton" Grid.Column="2" VerticalAlignment="Top" Margin="1, 3" Width="16" Height="13">
                                    <Button.Template>
                                        <ControlTemplate TargetType="Button">
                                            <Image Source="x.png"/>
                                        </ControlTemplate>
                     开发者_Go百科               </Button.Template>
                                </Button>
                                <StackPanel Grid.Column="1" Margin="5, 7">
                                    <TextBlock Text="{TemplateBinding Header}" FontFamily="Verdana" FontWeight="Bold" FontSize="11"/>
                                    <TextBlock Text="{TemplateBinding Text}" FontFamily="Verdana" FontSize="11" TextWrapping="Wrap"/>
                                </StackPanel>
                            </Grid>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Here goes my CustomNotification class code -

 public class CustomNotification : ContentControl
    {
        public CustomNotification()
        {
            this.DefaultStyleKey = typeof(CustomNotification);
        }

        public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register(
            "Header",
            typeof(string),
            typeof(CustomNotification),
            new PropertyMetadata(OnHeaderPropertyChanged));

        /// <summary>
        ///   Gets or sets a value that indicates whether 
        ///   the <see cref="P:System.Windows.Controls.Label.Target" /> field is required. 
        /// </summary>
        public string Header
        {
            get
            {
                return (string)GetValue(CustomNotification.HeaderProperty);
            }

            set
            {
                SetValue(CustomNotification.HeaderProperty, value);
            }
        }

        private static void OnHeaderPropertyChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
        {
        }

        public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
            "Text",
            typeof(string),
            typeof(CustomNotification),
            new PropertyMetadata(OnTextPropertyChanged));

        /// <summary>
        ///   Gets or sets a value that indicates whether 
        ///   the <see cref="P:System.Windows.Controls.Label.Target" /> field is required. 
        /// </summary>
        public string Text
        {
            get
            {
                return (string)GetValue(CustomNotification.TextProperty);
            }

            set
            {
                SetValue(CustomNotification.TextProperty, value);
            }
        }

        private static void OnTextPropertyChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
        {
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            Button closeButton = GetTemplateChild("closeButton") as Button;
            if (closeButton != null)
            {
                closeButton.Click += new RoutedEventHandler(closeButton_Click);
            }
        }


        public event EventHandler<EventArgs> Closed;

        void closeButton_Click(object sender, RoutedEventArgs e)
        {
            EventHandler<EventArgs> handler = this.Closed;
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }


I assume your CustomNotification class is just a user control (i.e. inherits directly from UserControl)? Assuming so, this line:

notify.Content = tb;

is wrong. I don't know what tb is, but it should be:

notify.Content = custom;

Then you'll see the content.

Note that you have to be careful to not try and show a notification whilst another one is currently being displayed. You'll get an exception, so you'll need to keep track of this.

Hope this helps...

Chris Anderson

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜