XAML 如何使我的WPF应用程序在所有东西之上,甚至是Windows任务栏(如Windows时钟应用程序)?

6tqwzwtp  于 2023-08-01  发布在  Windows
关注(0)|答案(1)|浏览(132)

我用WPF做了一个小计时器应用程序,我想把我的应用程序放在任务栏上,但是当我点击任务栏上的任何地方时,我的应用程序失去焦点,任务栏出现在我的应用程序的顶部,即使我在XAML代码中有Topmost=True
我的应用程序的行为:


的数据
和Windows时钟应用程序的行为正是我想要的方式:🥺



有什么方法可以在我的WPF应用程序中实现此行为吗?
XAML代码:

<Window x:Class="MultiStopwatch.StopwatchWindow"
        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:utility="clr-namespace:MultiStopwatch.Utility"
        mc:Ignorable="d"
        Title="MultiStopwatchWindow"
        WindowStyle="None" AllowsTransparency="True" Background="Transparent" ResizeMode="NoResize"
        ShowInTaskbar="False" Topmost="True" Height="28" Width="112"
        utility:EnableDragHelper.EnableDrag="True">
  <Window.Clip>
    <RectangleGeometry Rect="0,0,112,28" RadiusX="5.5" RadiusY="5.5" />
  </Window.Clip>
  <Window.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="assets/Icons.xaml"/>
      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
  </Window.Resources>
  <Grid>
    <Border Background="Black" CornerRadius="4">
      <Grid>
        <Button x:Name="StartBtn" Margin="0,0,83,0" Width="21" Height="21" Cursor="Hand"
                Click="StartBtn_OnClick">
          <Button.Template>
            <ControlTemplate TargetType="Button">
              <Border x:Name="StartBtnBorder" Background="#333333" CornerRadius="3" Padding="3.5"
                      RenderTransformOrigin="0.5,0.5">
                <Border.RenderTransform>
                  <ScaleTransform />
                </Border.RenderTransform>
                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
              </Border>
              <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                  <Trigger.EnterActions>
                    <BeginStoryboard>
                      <Storyboard>
                        <ColorAnimation Storyboard.TargetName="StartBtnBorder"
                                        Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
                                        To="#454545" Duration="0:0:0.13" />
                      </Storyboard>
                    </BeginStoryboard>
                  </Trigger.EnterActions>
                  <Trigger.ExitActions>
                    <BeginStoryboard>
                      <Storyboard>
                        <ColorAnimation Storyboard.TargetName="StartBtnBorder"
                                        Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
                                        To="#333333" Duration="0:0:0.13" />
                      </Storyboard>
                    </BeginStoryboard>
                  </Trigger.ExitActions>
                </Trigger>
                <EventTrigger RoutedEvent="Button.Click">
                  <BeginStoryboard>
                    <Storyboard>
                      <DoubleAnimationUsingKeyFrames Storyboard.TargetName="StartBtnBorder"
                                                     Storyboard.TargetProperty="(Border.RenderTransform).(ScaleTransform.ScaleX)">
                        <EasingDoubleKeyFrame KeyTime="0:0:0" Value="0.9" />
                        <EasingDoubleKeyFrame KeyTime="0:0:0.09" Value="1" />
                      </DoubleAnimationUsingKeyFrames>
                      <DoubleAnimationUsingKeyFrames Storyboard.TargetName="StartBtnBorder"
                                                     Storyboard.TargetProperty="(Border.RenderTransform).(ScaleTransform.ScaleY)">
                        <EasingDoubleKeyFrame KeyTime="0:0:0" Value="0.9" />
                        <EasingDoubleKeyFrame KeyTime="0:0:0.09" Value="1" />
                      </DoubleAnimationUsingKeyFrames>
                    </Storyboard>
                  </BeginStoryboard>
                </EventTrigger>
              </ControlTemplate.Triggers>
            </ControlTemplate>
          </Button.Template>
          <Grid>
            <Image x:Name="StartBtnIcon" Source="{StaticResource StartDrawingImage}" />
          </Grid>
        </Button>
        <Button x:Name="ResetButton" Margin="0,0,37,0" Width="21" Height="21" Cursor="Hand"
                Click="ResetButton_OnClick">
          <Button.Template>
            <ControlTemplate TargetType="Button">
              <Border x:Name="ResetBtnBorder" Background="#333333" CornerRadius="3" Padding="3"
                      RenderTransformOrigin="0.5,0.5">
                <Border.RenderTransform>
                  <ScaleTransform />
                </Border.RenderTransform>
                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
              </Border>
              <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                  <Trigger.EnterActions>
                    <BeginStoryboard>
                      <Storyboard>
                        <ColorAnimation Storyboard.TargetName="ResetBtnBorder"
                                        Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
                                        To="#454545" Duration="0:0:0.15" />
                      </Storyboard>
                    </BeginStoryboard>
                  </Trigger.EnterActions>
                  <Trigger.ExitActions>
                    <BeginStoryboard>
                      <Storyboard>
                        <ColorAnimation Storyboard.TargetName="ResetBtnBorder"
                                        Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
                                        To="#333333" Duration="0:0:0.13" />
                      </Storyboard>
                    </BeginStoryboard>
                  </Trigger.ExitActions>
                </Trigger>
                <EventTrigger RoutedEvent="Button.Click">
                  <BeginStoryboard>
                    <Storyboard>
                      <DoubleAnimationUsingKeyFrames Storyboard.TargetName="ResetBtnBorder"
                                                     Storyboard.TargetProperty="(Border.RenderTransform).(ScaleTransform.ScaleX)">
                        <EasingDoubleKeyFrame KeyTime="0:0:0" Value="0.9" />
                        <EasingDoubleKeyFrame KeyTime="0:0:0.09" Value="1" />
                      </DoubleAnimationUsingKeyFrames>
                      <DoubleAnimationUsingKeyFrames Storyboard.TargetName="ResetBtnBorder"
                                                     Storyboard.TargetProperty="(Border.RenderTransform).(ScaleTransform.ScaleY)">
                        <EasingDoubleKeyFrame KeyTime="0:0:0" Value="0.9" />
                        <EasingDoubleKeyFrame KeyTime="0:0:0.09" Value="1" />
                      </DoubleAnimationUsingKeyFrames>
                    </Storyboard>
                  </BeginStoryboard>
                </EventTrigger>
              </ControlTemplate.Triggers>
            </ControlTemplate>
          </Button.Template>
          <Grid>
            <Image Source="{StaticResource ResetDrawingImage}" />
          </Grid>
        </Button>
        <TextBox x:Name="FirstStopwatchTextBox" IsReadOnly="True" SelectionBrush="Transparent"
                 FontSize="12" BorderThickness="0" Cursor="SizeAll" TextAlignment="Center"
                 FontFamily="JetBrains Mono" FontWeight="SemiBold" Foreground="#DCDCDC"
                 Background="Transparent" Height="15" Margin="48,6,0,6" Text="00:00:00" />
      </Grid>
    </Border>
  </Grid>
</Window>

字符串
编辑:
我试过使用Window.Activate();,但它只适用于第一次点击任务栏,最重要的是,它不能很好地工作。它强制将焦点放在应用程序上,并且不允许在第一次点击时将焦点转移到另一个应用程序,这真的很糟糕。在下面的gif中,我试图展示它的行为。请注意,在第一次单击时,应用程序仍然在顶部,但在第二次单击时,应用程序再次位于任务栏后面。请注意,第一次单击时焦点没有转到Visual Studio上,但当我再次单击时,文本光标(插入符号)出现在我的代码上,焦点现在完全在Visual Studio上。Windows时钟应用程序不会发生这种情况,如果您在应用程序外部单击一次,焦点会立即转移到其他应用程序(尽管它仍然位于所有内容的顶部)。


uklbhaso

uklbhaso1#

DeActivated Event添加以下代码XAML:

<Window Deactivated="Window_Deactivated" >

字符串
C#

private void Window_Deactivated(object sender, EventArgs e) 
    {
       Window window = (Window)sender;
       window.Topmost = true;
       window.Activate();
    }


Original Source

相关问题