如何从我的WPF应用程序获取应用程序的目录,在设计时? [英] How do I get the application's directory from my WPF application, at design time?

查看:202
本文介绍了如何从我的WPF应用程序获取应用程序的目录,在设计时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取应用程序的目录,从我的WPF应用程序,在设计时?我需要访问在设计时,我的应用程序的当前目录下的资源,而我的XAML被显示在设计。我不能够使用在<一个指定的溶液href="http://stackoverflow.com/questions/938421/getting-the-applications-directory-from-a-wpf-application">this问题因为在设计时既 System.IO.Path.GetDirectoryName(Process.GetCurrentProcess()。MainModule.FileName)系统。 Reflection.Assembly.GetExecutingAssembly()。位置指向IDE的位置(Visual Studio中...... Common7什么的)。

How do I get the application's directory from my WPF application, at design time? I need to access a resource in my application's current directory at design time, while my XAML is being displayed in the designer. I'm not able to use the solution specified in this question as at design time both System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) and System.Reflection.Assembly.GetExecutingAssembly().Location point to the IDE's location (Visual Studio... Common7 or something).

根据要求,进一步明确自己的目标:我要访问一个数据库表在设计时并显示数据的图形。该设计是在Visual Studio 2008中完成的,所以我需要的是一个非常具体的解决方案,以一个非常具体的问题,那就是让大会目录我的应用程序。

Upon request to further clarify my goals: I want to access a database table at design time and display a graphic of that data. The design is done in Visual Studio 2008, so what I need is a very specific solution to a very specific problem, and that is getting the assembly directory for my app.

推荐答案

从你的描述,它听起来就像你的code其实是Visual Studio中的WPF设计中运行,例如,它是一个自定义的控件库一部分被用于设计

From your description it sounds like your code is actually running inside the WPF Designer within Visual Studio, for example it is part of a custom control library that is being used for design.

在这种情况下, Assembly.GetEntryAssembly()返回null,但下面的code获取路径应用程序目录:

In this case, Assembly.GetEntryAssembly() returns null, but the following code gets the path to the application directory:

  string applicationDirectory = (
    from assembly in AppDomain.CurrentDomain.GetAssemblies()
    where assembly.CodeBase.EndsWith(".exe")
    select System.IO.Path.GetDirectoryName(assembly.CodeBase.Replace("file:///", ""))
    ).FirstOrDefault();

可以使用下面的步骤来证明这个作品里面VS.NET 2008的WPF设计工具:

The following steps can be used to demonstrate this works inside VS.NET 2008's WPF Designer tool:

  1. 将这个code一个WPF自定义控件库或内部类库项目
  2. 添加任何code是要读取数据库并返回数据显示(以我来说,我只是回到了应用程序目录本身作为一个字符串)
  3. 从项目引用该库项目正在设计
  4. 使用自定义的控件或类从一个XAML文件来填充您的DataContext或以其他方式提供数据给你的UI(在我来说,我必然的DataContext用x:静态)
  5. 编辑,随着视窗presentation Foundation设计,它可以通过只双击这样做,除非你改变你的默认编辑器,在这种情况下使用打开方式...

当您按照这些步骤,你正在寻找在运行时和设计时将填充数据从数据库中以同样的方式既对象。

When you follow these steps, the object you are looking at will be populated with data from your database the same way both at run time and design time.

有在同样的技术效果一样好其他方案,并有可根据您的需要其他解决方案。请让我们知道您的需求是不同的我以为以上。例如,如果你正在编写一个VS.NET加载项,你是在一个完全不同的球赛。

There are other scenarios in which this same technique works just as well, and there are other solutions available depending on your needs. Please let us know if your needs are different those I assumed above. For example, if you are writing a VS.NET add-in, you are in a completely different ball game.

这篇关于如何从我的WPF应用程序获取应用程序的目录,在设计时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆