WPF(5)----文件浏览对话框

WPF中文件浏览对话框的实现可以利用Windows API Code Pack,它是一个用于访问Windows Vista/7 特性的托管代码函数库,但并没有包含在.NET 4.0中。

该代码包的特性如下所示:

  • 支持Windows Shell命名空间对象,包括新的Windows 7资源库(Libraries)、固定名称文件夹和非文件系统容器。
  • Windows Vista和Windows 7任务对话框(Task Dialogs)。
  • 支持WPF和Windows Forms的Windows 7资源管理器浏览器控件(Explorer Browser Control)。
  • 支持Shell的属性系统。
  • 用于Windows 7任务栏Jumplists、Icon Overlay和Progress Bar的帮助程序。
  • 支持Windows Vista和Windows 7的通用文件对话框,并包括了自定义文件对话框控件。
  • 支持Direct3D 11.0和DXGI 1.0/1.1的API。
  • 传感器平台(Sensor Platform)API
  • 扩展的语言服务(Extended Linguistic Services)API。

1:代码包下载之后,解压,将其中的Microsoft.WindowsAPICodePack.dll 和Microsoft.WindowsAPICodePack.Shell.dll拷贝至工程中。然后Reference-->Add将其添加至Project中的References。



2:代码编写时,将其导入命名空间:

using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Dialogs;

3:前台xmal代码如下:

<Window x:Class="WpfFileExploerDialog.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="148" Width="434" Background="#E609072F">
    <Grid Name="Grid1">
        <TextBox Height="25" Text = "{Binding Path=TextBoxValue}" HorizontalAlignment="Left" Margin="15,29,0,0" Name="textBoxFilePath" VerticalAlignment="Top" Width="347" />
        <Button Content="..." Click="ButtonFileSelect" Height="24" HorizontalAlignment="Left" Margin="377,30,0,0" Name="buttonFileDialog" VerticalAlignment="Top" Width="25" />
    </Grid>
</Window>

4:后台xmal.cs代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.ComponentModel;  
using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Dialogs;

namespace WpfFileExploerDialog
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged  
    {
        public MainWindow()
        {
            InitializeComponent();
            Grid1.DataContext = this; 
        }


        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        private string _Value2;

        public string TextBoxValue
        {
            get { return _Value2; }
            set
            {
                if (value != _Value2)
                {
                    _Value2 = value;
                    NotifyPropertyChanged("TextBoxValue");
                }
            }
        }  

        private void ButtonFileSelect(object sender, RoutedEventArgs e)
        {
            ShellContainer selectedFolder = null;
            selectedFolder = KnownFolders.Computer as ShellContainer;
            CommonOpenFileDialog commonOpenFileDialog = new CommonOpenFileDialog();
            commonOpenFileDialog.InitialDirectoryShellContainer = selectedFolder;
            commonOpenFileDialog.EnsureReadOnly = true;


            if (commonOpenFileDialog.ShowDialog() == CommonFileDialogResult.Ok)
            {
                TextBoxValue = commonOpenFileDialog.FileName;
            }
        }
    }
}

5:程序运行结果如下:



另外,还可以将文件浏览窗口直接定位到固定的文件夹,并且添加想要的文件过滤器,例如下面的代码就是将其定位到SampleVideos文件夹:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.ComponentModel;  
using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Dialogs;

namespace WpfFileExploerDialog
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged  
    {
        public MainWindow()
        {
            InitializeComponent();
            Grid1.DataContext = this; 
        }


        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        private string _Value2;

        public string TextBoxValue
        {
            get { return _Value2; }
            set
            {
                if (value != _Value2)
                {
                    _Value2 = value;
                    NotifyPropertyChanged("TextBoxValue");
                }
            }
        }  

        private void ButtonFileSelect(object sender, RoutedEventArgs e)
        {
            ShellContainer selectedFolder = null;

            //文件夹定位至SampleVideos
            selectedFolder = KnownFolders.SampleVideos as ShellContainer;
            CommonOpenFileDialog commonOpenFileDialog = new CommonOpenFileDialog();
            commonOpenFileDialog.InitialDirectoryShellContainer = selectedFolder;
            commonOpenFileDialog.EnsureReadOnly = true;

            //设置文件过滤
            commonOpenFileDialog.Filters.Add(new CommonFileDialogFilter("WMV Files", "*.wmv"));
            commonOpenFileDialog.Filters.Add(new CommonFileDialogFilter("AVI Files", "*.avi"));
            commonOpenFileDialog.Filters.Add(new CommonFileDialogFilter("MP3 Files", "*.mp3"));
            commonOpenFileDialog.Filters.Add(new CommonFileDialogFilter("MKV Files", "*.mkv"));

            if (commonOpenFileDialog.ShowDialog() == CommonFileDialogResult.Ok)
            {
                TextBoxValue = commonOpenFileDialog.FileName;
            }
        }
    }
}




  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
WPF 01-BootstrapperShell是一种用于启动和初始化WPF应用程序的框架。它是指示WPF应用程序在启动时应执行的代码的入口点。通常情况下,我们可以在App.xaml.cs文件中找到它。 BootstrapperShell提供了一种将应用程序的各个部分组织在一起的方式,以便在启动时执行特定的操作。这些操作可以包括设置应用程序的默认样式、添加全局资源、注册服务和创建主窗口等。通过将所有这些相关的代码集中到一个地方,我们可以更好地管控应用程序的启动过程。 通常情况下,BootstrapperShell会执行以下几个步骤: 1. 创建应用程序的主窗口:这个步骤通常在App.xaml.cs文件的OnStartup方法中完成。我们可以在这里创建一个MainWindow实例,并将其设置为应用程序的主窗口。 2. 设置应用程序的默认样式:WPF应用程序通常使用样式来定义应用程序中各个控件的外观和行为。在BootstrapperShell中,我们可以通过添加资源字典来设置应用程序的默认样式。 3. 注册服务和初始化其他组件:在应用程序启动时,我们可能需要注册一些服务或初始化其他组件,以便在应用程序中的其他地方使用。在BootstrapperShell中,我们可以执行这些操作。 4. 处理未捕获的异常:在应用程序中可能会发生未捕获的异常,我们可以通过在BootstrapperShell中实现Application.DispatcherUnhandledException事件处理程序来捕获和处理这些异常。 总而言之,WPF 01-BootstrapperShell是一种用于组织和管理WPF应用程序启动过程的框架。它提供了一个入口点来集中所有与应用程序启动相关的代码和操作,从而更好地控制应用程序的行为和外观。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值