C# - 无法使用SystemParameters,"type or namespace does not exist"。

3

我使用Visual Studio 2013专业版,C#语言编写,我的代码中有以下引用和一行代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;               
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

this.Width = System.Windows.SystemParameters.PrimaryScreenWidth;

然而,它出现了一个错误,如下所示:“The type or namespace name 'SystemParameters' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?)”。
我并不真正理解发生了什么,我应该添加更多内容以使用systemparameters吗?

你可以获取窗体的高度和宽度,检查我的答案。 - Dayan
4个回答

6

@user3332706 兄弟,你是在做一个 WPF 应用程序还是 WinForms 应用程序?如果是 WPF 的话,就把对 System.Windows.Forms 的引用移除,并添加对 PresentationCore.dllPresentationFramework.dllWindowsBase.dllSystem.Xaml.dll 的引用,否则你可能正在查看错误的类。 - Federico Berasategui
这是一个 Windows 窗体应用程序,我不能获取屏幕宽度或屏幕高度吗? - user3332706
@user3332706 是的,您可以获取表单的宽度和高度。 var height = this.Size.Height;var width = this.Size.Width; - Dayan

4
将引用PresentationFramework添加到您的项目中,并将using System.Windows;命名空间添加到需要使用SystemParameters的任何文件中。

enter image description here


1

更新

要获取窗体的屏幕宽度和高度,只需执行以下操作:

var height = this.Size.Height; //Gets the current Height of the Form.
var width  = this.Size.Width; //Gets the current Width of the Form.

this.Size = new Size(1000, 200); // Sets new Width/Height for form.

你需要引用DLL。我感觉你创建了一个WinForms项目,但实际上你想要一个WPF项目;并且你正在尝试使用WPF组件...
PresentationFramework(presentationframework.dll)实现了最终用户的呈现特性,包括布局、基于时间的、基于story-board的动画和数据绑定。
要添加项目引用:
1. 在“解决方案资源管理器”中展开项目,在“引用”上单击右键,然后单击“添加引用”。
2. 现在在底部点击“浏览”,找到你正在尝试使用的DLL。在这种情况下,它应该包含在“程序集 - 框架”下。单击左侧菜单上的“框架”。
3. 在右侧找到PresentationFramework。确保你勾选它。
4. 单击确定。
现在你有了引用,回到你的代码,它应该可以正常工作。再次确保你创建了一个WPF项目而不是WinForms...

0

你需要添加对PresentationFramework.dll的引用。


@user3332706 那里缺少了PresentationFramework,你需要添加它。 - JaredPar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接