简体   繁体   English

在WPF C#应用程序中的哪里设置默认值?

[英]Where to set default values in a WPF C# app?

Where would I set default values that I want to use throughout a project and only define once? 我将在哪里设置要在整个项目中使用且仅定义一次的默认值?

An example would be a default margin, or a color that I want to use in multiple places. 一个示例是默认边距,或者是我想在多个地方使用的颜色。

In pure C, one way to do this would be with a "defaults.h" header file with "#define"s. 在纯C语言中,一种实现方法是使用带有“ #define”的“ defaults.h”头文件。

You can use the appsettings section of an app.config file. 您可以使用app.config文件的appsettings部分。 See here for more info. 有关更多信息,请参见此处

You can do most of this with resources and templates, if you want common looks and styles throughout your app. 如果您希望整个应用程序具有共同的外观和样式,则可以使用资源和模板来完成大多数操作。

Edit : Based on your comment, looking at x:Static may be what you're after. 编辑 :根据您的评论,查看x:Static可能就是您想要的。 Here's a SO question covering it. 这是一个涉及它的SO问题。 If that's still not quite it...try elaborating a bit more. 如果还不够,请尝试更多。

This is what I first read when playing around with WPF explaining it. 这是我在使用WPF进行解释时首先阅读的内容。

The simplest way to do this is to store them in the application's resource dictionary, either in markup (in the <Application.Resources> element in app.xaml ) or by creating them in code at startup and adding them to the resource dictionary. 最简单的方法是将它们存储在应用程序的资源字典中,或者存储在标记中(位于app.xaml中的<Application.Resources>元素中),或者通过在启动时在代码中创建它们并将它们添加到资源字典中。

This allows you to do some pretty sophisticated stuff fairly easily, so long as you have a consistent application-wide naming convention for resource keys. 只要您对资源密钥具有一致的应用程序范围内的命名约定,就可以轻松地完成一些相当复杂的工作。 For instance, it's easy to implement simple skinning in an application by building resource dictionaries in XAML files and then merging a specific dictionary into the application's dictionary at startup. 例如,通过在XAML文件中构建资源字典,然后在启动时将特定的词典合并到应用程序的词典中,很容易在应用程序中实现简单外观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM