WPF界面美化(整体作用到控件),一步步教你使用FirstFloor.ModernUI

7 篇文章 0 订阅

开发工具:VS2015

1、获取相关DLL(通过NuGet或者GitHub上下载的源码中获得),并在项目中添加引用

FirstFloor.ModernUI.dll

Microsoft.Windows.Shell.dll

UIShell.OSGi(这个是我运行程序时报的错误"未能加载文件或程序集",然后在NuGet上下载得到的)

2、在.xaml文件中将MainWindow转为Mui.ModernUIWindow

.xaml.cs文件中继承Window改为继承ModernWindow

<mui:ModernWindow x:Class="GFunctionCalculate.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:GFunctionCalculate"
        xmlns:mui="http://firstfloorsoftware.com/ModernUI"
        mc:Ignorable="d"
        Title="MainWindow" Height="600" Width="800" WindowState="Normal" WindowStartupLocation="CenterScreen" ContentSource="/page1.xaml">
   
    <mui:ModernWindow.MenuLinkGroups>
        <mui:LinkGroup DisplayName="" >
            <mui:LinkGroup.Links>
                <mui:Link DisplayName="选择文件" Source="/page1.xaml"/>
                <mui:Link DisplayName="数据列表" Source="/page2.xaml"/>
            </mui:LinkGroup.Links>
        </mui:LinkGroup>
    </mui:ModernWindow.MenuLinkGroups>
</mui:ModernWindow>

 

using FirstFloor.ModernUI.Windows.Controls;
using FirstFloor.ModernUI.Presentation;

namespace GFunctionCalculate
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : ModernWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

3、在App.xaml文件中添加样式(不添加运行会是全黑的窗口)

<Application x:Class="GFunctionCalculate.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:GFunctionCalculate"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.xaml" />
                <ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.Light.xaml"/>
            </ResourceDictionary.MergedDictionaries>
            <Color x:Key="AccentColor">#008A00</Color>
            <!--添加背景图片时使用-->
        <!--<Rectangle x:Key="WindowBackgroundContent" x:Shared="false">
            <Rectangle.Fill>
                    <ImageBrush Opacity=".1" ImageSource="/Assets/background.Love.jpg" Stretch="UniformToFill" />
            </Rectangle.Fill>
        </Rectangle>-->
        </ResourceDictionary>
    </Application.Resources>
</Application>

4、添加Page页

MainWindow中:

<mui:ModernWindow x:Class="GFunctionCalculate.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:GFunctionCalculate"
        xmlns:mui="http://firstfloorsoftware.com/ModernUI"
        mc:Ignorable="d"
        Title="MainWindow" Height="600" Width="800" WindowState="Normal" WindowStartupLocation="CenterScreen" ContentSource="/page1.xaml">
   
    <mui:ModernWindow.MenuLinkGroups>
        <mui:LinkGroup DisplayName="" >
            <mui:LinkGroup.Links>
                <mui:Link DisplayName="数据列表" Source="/page1.xaml"/>
            </mui:LinkGroup.Links>
        </mui:LinkGroup>
    </mui:ModernWindow.MenuLinkGroups>
</mui:ModernWindow>

新建UserControl,命名为page1

page1.xaml

<UserControl x:Class="GFunctionCalculate.page1"
             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:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:GFunctionCalculate"
             xmlns:mui="http://firstfloorsoftware.com/ModernUI"
             xmlns:core="clr-namespace:System;assembly=mscorlib"
             mc:Ignorable="d" 
             d:DesignHeight="600" d:DesignWidth="800">
    <Grid>
        <Grid.Resources>
            <!--Create list of enumeration values-->
            <ObjectDataProvider x:Key="myEnum" MethodName="GetValues" ObjectType="{x:Type core:Enum}">
                <ObjectDataProvider.MethodParameters>
                    <x:Type Type="local:OrderStatus"/>
                </ObjectDataProvider.MethodParameters>
            </ObjectDataProvider>
        </Grid.Resources>
        
        <DockPanel>
            <TextBlock DockPanel.Dock="Top" Text="DATAGRID" Style="{StaticResource Heading2}" Margin="0,0,0,8" />
            <DataGrid Name="DG1" ItemsSource="{Binding}" AutoGenerateColumns="False" >
                <DataGrid.Columns>
                    <mui:DataGridTextColumn Header="First Name"  Binding="{Binding FirstName}"/>
                    <mui:DataGridTextColumn Header="Last Name" Binding="{Binding LastName}" />
                    <mui:DataGridTextColumn Header="Email" Binding="{Binding Email}"/>
                    <mui:DataGridCheckBoxColumn Header="Member" Binding="{Binding IsMember}" />
                    <mui:DataGridComboBoxColumn Header="Order Status" SelectedItemBinding="{Binding Status}" ItemsSource="{Binding Source={StaticResource myEnum}}" />
                </DataGrid.Columns>
            </DataGrid>
        </DockPanel>
    </Grid>
</UserControl>

page1.xaml.cs

using System.Collections.ObjectModel;
using System.Windows.Controls;

namespace GFunctionCalculate
{
    public enum OrderStatus { None, New, Processing, Shipped, Received };
    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public bool IsMember { get; set; }
        public OrderStatus Status { get; set; }
    }


    /// <summary>
    /// Interaction logic for ControlsStylesDataGrid.xaml
    /// </summary>
    public partial class page1 : UserControl
    {
        public page1()
        {
            InitializeComponent();

            ObservableCollection<Customer> custdata = GetData();

            //Bind the DataGrid to the customer data
            DG1.DataContext = custdata;
        }

        private ObservableCollection<Customer> GetData()
        {
            var customers = new ObservableCollection<Customer>();
            customers.Add(new Customer { FirstName = "Orlando", LastName = "Gee", Email = "orlando0@adventure-works.com", IsMember = true, Status = OrderStatus.New });
            customers.Add(new Customer { FirstName = "Keith", LastName = "Harris", Email = "keith0@adventure-works.com", IsMember = true, Status = OrderStatus.Received });
            customers.Add(new Customer { FirstName = "Donna", LastName = "Carreras", Email = "donna0@adventure-works.com", IsMember = false, Status = OrderStatus.None });
            customers.Add(new Customer { FirstName = "Janet", LastName = "Gates", Email = "janet0@adventure-works.com", IsMember = true, Status = OrderStatus.Shipped });
            customers.Add(new Customer { FirstName = "Lucy", LastName = "Harrington", Email = "lucy0@adventure-works.com", IsMember = false, Status = OrderStatus.New });
            customers.Add(new Customer { FirstName = "Rosmarie", LastName = "Carroll", Email = "rosmarie0@adventure-works.com", IsMember = true, Status = OrderStatus.Processing });
            customers.Add(new Customer { FirstName = "Dominic", LastName = "Gash", Email = "dominic0@adventure-works.com", IsMember = true, Status = OrderStatus.Received });
            customers.Add(new Customer { FirstName = "Kathleen", LastName = "Garza", Email = "kathleen0@adventure-works.com", IsMember = false, Status = OrderStatus.None });
            customers.Add(new Customer { FirstName = "Katherine", LastName = "Harding", Email = "katherine0@adventure-works.com", IsMember = true, Status = OrderStatus.Shipped });
            customers.Add(new Customer { FirstName = "Johnny", LastName = "Caprio", Email = "johnny0@adventure-works.com", IsMember = false, Status = OrderStatus.Processing });

            return customers;
        }
    }
}

 运行结果:

谢谢观看,有所帮助的话请点个赞"*★,°*:.☆( ̄▽ ̄)/$:*.°★* 。"

 

ModernUI(http://mui.codeplex.com/)是一个开源的WPF界面库,利用该界面库,我们可以创建很酷的应用程序。下面是ModernUI官方示例,你可以从官方网站直接下载源码运行,如果是.NET 4.0的话,记得要声明“NET4”预编译变量,否则无法编译通过。 这个界面框架是基于ModernUI来实现的,在该文我将分享所有的源码,并详细描述如何基于ModernUI来构造一个非常通用的、插件化的WPF开发框架。下载源码的同志,希望点击一下推荐。 本文将按照以下四点来介绍: (1)ModernUI简介; (2)构建通用界面框架的思路; (3)基于ModernUI和OSGi.NET的插件化界面框架实现原理及源码分析; (4)其它更有趣的东西~~。 要编写这样的WPF界面,我们需要在一个Window上声明菜单和Tab页面,下图是定义菜单的声明。 此外,每一个Tab风格页面,你也需要手动的为菜单创建这样的界面元素。 直接用这样的方式来使用ModernUI,显然不太适合团队协作性的并行开发,因为在一个团队的协作中,不同的人需要完成不同的功能,实现不同页面,每个人都需要来更改主界面。 我非常希望模块化的开发方法,因为这可以尽可能的复用现有资产,使程序员可以聚焦在自己关注的业务逻辑上,不需要关心UI的使用。下面,我将来描述基于ModernUI实现的一个通用界面框架,这个界面框架允许程序员在自己的业务模块中配置需要显示的界面元素。 通用界面框架实现思路: 我希望能够实现这样的通用界面框架: (1)程序员可以直接实现需要展现业务逻辑的界面,不需要关注如何使用ModernUI; (2)程序员可以通过简单的配置就可以将自己实现的业务逻辑页面显示在主界面中; (3)这个界面框架可以完全复用。 当我看到ModernUI这个界面库时,我希望将应用程序做成模块化,每一个模块能够: (1)通过以下配置能够直接显示二级菜单。 (2)通过以下配置能够直接显示三级菜单。 这样做的好处是,开发插件的时候可以不需要关心界面框架插件;团队在协作开发应用的时候,可以独立开发并不需要修改主界面;团队成员的插件可以随时集成到这个主界面;当主界面无法满足我们的布局时或者用户需求无法满足时,可以直接替换主界面框架而不需要修改任何插件代码。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值