小编Ale*_*kov的帖子

C#构造函数重载

我如何在C#中使用构造函数,如下所示:

public Point2D(double x, double y)
{
    // ... Contracts ...

    X = x;
    Y = y;
}

public Point2D(Point2D point)
{
    if (point == null)
        ArgumentNullException("point");
    Contract.EndContractsBlock();

    this(point.X, point.Y);
}
Run Code Online (Sandbox Code Playgroud)

我需要它不要从另一个构造函数复制代码...

c# constructor constructor-overloading

62
推荐指数
3
解决办法
10万
查看次数

Visual Studio 2010/2012 WPF设计器扩展

当我使用Blend 4/5时,我可以为Blend WPF设计器创建扩展,如下所示:

using System.ComponentModel.Composition;

using Microsoft.Expression.DesignModel.Metadata;
using Microsoft.Expression.Extensibility;
using Microsoft.Expression.Platform;
using Microsoft.Expression.WpfPlatform;

namespace Elysium.Extension
{
    [Export(typeof(IPackage))]
    public class Package : IPackage
    {
        private IServices _services;

        public void Load(IServices services)
        {
            _services = services;

            var platformService = _services.GetService<IPlatformService>();
            platformService.PlatformCreated += Register;
        }

        private void Register(object sender, PlatformEventArgs e)
        {
            var wpf = e.Platform as WpfPlatform;
            if (wpf != null)
            {
                wpf.Metadata.AddAssemblyGroupMapping(AssemblyGroup.ExtendedControls, "Elysium.Extension");
                wpf.InstanceBuilderFactory.Register(new CustomWindowInstanceBuilder());
            }
        }

        public void Unload()
        {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在这段代码中,我订阅了IPlatform服务,当它更新时,我通过WPFPlatform对象注册我的自定义WindowInstanceBuilder.

我如何为Visual Studio 2010/2012设计师做到这一点?

谢谢.

wpf designer visual-studio-2010 visual-studio visual-studio-2012

20
推荐指数
1
解决办法
3170
查看次数

例外:实例"实例名称"在指定的类别中不存在

当我创建和使用这样的性能计数器时:

private readonly PerformanceCounter _cpuPerformanceCounter;
public ProcessViewModel(Process process)
        {

             _cpuPerformanceCounter = new PerformanceCounter("Process", "% Processor Time", process.ProcessName, true);
        }

public void Update()
        {
            CPU = (int)_cpuPerformanceCounter.NextValue() / Environment.ProcessorCount; // Exception
        }
Run Code Online (Sandbox Code Playgroud)

...我得到一个异常实例"实例的名称"在指定的类别中不存在,并且不理解原因.

PS代码

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.net>
    <settings>
      <performanceCounters enabled="true"/>
    </settings>
  </system.net>
</configuration>
Run Code Online (Sandbox Code Playgroud)

...包含在App.config中.

c# exception performancecounter

10
推荐指数
1
解决办法
9601
查看次数

使用min(width,height)/ 2作为半径在WPF中绘制一个圆

如何使用min(width, height)/2半径在WPF(没有代码隐藏)中绘制圆圈?

wpf geometry xaml code-behind

10
推荐指数
2
解决办法
2万
查看次数

在WPF应用程序中做广告?

那可能吗?我知道Google广告等服务禁止在桌面应用中投放广告.是否有支持WPF/.NET的服务?

wpf desktop desktop-application ads

6
推荐指数
1
解决办法
1918
查看次数

C#泛型类型

我在我的库中使用了三个类:

public abstract class Base<TFirst, TSecond>
{
    public Base()
    {
      // actions with ID and Data of TFirst and TSecond
    }
}

public abstract class First<TFirstID, TFirstData>
{
    public TFirstID ID {get; set;}
    public TFirstData Data {get; set;}
}

public abstract class Second<TSecondID, TSecondData>
{
    public TSecondID ID {get; set;}
    public TSecondData Data {get; set;}
}
Run Code Online (Sandbox Code Playgroud)

如何指定TFirst必须从First继承而TSecond必须从Second继承,而不是在Base中使用ID和Data的泛型类型?

像这样:

public abstract class Base<TFirst, TSecond>
    where TFirst : First // without generic-types
...
Run Code Online (Sandbox Code Playgroud)

编辑: 在类First,Second我使用TFirstID和TSecondID作为属性.在类Base中我使用此属性.

c# generics

5
推荐指数
2
解决办法
1832
查看次数

如何在C#中获取有关进程的信息?

如何在C#应用程序中获取有关进程(cpu,内存,磁盘和网络使用情况)的信息?

PS System.Diagnostics.Process和System.Diagnostics.PerformanceCounter不提供有关磁盘和网络使用情况的信息.我不使用它.

c# process

5
推荐指数
1
解决办法
1万
查看次数

在WPF应用程序中禁用Aero Peek

我想在我的WPF应用程序中禁用Aero Peek(当用户将鼠标放在"显示桌面"按钮上时,我的应用程序必须可见).我使用这个PInvoke签名:

[Flags]
public enum DwmWindowAttribute : uint
{
    DWMWA_NCRENDERING_ENABLED = 1,
    DWMWA_NCRENDERING_POLICY,
    DWMWA_TRANSITIONS_FORCEDISABLED,
    DWMWA_ALLOW_NCPAINT,
    DWMWA_CAPTION_BUTTON_BOUNDS,
    DWMWA_NONCLIENT_RTL_LAYOUT,
    DWMWA_FORCE_ICONIC_REPRESENTATION,
    DWMWA_FLIP3D_POLICY,
    DWMWA_EXTENDED_FRAME_BOUNDS,
    DWMWA_HAS_ICONIC_BITMAP,
    DWMWA_DISALLOW_PEEK,
    DWMWA_EXCLUDED_FROM_PEEK,
    DWMWA_LAST
}

[Flags]
public enum DWMNCRenderingPolicy : uint
{
    UseWindowStyle,
    Disabled,
    Enabled,
    Last
}

[DllImport("dwmapi.dll", PreserveSig=false)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DwmIsCompositionEnabled();

[DllImport("dwmapi.dll", PreserveSig=false)]
public static extern Int32 DwmSetWindowAttribute(IntPtr hwnd,
                                                 DwmWindowAttribute dwmAttribute,
                                                 IntPtr pvAttribute,
                                                 uint cbAttribute);
Run Code Online (Sandbox Code Playgroud)

这个用法:

    var helper = new WindowInteropHelper(this);
    helper.EnsureHandle();

    if (API.DwmIsCompositionEnabled())
    {
        var status = Marshal.AllocCoTaskMem(sizeof(uint));
        Marshal.Copy(new[] {(int) API.DWMNCRenderingPolicy.Enabled}, 0, status, 1); …
Run Code Online (Sandbox Code Playgroud)

wpf pinvoke dwm aero-peek

5
推荐指数
1
解决办法
2213
查看次数

通过MSBuild构建具有不同平台的Visual Studio项目

我有3个配置项目:

  • 项目A:调试| AnyCPU,发布| AnyCPU
  • 项目B:调试| AnyCPU,发布| AnyCPU
  • 项目C:调试| x86,调试| x64,发布| x86,发布| x64

项目C具有依赖关系中的B,并且B具有依赖关系中的A. (A < - B < - C)

我使用*.bat文件从命令行构建它:

msbuild A.csproj /target:Build /property:Configuration=Debug;Platform=AnyCPU /verbosity:minimal
msbuild A.csproj /target:Build /property:Configuration=Release;Platform=AnyCPU /verbosity:minimal<br/>
msbuild B.csproj /target:Build /property:Configuration=Debug;Platform=AnyCPU /verbosity:minimal
msbuild B.csproj /target:Build /property:Configuration=Release;Platform=AnyCPU /verbosity:minimal
msbuild C.csproj /target:Build /property:Configuration=Debug;Platform=x86 /verbosity:minimal
msbuild C.csproj /target:Build /property:Configuration=Release;Platform=x86 /verbosity:minimal
msbuild C.csproj /target:Build /property:Configuration=Debug;Platform=x64 /verbosity:minimal
msbuild C.csproj /target:Build /property:Configuration=Release;Platform=x64 /verbosity:minimal
Run Code Online (Sandbox Code Playgroud)

并收到错误:

C:\ Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Common.targets(609,5):错误:未为项目"A.csproj"设置OutputPath属性.请检查以确保您为此项目指定了Configuration和Platform的有效组合.Configuration ='Debug'Blatform ='x86'.您可能会看到此消息,因为您正在尝试构建没有解决方案文件的项目,并且已指定了此项目不存在的非默认配置或平台.[A.csproj] C:\ Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Common.targets(609,5):错误:未为项目"B.csproj"设置OutputPath属性.请检查以确保您为此项目指定了Configuration和Platform的有效组合.Configuration ='Debug'Blatform ='x86'.您可能会看到此消息,因为您正在尝试构建没有解决方案文件的项目,并且已指定了此项目不存在的非默认配置或平台.[B.csproj]

msbuild platform visual-studio

4
推荐指数
1
解决办法
1万
查看次数

我如何将我的C#AnyCPU应用程序重新运行为64位,如果它运行为32位(WOW64)?

我开发了32位和64位Windows的应用程序.我有一个问题:如果我的应用程序是在64位操作系统上从32位进程启动的,那么我的应用程序将以32位运行.我如何将我的C#AnyCPU应用程序重新运行为64位,如果它运行为32位(WOW64)?

c# 32bit-64bit anycpu

2
推荐指数
1
解决办法
2220
查看次数