zoukankan      html  css  js  c++  java
  • WPF 跨程序集引用UserControl

    在项目的开发过程中,有是有会把一些用户自定义的元素单独放在一个程序集下面(我没这么做过),这样在另一个程序集中引用这些元素是就容易出一些问题,不过这也很简单就可以解决.

    项目文件图:

    HH9GN0YPAHSJ0(]A29F3K72

    ReferenceUserControl是的主窗体工程,此工程中有一个主窗体和一个用户自定义的元素UserControl1(背景色为绿色)

    MyControl是专门存放用户自定义元素的工程,里面有两个相同的UserControl1元素,不过所在的namespace不同,这两个UserControl1中也没写什么代码,,就改变了一下Grid的背景色,,在最外层这个UserControl1背景色为红色,在TWO文件下面的UserControl1背景色为蓝色

    1.引用本程序集中的Element

    下面是Window1中的XAML代码

    <Window x:Class="ReferenceUserControl.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            
        xmlns:src="clr-namespace:ReferenceUserControl"
            
        Title="Window1">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <src:UserControl1/>
        </Grid>
    </Window>
    

    结果:

    CBG~WQU4K%CZNMFY}(M49[K

    程序执行成功,接下来马上测试另外两个UserControl1

     

    2.测试在MyControl根目录下的那个UserControl,背景色为红色,在项目上添加对MyControl的引用以后,简单的吧原来的命名空间ReferenceUserControl替换为MyControl后,运行出错,如图:

    代码:

    <Window x:Class="ReferenceUserControl.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            
        xmlns:src="clr-namespace:MyControl"
            
        Title="Window1" SizeToContent="WidthAndHeight">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <src:UserControl1/>
        </Grid>
    </Window>

    CP~FRRV@%G6}{Y7_`3_S6IW


    第一条错误时最主要的错误,提示说命名空间没有找到,在这个Uri命名空间中不包含这个MyControl程序集,一般来说,完整的命名空间应该是这样:

    xmlns:src="clr-namespace:ReferenceUserControl;assembly=ReferenceUserControl"
    有时候是在同一个程序集下面对自身的引用,所以可以省略后面assembly的部分,但是当引用的是其他程序集的Element时,
    这一部分是不可以省略的,需要附带程序集的信息.
    这样,简单吧上面的引用部分变为:
        xmlns:src="clr-namespace:MyControl;assembly=MyControl"
    这样上面的代码就可以正常运行,显示的窗体为红色

    }F(}C{4HCD8WQ2K2ZAY12AW

     
     
    3.现在再引用MyControl下面Two文件下面的Element就简单了(蓝色背景)
     
    <Window x:Class="ReferenceUserControl.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            
        xmlns:src="clr-namespace:MyControl.One.Two;assembly=MyControl"
            
        Title="Window1" SizeToContent="WidthAndHeight">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <src:UserControl1/>
        </Grid>
    </Window>
    注意:assembly为MyControl
    结果:蓝色的窗体

    XA937BQ2L4K}$]82_GD8PE9

  • 相关阅读:
    Windows Store App 主题动画
    Windows Store App 过渡动画
    Windows Store App 控件动画
    Windows Store App 近期访问列表
    Windows Store App 文件选取器
    Windows Store App 访问应用内部文件
    Windows Store App 用户库文件分组
    Windows Store App 获取文件及文件夹列表
    Windows Store App 用户库文件夹操作
    Windows Store App 用户库文件操作
  • 原文地址:https://www.cnblogs.com/wangshuai/p/1807624.html
Copyright © 2011-2022 走看看