首页 > 解决方案 > WPF 应用程序 .Core 3.0 和 microsoft 功能区控件库 4.0

问题描述

您好,我尝试使用 WPF、.Netcore 3.0 和 microsoft 功能区控件库 4.0 构建应用程序,但失败了。

我需要帮助或网络上的示例。我确实在 GitHub 上查看了Microsoft WPF-Sample,但没有找到适用于 .netcore 3.0 的功能区示例

主窗口.Xaml

<Window x:Class="WpfDoNetcore3.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:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
        xmlns:local="clr-namespace:WpfDoNetcore3"
        mc:Ignorable="d"
        x:Name="RibbonWindow"
        Title="MainWindow" Height="450" Width="800">

    <Grid x:Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <ribbon:Ribbon x:Name="Ribbon" Title="Ribbon Title">
            <ribbon:Ribbon.HelpPaneContent>
                <ribbon:RibbonButton SmallImageSource="Images\Smallicon.png" />
            </ribbon:Ribbon.HelpPaneContent>
            <ribbon:Ribbon.QuickAccessToolBar>
                <ribbon:RibbonQuickAccessToolBar >
                    <ribbon:RibbonButton x:Name="QATButton1" 
                                         SmallImageSource="Images\Smallicon.png" />
                    <ribbon:RibbonButton x:Name="QATButton2" 
                                         SmallImageSource="Images\Smallicon.png" />
                </ribbon:RibbonQuickAccessToolBar>
            </ribbon:Ribbon.QuickAccessToolBar>
            <ribbon:Ribbon.ApplicationMenu>
                <ribbon:RibbonApplicationMenu SmallImageSource="Images\Smallicon.png">
                    <ribbon:RibbonApplicationMenuItem Header="Hello _Ribbon"
                                                      x:Name="MenuItem1"
                                                      ImageSource="Images\Largeicon.png"/>
                </ribbon:RibbonApplicationMenu>
            </ribbon:Ribbon.ApplicationMenu>
            <ribbon:RibbonTab x:Name="HomeTab" 
                              Header="Home">
                <ribbon:RibbonGroup x:Name="Group1" 
                                    Header="Group1">
                    <ribbon:RibbonButton x:Name="Button1"
                                         LargeImageSource="Images\Largeicon.png"
                                         Label="Button1" />
                    <ribbon:RibbonButton x:Name="Button2"
                                         SmallImageSource="Images\Smallicon.png"
                                         Label="Button2" />
                    <ribbon:RibbonButton x:Name="Button3"
                                         SmallImageSource="Images\Smallicon.png"
                                         Label="Button3" />
                    <ribbon:RibbonButton x:Name="Button4"
                                         SmallImageSource="Images\Smallicon.png"
                                         Label="Button4" />
                </ribbon:RibbonGroup>
            </ribbon:RibbonTab>
        </ribbon:Ribbon>
    </Grid>

</Window>

主窗口.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Ribbon;

namespace WpfDoNetcore3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {


            InitializeComponent();
        }
    }
}

参考:Github 项目

标签: c#wpf.net-core-3.0ribboncontrolslibrary

解决方案


只需从 XAML 中删除xmlns:ribbon命名空间声明和ribbon:前缀并尝试构建:

<Window x:Class="WpfDoNetcore3.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"
        mc:Ignorable="d"
        x:Name="RibbonWindow"
        Title="MainWindow" Height="450" Width="800">
    <Grid x:Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Ribbon x:Name="Ribbon" Title="Ribbon Title">
            <Ribbon.HelpPaneContent>
                <RibbonButton SmallImageSource="Images\Smallicon.png" />
            </Ribbon.HelpPaneContent>
            <Ribbon.QuickAccessToolBar>
                <RibbonQuickAccessToolBar >
                    <RibbonButton x:Name="QATButton1" 
                                         SmallImageSource="Images\Smallicon.png" />
                    <RibbonButton x:Name="QATButton2" 
                                         SmallImageSource="Images\Smallicon.png" />
                </RibbonQuickAccessToolBar>
            </Ribbon.QuickAccessToolBar>
            <Ribbon.ApplicationMenu>
                <RibbonApplicationMenu SmallImageSource="Images\Smallicon.png">
                    <RibbonApplicationMenuItem Header="Hello _Ribbon"
                                                      x:Name="MenuItem1"
                                                      ImageSource="Images\Largeicon.png"/>
                </RibbonApplicationMenu>
            </Ribbon.ApplicationMenu>
            <RibbonTab x:Name="HomeTab" 
                              Header="Home">
                <RibbonGroup x:Name="Group1" 
                                    Header="Group1">
                    <RibbonButton x:Name="Button1"
                                         LargeImageSource="Images\Largeicon.png"
                                         Label="Button1" />
                    <RibbonButton x:Name="Button2"
                                         SmallImageSource="Images\Smallicon.png"
                                         Label="Button2" />
                    <RibbonButton x:Name="Button3"
                                         SmallImageSource="Images\Smallicon.png"
                                         Label="Button3" />
                    <RibbonButton x:Name="Button4"
                                         SmallImageSource="Images\Smallicon.png"
                                         Label="Button4" />
                </RibbonGroup>
            </RibbonTab>
        </Ribbon>
    </Grid>

</Window>

这应该有效。自 .NET Framework 4.5 以来,这些Ribbon控件已成为 WPF 本身的一部分,因此如果您面向 Framework 4.5+ 或 .NET Core 3+,则无需引用和使用任何外部库。


推荐阅读