1

アプリケーションの通知ウィンドウを開発しています。この通知は、メイン ウィンドウの右下にポップアップするウィンドウです。通知をメイン ウィンドウの上部に保持するために、通知の所有者を設定します。

this.Owner = mainWindow;

しかし、私の問題は、メイン ウィンドウをクリックできないことです。この問題を解決する方法を知っていますか。

編集 :

私は.Net 4.0を使用しています

私のXAML:

<Window
    x:Class="ProjectTest.Resources.NotificationsWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
    xmlns:dxt="clr-namespace:DevExpress.Xpf.Utils.Themes;assembly=DevExpress.Xpf.Core.v12.2"
    xmlns:p="clr-namespace:ProjectTest.Properties"
    Title="NotificationsWindow"
    ShowInTaskbar="False"
    x:Name="me"
    WindowStyle="None" 
    ResizeMode="NoResize" 
    SizeToContent="Height"
    MinHeight="120"
    AllowsTransparency="True"
    Closing="me_Closing"
    WindowStartupLocation="Manual"
    Height="120"
    dx:ThemeManager.ThemeName="{Binding Source={x:Static p:Settings.Default}, Path=ThemeName}"
    Background="{DynamicResource {dxt:FloatingContainerThemeKey ResourceKey=FloatingContainerBackground, ThemeName=MetropolisDark}}"
    FlowDirection="{Binding Source={x:Static p:Settings.Default}, Path=FlowDirection, Mode=TwoWay}"
    Width="240"
    MouseMove="me_MouseMove">
    <Window.Triggers>
        <EventTrigger RoutedEvent="Window.Unloaded">
            <BeginStoryboard>
                <Storyboard x:Name="boardUnloaded" Completed="boardUnloaded_Completed">
                    <DoubleAnimation Storyboard.TargetName="me" Storyboard.TargetProperty="Opacity" From="1.0" To="0" Duration="0:0:1" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
        <EventTrigger RoutedEvent="Window.Loaded">
            <BeginStoryboard>
                <Storyboard x:Name="boardLoaded">
                    <DoubleAnimation Storyboard.TargetName="me" Storyboard.TargetProperty="Opacity" From="0" To="1.0" Duration="0:0:1" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Window.Triggers>
    <Grid>
        <StackPanel>
        <Image Source="/ProjectTest;component/Assets/ContextMenu/Delete.png" Stretch="Uniform" HorizontalAlignment="Right" VerticalAlignment="Top" Height="16" Margin="4"
               MouseUp="Image_MouseUp" Cursor="Hand"/>
            <Label x:Name="MessageTextBlock"/>
        </StackPanel>
    </Grid>
</Window>

私のC# :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Shapes;
using DevExpress.Xpf.Core;
using System.Windows.Threading;


namespace ProjectTest.Resources
{
    /// <summary>
    /// Interaction logic for NotificationsWindow.xaml
    /// </summary>
    public partial class NotificationsWindow : Window
    {
        Window mainWindow = Application.Current.MainWindow;
        int countTimer = 10;
        int defaultTimer;

        public NotificationsWindow(int timer, string message)
        {
            InitializeComponent();
            MessageTextBlock.Content = message;
            defaultTimer = timer;
            countTimer = defaultTimer;

            this.Owner = mainWindow;
            mainWindow.Focus();

            Loaded += (o, e) =>
            {
                mainWindow.LocationChanged += mainWindow_LocationChanged;
                mainWindow.SizeChanged += mainWindow_SizeChanged;

                UpdatePosition();

                DispatcherTimer DispatcherTimer = new DispatcherTimer();
                DispatcherTimer.Interval = TimeSpan.FromMilliseconds(1000);

                DispatcherTimer.Tick += delegate
                {
                    countTimer--;
                    if (countTimer < 0)
                        CloseNotification();
                };

                DispatcherTimer.Start();
            };
        }

        public static void Show(int timer, string message)
        {
            var window = new NotificationsWindow(timer, message);
            window.ShowDialog();
        }

        void mainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            UpdatePosition();
        }

        void mainWindow_LocationChanged(object sender, EventArgs e)
        {
            UpdatePosition();
        }

        private void UpdatePosition()
        {
            if (PresentationSource.FromVisual(this) == null)
                return;
            try
            {
                var transform = PresentationSource.FromVisual(this).CompositionTarget.TransformFromDevice;
                var MainWindowPosition = mainWindow.GetPosition();

                var corner = transform.Transform(new Point(MainWindowPosition.X + mainWindow.Width, MainWindowPosition.Y + mainWindow.Height));

                this.Left = corner.X - this.ActualWidth - 10;
                this.Top = corner.Y - this.ActualHeight - 10;
            }
            catch (Exception e)
            {
            }
        }

        bool FinalClose = false;
        private void Image_MouseUp(object sender, MouseButtonEventArgs e)
        {
            FinalClose = true;
            CloseNotification();
        }

        public void CloseNotification()
        {
            this.Close();
        }

        private bool closeStoryBoardCompleted = false;

        private void me_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (countTimer > 0 && !FinalClose)
            {
                e.Cancel = true;
                return;
            }

            if (!closeStoryBoardCompleted)
            {
                boardUnloaded.Begin();
                e.Cancel = true;
            }

        }

        private void boardUnloaded_Completed(object sender, EventArgs e)
        {
            mainWindow.Focus();
            closeStoryBoardCompleted = true;
            this.Close();

        }

        private void me_MouseMove(object sender, MouseEventArgs e)
        {
            if (FinalClose)
                return;

            boardUnloaded.Seek(new TimeSpan());
            boardUnloaded.Stop();
            countTimer = defaultTimer;
        }

    }
}
4

1 に答える 1

2

以前のコメントを回答に変換します。

.ShowDialog() で通知ウィンドウを表示していますか? その場合は .Show() に切り替えて、同じことを試してください

Ownera のプロパティを設定するだけWindowではモーダルにはなりません。その部分を行うのShowDialog()は です。

于 2013-06-21T16:44:27.203 に答える