说来惭愧,接触WPF这么长时间了,今天在写自定义控件时遇到一个问题:运行界面中并没有显示自定义控件,经调试发现原来没有加载Themes中的Generic.xaml。

可是为什么在其他solution中可以成功显示呢?后来就google学习了一下WPF中加载资源的相关文档,但都是理论性的介绍。对这个问题还是没有多大的帮助。

没有办法只能比较两个solution中的proj有什么不同,打开proj属性,发现application、build、buildEvents...等都一样,后来只能比较assemblyInfo,发现多了

[assembly:ThemeInfo(
......
......
......
)]

一瞬间像找到了答案一样兴奋(虽然没有验证),继续万能的google呀,于是找到了ClassLiary,WPF User Control Libary,WPF Custom Control Libary.

1:先看Class Library和WPF User Control Library,区别在于,

  • 增加了4个Reference,PresentationCore,PresentationFramework,System.Xaml,WindowsBase
  • AssemblyInfo.cs中多了ThemeInfo的Attribute,如下,
    [assembly:ThemeInfo(
    ResourceDictionaryLocation.None,
    //where theme specific resource dictionaries are located
    //(used if a resource is not found in the page,
    // or application resource dictionaries)
    ResourceDictionaryLocation.SourceAssembly
    //where the generic resource dictionary is located
    //(used if a resource is not found in the page,
    // app, or any theme specific resource dictionaries)
    )

所以如果要在一个Class Library中新建一个WPF UserControl就需要新建一个WPF UserControl,Visual Stuido会自动为你加入4个Reference,然后手动在AssemblyInfo.cs加入ThemeInfo即可.

[assembly: ThemeInfo(ResourceDictionaryLocation.None,
ResourceDictionaryLocation.SourceAssembly)]

  

2:然后说说WPF User Control Library和WPF Custom Control Library的区别,结果看下来基本没区别,无非是Custom Control Library默认帮你创建了一个CustomControl继承Control,然后在Themes目录下创建了Generic.xaml。

所以如果新建Class Library后需要创建Custom Control,就需要增加对应的4个Renference,在Add File中无法增加Custom Control,这个VS有点2,只能手动创建Class,然后改为继承自Control,

public class Class1 : Control {
static Class1() {
DefaultStyleKeyProperty.OverrideMetadata(typeof(Class1), new FrameworkPropertyMetadata(typeof(Class1)));
}
}

然后创建目录Themes增加Generic.xaml,确定Generic.xaml的build方式为Page,最后增加ThemeInfo即可。

3:总结一下,原来真的就是assembly中的assembly:ThemeInfo那段代码起到了加载ResourceDictionary的作用。不禁感慨在项目中直接根据源代码摸索真不如好好看一本书呀。

ClassLibary和WPF User Control LIbary和WPF Custom Control Libary的异同的更多相关文章

  1. Writing a Reusable Custom Control in WPF

    In my previous post, I have already defined how you can inherit from an existing control and define ...

  2. Recommended Practices for WPF Custom Control Developers

    I have always found that there isn’t enough documentation about Custom Control development in WPF. M ...

  3. WPF中的Generic.xaml, theme以及custom control

    原文:WPF中的Generic.xaml, theme以及custom control 在Visual Studio中创建自定义控件时,所有控件都将添加到/Themes/Generic.xaml. 最 ...

  4. .NET Core学习笔记(2)—— WPF使用UWP Custom Control

    自.NET Core 3.0开始,某软加入了对WPF的支持.同时对XAML Islands也做了进一步加强.在.NET Core 3.0之前,我们只能在WPF程序中,通过两种方式有限制地使用Stand ...

  5. WPF快速入门系列(6)——WPF资源和样式

    一.引言 WPF资源系统可以用来保存一些公有对象和样式,从而实现重用这些对象和样式的作用.而WPF样式是重用元素的格式的重要手段,可以理解样式就如CSS一样,尽管我们可以在每个控件中定义格式,但是如果 ...

  6. WPF快速入门系列(6)—— WPF资源和样式

    一.引言 WPF资源系统可以用来保存一些公有对象和样式,从而实现重用这些对象和样式的作用.而WPF样式是重用元素的格式的重要手段,可以理解样式就如CSS一样,尽管我们可以在每个控件中定义格式,但是如果 ...

  7. WPF/MVVM Quick Start Tutorial - WPF/MVVM 快速入门教程 -原文,翻译及一点自己的补充

    转载自 https://www.codeproject.com/articles/165368/wpf-mvvm-quick-start-tutorial WPF/MVVM Quick Start T ...

  8. WPF快速入门系列(1)——WPF布局概览

    一.引言 关于WPF早在一年前就已经看过<深入浅出WPF>这本书,当时看完之后由于没有做笔记,以至于我现在又重新捡起来并记录下学习的过程,本系列将是一个WPF快速入门系列,主要介绍WPF中 ...

  9. WPF 3D 小小小小引擎 - ·WPF 3D变换应用

    原文:WPF 3D 小小小小引擎 - ·WPF 3D变换应用 WPF可以提供的3D模型使我们可以轻松地创建3D实体,虽然目前来看还很有一些性能上的问题,不过对于一些简单的3D应用应该是可取的,毕竟其开 ...

随机推荐

  1. shell脚本中各个参数的意思

    文件表达式-e filename 如果 filename存在,则为真-d filename 如果 filename为目录,则为真 -f filename 如果 filename为常规文件,则为真-L ...

  2. J.U.C并发框架源码阅读(十二)ConcurrentHashMap

    基于版本jdk1.7.0_80 java.util.concurrent.ConcurrentHashMap 代码如下 /* * ORACLE PROPRIETARY/CONFIDENTIAL. Us ...

  3. 计蒜客 28315.Excellent Engineers-线段树(单点更新、区间最值) (Benelux Algorithm Programming Contest 2014 Final ACM-ICPC Asia Training League 暑假第一阶段第二场 E)

    先写这几道题,比赛的时候有事就只签了个到. 题目传送门 E. Excellent Engineers 传送门 这个题的意思就是如果一个人的r1,r2,r3中的某一个比已存在的人中的小,就把这个人添加到 ...

  4. bootstrap只有遮罩层没有对话框的解决方法

    前端很差很差,猜测应该是各种js冲突的问题,换了一个jquery或bootstrap版本的不兼容. https://blog.csdn.net/Pabebe/article/details/70230 ...

  5. 陕西师范大学第七届程序设计竞赛网络同步赛 C iko和她的糖【贪心/ STL-优先队列/ 从1-N每个点有能量补充,每段有消耗,选三个点剩下最多能量】

    链接:https://www.nowcoder.com/acm/contest/121/C来源:牛客网 题目描述 iko超级超级喜欢吃糖,有一天iko想出去玩,她计划从1点走到N点(按1,2,3,.. ...

  6. 【模拟】Friday the Thirteenth

    题目描述 Is Friday the 13th really an unusual event?That is, does the 13th of the month land on a Friday ...

  7. 使用after伪类,配合IE的zoom或者overflow清除浮动

    用after伪类实现,兼容多种浏览器:.clearfix:after {content:"."; display:block; height:0; clear:both; visi ...

  8. 使用gtest自动化测试并给出性能测试结果(windows 版本,版本平台也可以使用,但并没有做完整的测试)

    /************************************************************* *使用gtest自动化测试 * ********************* ...

  9. Linux守护进程的启动方法(转)

    作者: 阮一峰 日期: 2016年2月28日 "守护进程"(daemon)就是一直在后台运行的进程(daemon). 本文介绍如何将一个 Web 应用,启动为守护进程. 一.问题的 ...

  10. bin/...的访问被拒绝被拒绝的问题

    复制到bin.... 对路径bin/.... 的访问被拒绝出现这们的问题,把源码管理器中项目的Bin目录删除,重新获取就要以了