ironpython - 如何将 MahApps Ui 库安装到 IronPython 项目中
问题描述
我正在尝试在 IronPython WPF 项目上从https://mahapps.com安装 MahApps.metro。由于我无法使用 nugget 安装它,因为 Visual Studio 只是不给我这个选项,我该如何导入和使用这个库?
我尝试通过 nugget 控制台安装,但也收到错误 Install-Package MahApps.Metro -Version 1.6.5
安装包:找不到项目“默认”。在 line:1 char:1 + Install-Package MahApps.Metro -Version 1.6.5 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Default:String) [Install-Package], ItemNotFoundException + FullyQualifiedErrorId : NuGetProjectNotFound,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand
Python
import wpf
from System.Windows import Application, Window
import clr
clr.AddReferenceToFile("MahApps.Metro.dll")
import MahApps.Metro
from MahApps.Metro import Controls
class MyWindow(Controls.MetroWindow):
def __init__(self):
wpf.LoadComponent(self, 'DemoApp.xaml')
if __name__ == '__main__':
Application().Run(MyWindow())
XAML
<Controls:MetroWindow
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="DemoApp" Height="300" Width="300">
<Grid></Grid>
</Controls:MetroWindow>
它构建并似乎正确加载了库,但在启动时崩溃,
“ipyw64.exe”(CLR v4.0.30319:ipyw64.exe):加载“C:\Users\Rodrigo.Queiroz\source\PoCs\IPython_WPFInterface\WpfIronPy\DemoApp\MahApps.Metro.dll”。已加载符号。
抛出的异常:mscorlib.dll 中的“System.IO.FileNotFoundException” 抛出的异常:Microsoft.Dynamic.dll 中的“System.Reflection.ReflectionTypeLoadException” 抛出的异常:mscorlib.dll 中的“System.IO.FileNotFoundException” 抛出的异常:“System. Microsoft.Dynamic.dll 中的 Reflection.ReflectionTypeLoadException'
解决方案
最后我想出了如何将样式项目导入我的应用程序
步骤: 1 - Nugget 包:为了从 Nugget 下载 MahApps.Metro,在解决方案中创建了一个新的 C# 库项目,现在包文件夹是这些库的路径
2 - MahApps.Metro 安装:下载后,您必须使用 clr.AddReferenceToFileAndPath 将它们导入您的 Python 代码,如下面的 Python 文件所示
3 - 在你的 Python 类中设置正确的类型:设置 MetroWindow 类型,而不是你的类继承中的 Window
4 - 在 XAML 文件中设置 xmlns:在 XAML 中设置 xmlns,如下面的 XAML 代码所示,例如:xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
5 - 更改 XAML 中的控件类型:将 xaml 中的控件类型从 Window 更改为 Controls:MetroWindow
这个设置足以让事情正常进行
蟒蛇代码:
import wpf
import clr
clr.AddReferenceToFileAndPath("C:\\Users\\Rodrigo.Queiroz\\source\\PoCs\\WPFIronPython\\packages\\ControlzEx.3.0.2.4\\lib\\net45\\ControlzEx.dll")
clr.AddReferenceToFileAndPath("C:\\Users\\Rodrigo.Queiroz\\source\\PoCs\\WPFIronPython\\packages\\ControlzEx.3.0.2.4\\lib\\net45\\System.Windows.Interactivity.dll")
clr.AddReferenceToFileAndPath('C:\\Users\\Rodrigo.Queiroz\\source\\PoCs\\WPFIronPython\\packages\\MahApps.Metro.1.6.5\\lib\\net45\\MahApps.Metro.dll')
from MahApps.Metro.Controls import MetroWindow
from System.Windows import Application, Window
class PageLoader(MetroWindow):
def __init__(self , page):
ui = wpf.LoadComponent(self, page)
def HamburgerMenuControl_OnItemClick(self,sender,e):
self.HamburgerMenuControl.Content = e.ClickedItem
#self.HamburgerMenuControl.IsPaneOpen = False
if __name__ == '__main__':
Application().Run(PageLoader('C:\Users\Rodrigo.Queiroz\source\PoCs\WPFIronPython\WpfApplication1\WpfApplication1.xaml'))
XAML 代码:
<Controls:MetroWindow
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WpfApplication1" Height="300" Width="300">
<Controls:MetroWindow.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Themes/HamburgerMenuTemplate.xaml" />
<!-- Accent and AppTheme setting -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
<DataTemplate x:Key="MenuItemTemplate" DataType="{x:Type Controls:HamburgerMenuGlyphItem}">
<Grid Height="48">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="48" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0"
FontSize="16"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontFamily="Segoe MDL2 Assets"
Foreground="White"
Text="{Binding Glyph}" />
<TextBlock Grid.Column="1"
VerticalAlignment="Center"
FontSize="16"
Foreground="White"
Text="{Binding Label}" />
</Grid>
</DataTemplate>
</ResourceDictionary>
</Controls:MetroWindow.Resources>
<Controls:HamburgerMenu x:Name="HamburgerMenuControl"
Foreground="White"
PaneBackground="#FF444444"
IsPaneOpen="False"
ItemTemplate="{StaticResource MenuItemTemplate}"
OptionsItemTemplate="{StaticResource MenuItemTemplate}"
ItemClick="HamburgerMenuControl_OnItemClick"
OptionsItemClick="HamburgerMenuControl_OnItemClick"
DisplayMode="CompactInline">
<Controls:HamburgerMenu.ContentTemplate>
<DataTemplate DataType="{x:Type Controls:HamburgerMenuItem}">
<Grid x:Name="TheContentGrid">
<Grid.RowDefinitions>
<RowDefinition Height="48" />
<RowDefinition />
</Grid.RowDefinitions>
<Border Grid.Row="0"
Background="#FF444444">
<TextBlock x:Name="Header"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="24"
Foreground="White"
Text="{Binding Label}" />
</Border>
<ContentControl x:Name="TheContent"
Grid.Row="1"
Focusable="False"
Foreground="{DynamicResource BlackBrush}"
Content="{Binding Tag}" />
</Grid>
</DataTemplate>
</Controls:HamburgerMenu.ContentTemplate>
<Controls:HamburgerMenu.ItemsSource>
<Controls:HamburgerMenuItemCollection>
<Controls:HamburgerMenuGlyphItem Glyph="" Label="Home">
<Controls:HamburgerMenuGlyphItem.Tag>
<Label Content="This is a Label control1." />
</Controls:HamburgerMenuGlyphItem.Tag>
</Controls:HamburgerMenuGlyphItem>
<Controls:HamburgerMenuGlyphItem Glyph="" Label="Private">
<Controls:HamburgerMenuGlyphItem.Tag>
<Label Content="This is a Label control2." />
</Controls:HamburgerMenuGlyphItem.Tag>
</Controls:HamburgerMenuGlyphItem>
<Controls:HamburgerMenuGlyphItem Glyph="" Label="Settings">
<Controls:HamburgerMenuGlyphItem.Tag>
<Label Content="This is a Label control3." />
</Controls:HamburgerMenuGlyphItem.Tag>
</Controls:HamburgerMenuGlyphItem>
</Controls:HamburgerMenuItemCollection>
</Controls:HamburgerMenu.ItemsSource>
<!-- Options -->
<Controls:HamburgerMenu.OptionsItemsSource>
<Controls:HamburgerMenuItemCollection>
<Controls:HamburgerMenuGlyphItem Glyph="" Label="About">
<Controls:HamburgerMenuGlyphItem.Tag>
</Controls:HamburgerMenuGlyphItem.Tag>
</Controls:HamburgerMenuGlyphItem>
</Controls:HamburgerMenuItemCollection>
</Controls:HamburgerMenu.OptionsItemsSource>
</Controls:HamburgerMenu>
</Controls:MetroWindow>
推荐阅读
- gradle - 任务“:bootRun”执行失败
- vb.net - Ftp 应用 Chilkat Ftp2 不启动 ERR CLR20r3
- python-3.x - 我想检查所有其他项目中特定项目的出现(甚至是子字符串)
- javascript - 在正式的“[“Admin”]”中从 .NET 核心 API 获取角色。我如何将其转换为字符串?
- visual-studio - QT Visual Studio 工具总是创建 x86 Qt 项目
- wordpress - Wordpress - 在域根目录设置 wp-admin
- html - 淘汰赛div在foreach中滚动
- sql - 如何通过 Hana SP12 中的 Query 满足以下要求
- android - Yandex 地图套件 Android
- android - Android 条纹集成