WPF作为库 - “应用程序定义”问题 [英] WPF as a library - "Application Definition" problem

查看:76
本文介绍了WPF作为库 - “应用程序定义”问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我将WPF应用程序作为库存在小问题。它最初设计为独立应用程序,但后来我们决定将其用作插件。我在谷歌搜索,现在我知道我应该使用WPF自定义控件库或WPF用户控件库。我的项目是WPF App,在将输出类型更改为类库后,VS会给我以下警告:



1.库项目文件无法指定ApplicationDefinition元素。

2.项目文件包含无效的属性值。



谷歌告诉我将App.xaml构建选项更改为页面,这里我得到另一个问题。我正在使用外部样式,在开始之后,我在每个地方都使用{StaticResource Foreground}获得异常。我很喜欢WPF,所以我将不胜感激。

Hi,

I have small problem with WPF app as a library. It was originally designed to be standalone application but later we decided to use it as a plugin. I searched in Google and now I know that I should use WPF Custom Control Library or WPF User Control Library. My project is WPF App, and after changing output type to class library VS gives me following warnings:

1. Library project file cannot specify ApplicationDefinition element.
2. The project file contains a property value that is not valid.

Google told me to change App.xaml build options to page and here I get another problem. I am using external style and after start I get exceptions in every place with "{StaticResource Foreground}". I am fresh in WPF so I will appreciate any help.

推荐答案

你不应该在用户控件库中拥有app.xaml。摆脱它。



你遇到的问题是用户控件库实际上是用于UIelements,有效地告诉引用用户的应用程序使用什么资源控制库。解决这个问题的方法是创建一个包含其中资源(和/或引用)的资源字典,然后在用户控件上引用usercontrol.resources部分中的这个资源。



ie(注意这是来自Silverlight项目,但是校长是相同的)



资源字典

You should not have the app.xaml in a user control lib. get rid of that.

The problem you have is that the user control lib is really for UIelements that are effectively told what resources to use by the application that references the user control lib. The way around this is to create a resource dictionary with the resources in it (and or references) and then on the user controls reference this resource in the usercontrol.resources section.

i.e.(note this is from a Silverlight Project but principals are the same)

Resource Dictionary
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="TextBlockWithMargin" TargetType="TextBlock">
        <Setter Property="Margin" Value="10" />
    </Style>





用户控制



User control

<UserControl x:Class="x.x.Test"

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

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

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d"

    d:DesignHeight="300" d:DesignWidth="400">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/ProjectA;component/MyResourceDictionary.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" removed="White">

    </Grid>
</UserControl>





您通常会拥有应用程序app.xaml中设置的资源。您已经提到应用程序已更改为usercontrol lib。那么这将被引用到哪里?你是说你想自己运行一个usercontrol lib吗?



为了解决编译时错误你可以使用{DynamicResource Foreground}。直到运行时才会设置动态资源,因此编译应该起作用。如果用户控件不知道或者找不到正确的资源,你仍会遇到问题。



如果你提供更多背景知识你正在做什么或想要什么实现那么我可以进一步帮助。



谢谢



You would normally have the resources set in the application app.xaml. You have mentioned that the application has been changed to a usercontrol lib. So where will this be referenced? Are you saying that you want to run a usercontrol lib on its own?

To get around the compiletime errors you can use {DynamicResource Foreground}. Dynamic resource will not get set until runtime so compilations should work. You will still have issues if the usercontrol is not aware or cannot find the correct resource though.

If you provide a bit more background to what you are doing or want to achieve then I can probably help further.

thanks


这篇关于WPF作为库 - “应用程序定义”问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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