英文:
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:
<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>
答案1
得分: 0
是的,您可以使用一个名为UWP Host的包来实现它。
步骤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
<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
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/UWPHost;component/Themes/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
<br>
<b>STEP 3:</b>
<br><br>
Add this namespace to the window that you want to change
<br>
xmlns:uwp="clr-namespace:UWPHost;assembly=UWPHost"
<br>
<b>STEP 4:</b>
<br>
<br>
Add uwp prefix to the window tag and make sure that it look similar to this
<br>
<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>
<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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论