我如何将MahApps Ui库安装到IronPython项目中

问题描述 投票:1回答:1

我正在尝试在IronPython WPF项目上从https://mahapps.com安装MahApps.metro。由于Visual Studio只是不给我这个选项,所以我无法使用块安装,所以如何导入和使用该库?

我尝试通过块控制台进行安装,但也出现错误安装软件包MahApps.Metro-版本1.6.5

安装包:未找到项目'默认'。在第1行:char:1+安装软件包MahApps.Metro-版本1.6.5+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo:ObjectNotFound :(默认:字符串)[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”引发异常:Microsoft.Dynamic.dll中的“ System.Reflection.ReflectionTypeLoadException”

ironpython mahapps.metro
1个回答
1
投票

最后我弄清楚了如何将样式项目导入到我的应用程序中

步骤:1-块金套餐:为了从Nugget下载MahApps.Metro,在解决方案中创建了一个新的C#库项目,packages文件夹现在是这些库的路径

2-MahApps.Metro安装:下载后,您必须使用clr.AddReferenceToFileAndPath将它们导入到您的python代码中,如下面的python文件所示

3-在您的Python类中设置正确的类型:设置MetroWindow类型,而不是在类继承上使用Window

4-在XAML文件上设置xmlns:如以下XAML代码所示,在XAML中设置xmlns:xmlns:Controls =“ clr-namespace:MahApps.Metro.Controls; assembly = MahApps.Metro”

5-更改XAML中的控件类型:将xaml中的控件类型从Window更改为Controls:MetroWindow

此设置足以使工作正常

Python代码:

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>

© www.soinside.com 2019 - 2024. All rights reserved.