也许metro wpf样式在类库中不起作用

本文关键字:类库 不起作用 样式 metro wpf 也许 | 更新日期: 2023-09-27 18:08:12

我在解决方案中使用MVVM模式。主应用程序有一个窗口,该窗口继承了其他用户控件和类库中的一些函数。例如在用户控件中,我有一个树视图,它有一个上下文菜单,树视图不继承mahapps metro风格但上下文菜单继承了。我需要做什么?

下面是我的结构示例:

MainApp:

->Model
->View   ->MainView.xaml
->ViewModel
->App.xaml

MainView。xaml代码:

        <Controls:MetroWindow x:Class="MainApp.View.MainView"
                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:uc="clr-namespace:ClassLibrary.View;assembly=ClassLibrary"
                xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"            
                Title="MainView" Height="500" Width="800" MinWidth="600" MinHeight="400"
                EnableDWMDropShadow="True"
                ResizeMode="CanResizeWithGrip"
                WindowTransitionsEnabled="False"
                WindowStartupLocation="CenterScreen">
            <Grid>
                <Grid Margin="184,0,0,0">
                    <uc:UserControlView/>
                </Grid>
            </Grid>
        </Controls:MetroWindow>

应用程序。xaml代码:

    <Application x:Class="MainApp.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="Views/LoginWindow.xaml">
        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="Resources/Icons.xaml" />
                    <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" />
                    <!-- accent resource -->
                    <!-- change "Cobalt" to the accent color you want -->
                    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Red.xaml" />
                    <!-- theme resource -->
                    <!-- change "BaseLight" to the theme you want -->
                    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>
    </Application>

ClassLibrary:

->Model
->View
->UserControlView.xaml
->ViewModel

UserControlView。xaml代码:

<UserControl x:Class="ClassLibrary.View.UserControlView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
             mc:Ignorable="d" 
             d:DesignHeight="350" d:DesignWidth="525">
    <Grid>
        <Label x:Name="label_selected" HorizontalAlignment="Left" Margin="252,10,0,0" VerticalAlignment="Top" Width="85" Height="26" Content="Selected Item: "/>
        <TreeView Margin="0,0,286,10">
            <TreeViewItem Header="Hello">
                <TreeViewItem Header="Hello"></TreeViewItem>
                <TreeViewItem Header="Hello"></TreeViewItem>
            </TreeViewItem>
            <TreeViewItem Header="Hello"></TreeViewItem>
            <TreeViewItem Header="Hello"></TreeViewItem>
            <TreeViewItem Header="Hello"></TreeViewItem>
        </TreeView>
        <Label x:Name="label_selectedItem" HorizontalAlignment="Left" Margin="337,10,0,0" VerticalAlignment="Top" Width="170" Height="26" Content="{Binding CurrentItem.Item, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>
    </Grid>
</UserControl>

也许metro wpf样式在类库中不起作用

我发现了这个问题…我在UserControlView中定义了用户控件资源。现在运行得很好

<UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="../Resources/Icons.xaml" />
                <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/Styles/Accents/Blue.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseDark.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>