国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項超值服

開通VIP
將 WPF 應(yīng)用升級到 .NET 7
userphoto

2023.04.23 加拿大

關(guān)注

使用.NET升級助手將 WPF 應(yīng)用升級到 .NET 7

  • 項目

升級.NET助手是一種命令行工具,可幫助將框架 WPF 應(yīng)用升級到 .NET.NET 7。 本文將提供:

  • 該工具如何與 WPF 項目配合使用的概述
  • 供你下載和升級的示例應(yīng)用
  • 故障排除提示

有關(guān)如何安裝該工具的詳細(xì)信息,請參閱升級助手概述.NET。

提示

升級 .NET 助手正在頻繁接收更新。 如果在使用該工具時發(fā)現(xiàn)問題,請在工具的 GitHub 存儲庫中進(jìn)行報告。

演示應(yīng)用

可以使用基本 WPF 示例項目來測試通過升級助手進(jìn)行升級。

分析應(yīng)用

升級 .NET 助手工具包括一個分析模式,用于執(zhí)行簡化的試運(yùn)行來升級應(yīng)用。 它可以提供有關(guān)在升級開始之前可能需要進(jìn)行的更改的見解。 打開終端,導(dǎo)航到目標(biāo)項目或解決方案所在的文件夾。 運(yùn)行 upgrade-assistant analyze 命令,傳入正在升級的項目或解決方案的名稱。

例如,使用基本 WPF 示例應(yīng)用運(yùn)行分析模式會產(chǎn)生以下輸出,表明在升級之前不會進(jìn)行任何更改:

控制臺
> upgrade-assistant analyze .\WebSiteRatings.sln [15:39:00 INF] Loaded 9 extensions [15:39:02 INF] Using MSBuild from C:\Program Files\dotnet\sdk\7.0.201[15:39:02 INF] Using Visual Studio install from C:\Program Files\Microsoft Visual Studio\2022\Preview [v17] [15:39:05 INF] Writing output to C:\code\migration\AnalysisReport.sarif [15:39:06 INF] Recommending Windows TFM net7.0-windows for project WebSiteRatings.csproj because the project either has Windows-specific dependencies or builds to a WinExe [15:39:06 INF] Marking assembly reference System.Configuration for removal based on package mapping configuration System.Configuration [15:39:06 INF] Adding package System.Configuration.ConfigurationManager based on package mapping configuration System.Configuration [15:39:08 INF] Package EntityFramework, Version=6.2.0 does not support the target(s) net7.0-windows but a newer version (6.4.4) does. [15:39:09 INF] Reference to .NET Upgrade Assistant analyzer package (Microsoft.DotNet.UpgradeAssistant.Extensions.Default.Analyzers, version 0.4.410601) needs to be added [15:39:10 INF] Adding Microsoft.Windows.Compatibility 7.0.0 helps with speeding up the upgrade process for Windows-based APIs [15:39:13 INF] Recommending Windows TFM net7.0-windows for project StarVoteControl.csproj because the project either has Windows-specific dependencies or builds to a WinExe [15:39:13 INF] Reference to .NET Upgrade Assistant analyzer package (Microsoft.DotNet.UpgradeAssistant.Extensions.Default.Analyzers, version 0.4.410601) needs to be added [15:39:13 INF] Adding Microsoft.Windows.Compatibility 7.0.0 helps with speeding up the upgrade process for Windows-based APIs [15:39:13 INF] Running analyzers on WebSiteRatings [15:39:14 INF] Identified 0 diagnostics in project WebSiteRatings [15:39:14 INF] Running analyzers on StarVoteControl [15:39:15 INF] Identified 0 diagnostics in project StarVoteControl [15:39:15 INF] Analysis Complete, the report is available at C:\code\migration\AnalysisReport.sarif

輸出中有很多內(nèi)部診斷信息,但某些信息非常有用。 請注意,分析模式指示升級建議項目將 net7.0-windows 目標(biāo)框架名字對象 (TFM) 。 之所以提出此建議,是因為解決方案引用的項目是 WPF 項目,這是一種僅限 Windows 的技術(shù)。 控制臺應(yīng)用程序可能會獲得直接升級到 TFM net7.0 的建議,除非它使用某些特定于 Windows 的庫。

如果報告了任何錯誤或警告,請在開始升級前處理這些錯誤或警告。

運(yùn)行升級助手

打開終端,導(dǎo)航到目標(biāo)項目或解決方案所在的文件夾。 運(yùn)行 upgrade-assistant upgrade 命令,傳入正在升級的項目或解決方案的名稱。

升級包含多個項目的解決方案時,必須選擇解決方案中的哪個項目是 入口點。 根據(jù)入口點項目,創(chuàng)建一個依賴項關(guān)系圖,以確定要升級哪些項目以及按何種順序升級。 如果解決方案包含的項目不屬于依賴項關(guān)系圖,則忽略這些項目,你需要單獨(dú)升級這些項目。

首先升級依賴項,然后升級 入口點 項目。

使用 基本 WPF 示例 應(yīng)用升級解決方案文件:

控制臺
upgrade-assistant upgrade .\WebSiteRatings.sln

該工具運(yùn)行并顯示它將執(zhí)行的步驟列表。 完成每個步驟后,該工具將提供一組命令,讓用戶應(yīng)用或跳過下一步或其他選項,例如:

  • 獲取有關(guān)此步驟的詳細(xì)信息。
  • 更改項目。
  • 調(diào)整日志記錄設(shè)置。
  • 停止升級并退出。

Enter 而不選擇數(shù)字會選擇列表中的第一項。

每個步驟初始化后,如果應(yīng)用該步驟,它可能會提供有關(guān)其認(rèn)為會發(fā)生的情況的信息。 下一步是選擇要升級的項目。 應(yīng)該會看到以下輸出:

Upgrade Steps 1. [Next step] Select an entrypoint 2. Select project to upgrade Choose a command: 1. Apply next step (Select an entrypoint) 2. Skip next step (Select an entrypoint) 3. See more step details 4. Configure logging 5. Exit

提示

請注意每個步驟的輸出,因為它可能包含有關(guān)工具無法升級的內(nèi)容的信息。 根據(jù)應(yīng)用的復(fù)雜性,該工具完成后,可能需要執(zhí)行更多升級工作。

選擇要升級的項目

確定入口點后,下一步是選擇要升級的項目。 使用 基本 WPF 示例 應(yīng)用時,該工具首先確定 StarVoteControl 項目,因為 WebSiteRatings 項目依賴于它。 最好遵循建議的升級路徑。

[15:45:52 INF] Applying upgrade step Select project to upgrade
Here is the recommended order to upgrade. Select enter to follow this list, or input the project you want to start with.
   1. StarVoteControl
   2. WebSiteRatings

升級項目

選擇項目后,會列出該工具執(zhí)行的升級步驟列表。 選擇第一步,即備份項目。 步驟列表類似于以下代碼片段:

[15:50:50 INF] Initializing upgrade step Back up project Upgrade Steps Entrypoint: C:\code\migration\WebSiteRatings\WebSiteRatings.csproj Current Project: C:\code\migration\WebSiteRatings\WebSiteRatings.csproj 1. [Next step] Back up project 2. Convert project file to SDK style 3. Clean up NuGet package references a. Duplicate reference analyzer b. Package map reference analyzer c. Target compatibility reference analyzer d. Upgrade assistant reference analyzer e. Windows Compatibility Pack Analyzer f. MyDotAnalyzer reference analyzer g. Newtonsoft.Json reference analyzer h. Windows App SDK package analysis i. Transitive reference analyzer 4. Update TFM 5. Update NuGet Packages a. Duplicate reference analyzer b. Package map reference analyzer c. Target compatibility reference analyzer d. Upgrade assistant reference analyzer e. Windows Compatibility Pack Analyzer f. MyDotAnalyzer reference analyzer g. Newtonsoft.Json reference analyzer h. Windows App SDK package analysis i. Transitive reference analyzer 6. Add template files 7. Update WCF service to CoreWCF (Preview) 8. Upgrade app config files a. Convert Application Settings b. Convert Connection Strings c. Disable unsupported configuration sections 9. Update source code a. Apply fix for UA0002: Types should be upgraded b. Apply fix for UA0012: 'UnsafeDeserialize()' does not exist 10. Move to next project Choose a command: 1. Apply next step (Back up project) 2. Skip next step (Back up project) 3. See more step details 4. Select different project 5. Configure logging 6. Exit

每個步驟首先詳細(xì)說明要執(zhí)行的操作,然后提示你執(zhí)行此操作。 例如,上一個代碼片段位于步驟 1 中。[下一步]備份項目。 跳過任何不適用的步驟。 例如,如果該工具處理 7。將 WCF 服務(wù)更新到 CoreWCF (預(yù)覽版) ,但應(yīng)用未定義 WCF 服務(wù),會跳過它并處理步驟 8。 反過來,如果步驟 8 不適用,也會跳過它。 當(dāng)該工具嘗試查找適用的下一步時,你可能會看到許多步驟被跳過。

完成升級

升級項目后,該工具會提示你選擇依賴項關(guān)系圖中的下一個項目進(jìn)行升級。 升級所有項目后,工具會執(zhí)行“完成升級”步驟,從而完成升級

1. [Next step] Finalize upgrade

Choose a command:
   1. Apply next step (Finalize upgrade)
   2. Skip next step (Finalize upgrade)
   3. See more step details
   4. Configure logging
   5. Exit

升級完成后,遷移的 WPF 項目將類似于以下 XML:

XML
<Project Sdk='Microsoft.NET.Sdk'> <PropertyGroup> <TargetFramework>net7.0-windows</TargetFramework> <OutputType>WinExe</OutputType> <GenerateAssemblyInfo>false</GenerateAssemblyInfo> <UseWPF>true</UseWPF> <ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets> </PropertyGroup> <ItemGroup> <None Update='sqlite.db'> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </None> </ItemGroup> <ItemGroup> <ProjectReference Include='..\StarVoteControl\StarVoteControl.csproj' /> </ItemGroup> <ItemGroup> <PackageReference Include='System.Data.DataSetExtensions' Version='4.5.0' /> <PackageReference Include='System.Configuration.ConfigurationManager' Version='5.0.0' /> <PackageReference Include='Microsoft.DotNet.UpgradeAssistant.Extensions.Default.Analyzers' Version='0.4.410601'> <PrivateAssets>all</PrivateAssets> </PackageReference> <PackageReference Include='EntityFramework' Version='6.4.4' /> </ItemGroup> <ItemGroup> <Service Include='{508349B6-6B84-4DF5-91F0-309BEEBAD82D}' /> </ItemGroup> <ItemGroup> <PackageReference Include='MahApps.Metro' Version='2.4.9' /> <PackageReference Include='Microsoft.Data.Sqlite' Version='1.0.0' /> <PackageReference Include='SQLite' Version='3.12.3' /> </ItemGroup> <ItemGroup> <Content Include='appsettings.json' /> </ItemGroup> </Project>

請注意, .NET 升級助手還會向項目添加分析器,以幫助繼續(xù)升級過程,例如 Microsoft.DotNet.UpgradeAssistant.Extensions.Default.Analyzers NuGet 包。

升級后

升級項目后,需要對其進(jìn)行編譯和測試。 很可能還有更多工作要做才能完成升級。 應(yīng)用的框架版本可能包含 .NET 項目實際未使用的庫引用。 分析每個引用并確定是否需要它。 該工具可能還添加或升級了對錯誤版本的 NuGet 包引用。

本文發(fā)布時,需要以下更新才能完成示例項目的升級:

  • System.Configuration.ConfigurationManager NuGet 包升級到版本 7.0.0

    升級工具選擇了錯誤的版本 (5.0.0):

    XML
    <PackageReference Include='System.Configuration.ConfigurationManager' Version='7.0.0' />
    
  • Microsoft.Data.Sqlite NuGet 包升級到版本 7.0.3。

    不需要升級此包。 但是,已選擇版本 1.0.0 ,這是直接支持 .NET 框架的最后一個版本,并支持 .NET Standard 1.3。 此包依賴于包 SQLite ,并且當(dāng)前已棄用。 如果 Microsoft.Data.Sqlite 升級到 7.0.3,則會刪除該依賴項,并且項目使用的是受支持的包版本。

    Microsoft.Data.Sqlite 升級到 7.0.3 后,可以從項目中刪除包 SQLite 。

現(xiàn)代化:appsettings.json

.NET 框架使用 App.config 文件加載應(yīng)用的設(shè)置,例如連接字符串和日志記錄提供程序。 .NET 現(xiàn)在將 appsettings.json 文件用于應(yīng)用設(shè)置。 升級工具會盡力將設(shè)置和連接字符串從 App.config 文件遷移到 appsettings.json 文件,但請確保對其進(jìn)行比較并驗證一切是否正確。

盡管 appsettings.json 是存儲和檢索設(shè)置和連接字符串的新式方式,但代碼未使用它,并且仍依賴于 App.config 文件。 App.config文件通過 System.Configuration.ConfigurationManager NuGet 包在 中.NET受支持,NuGet 包提供對 appsettings.json 的支持Microsoft.Extensions.Configuration

當(dāng)其他庫升級到 .NET時,它們通過支持 appsettings.json 而不是 App.config進(jìn)行現(xiàn)代化。例如,已針對 6+ 升級的.NET框架中的.NET日志記錄提供程序不再將App.config用于設(shè)置。 你最好遵循其方向,同時避免使用 App.config。

將 appsettings.json 與 WPF 示例應(yīng)用配合使用

例如,升級 WPF 示例應(yīng)用后,請刪除本地 System.Configuration.ConfigurationManager 數(shù)據(jù)庫使用的連接字符串的依賴項,并轉(zhuǎn)到 appsettings.json

  1. 刪除 System.Configuration.ConfigurationManager NuGet 包。

  2. 添加 Microsoft.Extensions.Configuration.Json NuGet 包。

  3. 從項目中刪除 App.config 文件。

    在示例應(yīng)用中,此文件僅包含單個連接字符串,該字符串已由升級工具遷移到 appsettings.json 文件。

  4. 將 appsettings.json 文件設(shè)置為復(fù)制到輸出目錄。

    使用“屬性”窗格通過 Visual Studio 設(shè)置此選項,或直接編輯項目并添加以下 ItemGroup

    XML
    <ItemGroup> <Content Include='appsettings.json'> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> </ItemGroup>
  5. 編輯 App.xaml.cs 文件,設(shè)置加載 appsettings.json 文件的配置對象,添加的行突出顯示:

    C#
    using System.Windows;
    using Microsoft.Extensions.Configuration;
    
    namespace WebSiteRatings
    {
        /// <summary>
        /// Interaction logic for App.xaml
        /// </summary>
        public partial class App : Application
        {
            public static IConfiguration Config { get; private set; }
    
            public App()
            {
                Config = new ConfigurationBuilder()
                    .AddJsonFile('appsettings.json')
                    .Build();
            }
        }
    }
    
  6. 在 .\Models\Database.cs 文件中,更改 OpenConnection 方法以使用新的 App.Config 屬性。 這需要導(dǎo)入 Microsoft.Extensions.Configuration 命名空間:

    C#
    using Microsoft.Data.Sqlite; using System.Collections.Generic; using Microsoft.Extensions.Configuration; namespace WebSiteRatings.Models { internal class Database { public static SqliteConnection OpenConnection() => new SqliteConnection(App.Config.GetConnectionString('database')); // More code below...

    GetConnectionStringMicrosoft.Extensions.Configuration 命名空間提供的擴(kuò)展方法。

現(xiàn)代化:Web 瀏覽器控件

WebBrowserWPF 示例應(yīng)用引用的控件基于 Internet Explorer,它已過期。 WPF 包含 .NET 基于 Microsoft Edge 的新瀏覽器控件。 完成以下步驟以升級到新的 WebView2 Web 瀏覽器控件:

  1. 添加 Microsoft.Web.WebView2 NuGet 包。

  2. 在 MainWindow.xaml 文件中:

    1. 將控件導(dǎo)入根元素中的 wpfControls 命名空間:

      XAML
      <mah:MetroWindow x:Class='WebSiteRatings.MainWindow'
              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'
              xmlns:mah='clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro'
              xmlns:local='clr-namespace:WebSiteRatings'
              xmlns:vm='clr-namespace:WebSiteRatings.ViewModels'
              xmlns:VoteControl='clr-namespace:StarVoteControl;assembly=StarVoteControl'
              xmlns:wpfControls='clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf'
              Loaded='MetroWindow_Loaded'
              mc:Ignorable='d'
              Title='My Sites' Height='650' Width='1000'>
      
    2. 在聲明 <Border> 元素的位置下,刪除 WebBrowser 控件并將其替換為 wpfControls:WebView2 控件:

      XAML
      <Border Grid.Row='2' Grid.Column='1' Grid.ColumnSpan='2' BorderThickness='1' BorderBrush='Black' Margin='5'> <wpfControls:WebView2 x:Name='browser' ScrollViewer.CanContentScroll='True' /> </Border>
  3. 編輯 MainWindow.xaml.cs 代碼隱藏文件。 更新 ListBox_SelectionChanged 方法以將 browser.Source 屬性設(shè)置為有效的 Uri。 此代碼以前作為字符串傳入網(wǎng)站 URL,但控件 WebView2 需要 Uri。

    C#
    private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var siteCollection = (ViewModels.SiteCollection)DataContext;
    
        if (siteCollection.SelectedSite != null)
            browser.Source = new Uri(siteCollection.SelectedSite.Url);
        else
            browser.NavigateToString('<body></body>');
    }
    

根據(jù)你的應(yīng)用的用戶運(yùn)行的 Windows 版本,他們可能需要安裝 WebView2 運(yùn)行時。 有關(guān)詳細(xì)信息,請參閱 WPF 應(yīng)用中的 WebView2 入門

Visual Basic 項目

如果使用 Visual Basic 對項目進(jìn)行編碼,升級助手可能包含額外的步驟,例如遷移 My 命名空間。 當(dāng)你的項目使用這些功能時,你應(yīng)該只會看到添加的這些步驟。

7. Update Visual Basic project a. Update vbproj to support 'My.' namespace

另請參閱

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
【運(yùn)維開發(fā)】windows下的自動化腳本語言autoit
aardio + .NET( C# ) 快速開發(fā)獨(dú)立 EXE 程序,防 ILSpy 反編譯 EXE
YAML配置管理最佳實踐
Entity Framework的原理及使用方式
字符串方法示例
SQL Server 2005 Express Edition 用戶實例
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服