.net 8 add System.Windows.PresentationCore dll and using System.Windows.Media namespace

1.add reference,C:\Program Files\dotnet\packs\Microsoft.WindowsDesktop.App.Ref\8.0.11\ref\net8.0\PresentationCore.dll

2.Source code

复制代码
using System;
using System.Windows;
using System.Windows.Media;

namespace ConsoleApp19
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
            string str = Guid.NewGuid().ToString();
            PrintTextFontsizeWidth(str, "Arial", 120);
        }

        static void PrintTextFontsizeWidth(string str, string fontFamilyName, int fontSize)
        {
            FormattedText formattedText = new FormattedText(
            str,
            System.Globalization.CultureInfo.CurrentCulture,
            FlowDirection.LeftToRight,
            new Typeface(fontFamilyName),
            fontSize,
            Brushes.Black, 1.25);
            Console.WriteLine($"{str}\nWidth:{formattedText.Width}\nHeight:{formattedText.Height}");
        }
    }
}
复制代码

 

3.Run and throw exception

复制代码
System.BadImageFormatException
  HResult=0x80131058
  Message=Could not load file or assembly 'PresentationCore, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Reference assemblies cannot be loaded for execution. (0x80131058)
  Source=ConsoleApp19
  StackTrace:
   at ConsoleApp19.Program.PrintTextFontsizeWidth(String str, String fontFamilyName, Int32 fontSize) in D:\C\ConsoleApp19\ConsoleApp19\Program.cs:line 26
   at ConsoleApp19.Program.Main(String[] args) in D:\C\ConsoleApp19\ConsoleApp19\Program.cs:line 13

Inner Exception 1:
BadImageFormatException: Cannot load a reference assembly for execution.
复制代码

 

 

4.Edit csproj file

复制代码
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <UseWPF>true</UseWPF>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <Reference Include="PresentationCore">
      <HintPath>C:\Program Files\dotnet\packs\Microsoft.WindowsDesktop.App.Ref\8.0.11\ref\net8.0\PresentationCore.dll</HintPath>
    </Reference>
  </ItemGroup>

</Project>
复制代码

 

5.Run and errors

The target platform must be set to Windows (usually by including '-windows' in the TargetFramework property) when using Windows Forms or WPF, or referencing projects or packages that do so.

 

 

6.Edit csproj file again,and the ultimate edition.

复制代码
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <Reference Include="PresentationCore">
      <HintPath>C:\Program Files\dotnet\packs\Microsoft.WindowsDesktop.App.Ref\8.0.11\ref\net8.0\PresentationCore.dll</HintPath>
    </Reference>
  </ItemGroup>

</Project>
复制代码

 

 

7.Run

 

posted @   FredGrit  阅读(35)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
历史上的今天:
2017-02-04 Linq
点击右上角即可分享
微信分享提示