如何在WPF中创建一个顶级窗口,使其看起来像一个“UWP”应用程序?

huangapple go评论89阅读模式
英文:

How to create a top-level window in WPF that looks like a "UWP" app?

问题

我需要创建一个类似于以下窗口的WPF自定义窗口:
这个窗口

有没有办法可以做到这一点?

我尝试使用MahApps.Metro Window,但在调整大小时窗口会闪烁很多。
这是示例代码:

<Controls:MetroWindow x:Class="MahAppsMetroSample.MainWindow"
                      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                      xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
                      xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
                      Title="MahApps.Metro.Sample"
                      GlowBrush="{DynamicResource AccentColorBrush}"
                      WindowStartupLocation="CenterScreen">

  <Controls:MetroWindow.RightWindowCommands>
    <Controls:WindowCommands>
      <Button Content="settings" />
      <Button>
        <StackPanel Orientation="Horizontal">
          <iconPacks:PackIconModern Width="24" Height="24" Kind="FoodCupcake" />
          <TextBlock Margin="4 0 0 0" VerticalAlignment="Center" Text="deploy cupcakes" />
        </StackPanel>
      </Button>
    </Controls:WindowCommands>
  </Controls:MetroWindow.RightWindowCommands>

  <Grid>
  </Grid>

</Controls:MetroWindow>
英文:

I need to create a WPF custom window which looks similar to:
this window

is there any way i could do that ?

I tried using MahApps.Metro Window but the window was very much flickering while resizing.
here is the sample code:

 &lt;Controls:MetroWindow x:Class=&quot;MahAppsMetroSample.MainWindow&quot;
                      xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
                      xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
                      xmlns:Controls=&quot;http://metro.mahapps.com/winfx/xaml/controls&quot;
                      xmlns:iconPacks=&quot;http://metro.mahapps.com/winfx/xaml/iconpacks&quot;
                      Title=&quot;MahApps.Metro.Sample&quot;
                      GlowBrush=&quot;{DynamicResource AccentColorBrush}&quot;
                      WindowStartupLocation=&quot;CenterScreen&quot;&gt;

  &lt;Controls:MetroWindow.RightWindowCommands&gt;
    &lt;Controls:WindowCommands&gt;
      &lt;Button Content=&quot;settings&quot; /&gt;
      &lt;Button&gt;
        &lt;StackPanel Orientation=&quot;Horizontal&quot;&gt;
          &lt;iconPacks:PackIconModern Width=&quot;24&quot; Height=&quot;24&quot; Kind=&quot;FoodCupcake&quot; /&gt;
            &lt;TextBlock Margin=&quot;4 0 0 0&quot; VerticalAlignment=&quot;Center&quot; Text=&quot;deploy cupcakes&quot; /&gt;
        &lt;/StackPanel&gt;
      &lt;/Button&gt;
    &lt;/Controls:WindowCommands&gt;
  &lt;/Controls:MetroWindow.RightWindowCommands&gt;

  &lt;Grid&gt;
  &lt;/Grid&gt;

&lt;/Controls:MetroWindow&gt;

答案1

得分: 0

是的,您可以使用一个名为UWP Host的包来实现它。

UWP Host - Github

UWP Host - Nuget

步骤1:您只需通过NuGet将UWP Host导入到您的项目中。

步骤2:将以下代码添加到您的App.xaml文件

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/UWPHost;component/Themes/Generic.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

步骤3:将此命名空间添加到您想要更改的窗口中

xmlns:uwp="clr-namespace:UWPHost;assembly=UWPHost"

步骤4:将uwp前缀添加到窗口标记,并确保它看起来类似于这样

<upw:Window x:Class="WpfApp1.MainWindow"
    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:WpfApp1"
    xmlns:uwp="clr-namespace:UWPHost;assembly=UWPHost"
    mc:Ignorable="d"
    ShowTitlebar="true" Theme="Light"
    Title="MainWindow" Height="300" Width="300">
</uwp:Window>

步骤5:继承UWPHost.Window类

public partial class MainWindow : UWPHost.Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
}

这就是全部了。

英文:

Yes you can implement it using a package know as UWP Host.<br><br>
UWP Host - Github

UWP Host - Nuget

<br>
<br>
<b>STEP 1:</b><br><br>
All you need to do is import UWP Host into your project through nuget.
<br><br>
<b>STEP 2:</b>
<br><br>
Add this code to your App.xaml file

&lt;Application.Resources&gt;
  &lt;ResourceDictionary&gt;
       &lt;ResourceDictionary.MergedDictionaries&gt;
           &lt;ResourceDictionary Source=&quot;pack://application:,,,/UWPHost;component/Themes/Generic.xaml&quot; /&gt;
       &lt;/ResourceDictionary.MergedDictionaries&gt;
  &lt;/ResourceDictionary&gt;

</Application.Resources>

<br>
<b>STEP 3:</b>
<br><br>
Add this namespace to the window that you want to change
<br>

xmlns:uwp=&quot;clr-namespace:UWPHost;assembly=UWPHost&quot;

<br>
<b>STEP 4:</b>
<br>
<br>
Add uwp prefix to the window tag and make sure that it look similar to this
<br>

&lt;upw:Window x:Class=&quot;WpfApp1.MainWindow&quot;
        xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
        xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
        xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
        xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
        xmlns:local=&quot;clr-namespace:WpfApp1&quot;
        xmlns:uwp=&quot;clr-namespace:UWPHost;assembly=UWPHost&quot;
        mc:Ignorable=&quot;d&quot;
        ShowTitlebar=&quot;true&quot; Theme=&quot;Light&quot;
        Title=&quot;MainWindow&quot; Height=&quot;300&quot; Width=&quot;300&quot;&gt;

&lt;/uwp:Window&gt;

<br>
<br><b>Step 5</b>
<br><br>
Inherit the UWPHost.Window class
<br>
eg:

public partial class MainWindow : UWPHost.Window{
   public MainWindow()
   {
       InitializeComponent();
   }
}

<b>And That's it</b>
1: https://github.com/Selastin-George/UWP-Host
2: https://nuget.org/packages/UWPHost

huangapple
  • 本文由 发表于 2020年1月7日 01:49:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616674.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定