WPF-如何引用在我的WPF Windows应用程序的另一个项目中编译的UserControl [英] WPF - How to reference a UserControl compiled in another project in my WPF Windows application

查看:114
本文介绍了WPF-如何引用在我的WPF Windows应用程序的另一个项目中编译的UserControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Vista机器上使用VS 2008 .NET 3.5创建并编译了WPF用户控件库.
然后,我创建了一个简单的WPF Windows应用程序.我添加了对已编译的UserControl库的引用.现在,当我尝试在窗口的XMAL代码中使用命名空间时,看不到编译后的UserControl命名空间.
我缺少什么?

I created and compiled a WPF UserControl Library using VS 2008 .NET 3.5 on a Vista machine.
Then I created a simple WPF Windows application. I added a reference to the compiled UserControl Library. Now when I try to a namespace in the XMAL code for the window I do not see my compiled UserControl namespace.
What am I missing?

推荐答案

添加引用后,可以直接从后面的代码中调用UserControl,如下所示:
After you''ve added the reference, you can call the UserControl directly from the code behind, like this:
public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Content = new WpfControlLibrary1.UserControl1();
        }
    }



如果要通过XAML代码使用它,请使用以下代码:



If you want to use it from the XAML code, use this:

<Window x:Class="WpfApplication1.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:lib="clr-namespace:WpfControlLibrary1;assembly=WpfControlLibrary1"

        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <lib:UserControl1 />
    </Grid>
</Window>



Window元素的xmlns:lib属性将xml名称空间lib映射到WpfControlLibrary1名称空间.这是语法:
xmlns:{name} ="clr-namespace:{namespace}; assembly = {assembly}"

希望对您有帮助!

PS:
请记住要重新编译整个解决方案,否则,IntelliSense将无法检测到新的名称空间!



The xmlns:lib attribute of the Window element, maps the xml namespace lib to the WpfControlLibrary1 namespace. This is the syntax:
xmlns:{name}="clr-namespace:{namespace};assembly={assembly}"

I hope this helped you!

PS:
remember to recompile the whole solution, otherwise, the IntelliSense won''t be able to detect the new namespace!


感谢691caro和其他人.我完全按照691caro的建议进行了操作,但它不起作用.我编译的dll在当前解决方案的外部.如果我运行该应用程序,它将正常工作.但是问题是我看不到将其插入xaml文件中的名称空间.那是正常的行为吗?而且,一旦我手动添加名称空间,设计人员就会抱怨我无法加载,这是Visual Studio 2008的问题.
另外,你们是否可以或可以指导我在哪里可以看到一些示例代码,该示例代码显示了使用UserControl的用法,该控件使用了与业务对象(例如人对象)的数据绑定,并且使用了DependencyProperty属性?我看到的有关使用DependencyProperty属性的UserControls和数据绑定的示例中,有99%仅具有一个属性.我想看到一个使用客户或个人对象的对象,更现实一些.

先感谢您.我真的很感激.
Thanks 691caro and the rest. I have done exactly what 691caro suggested and it doesn''t work. my compiled dll is external to the current solution. If I run the app, it works. But the problem is that I do not see my namespace to insert it in the xaml file. is that normal behaviour? and also, as soon as I manually add the namespace the designer complains that I cannot load, is this a problem with visual studio 2008.
Also, do you guys have or can direct me where I can take a look at some smaple code showing the use of a UserControl using data binding to a business object, say a person object, and which uses DependencyProperty properties? The examples I have seen about UserControls and databinding using DependencyProperty properties 99% of them only have one property. I would like to see one that uses a customer or a person object, something more realistic.

Thank you in advance. I really appreciate it.


这篇关于WPF-如何引用在我的WPF Windows应用程序的另一个项目中编译的UserControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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