簡體   English   中英

WPF UserControl 運行時錯誤:無法在 UserControl 中找到圖標的路徑

[英]WPF UserControl Runtime error: Unable to find path of the icon in UserControl

在我的WPF User Control中,我添加了一個圖標(如下面的 XAML 所示)。 但是在運行時,用戶控件的以下代碼中的InitializeComponent()調用給出了如下所示的錯誤:

UserControl1.xaml.cs

public partial class UserControl1 : System.Windows.Window
    {
        public UserControl1()
        {
            InitializeComponent();
        }
…………….
}

用戶控件的 XAML (帶圖標):

<Window x:Class="MyProject.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:MyProject"
             mc:Ignorable="d"
            "d:DesignHeight="450" d:DesignWidth="800"
             Icon="pack://siteoforigin:,,,/Resources/test_32.png">
    <Grid>
………………
    </Grid>

錯誤

DirectoryNotFoundException:找不到路徑“C:\MyFolder\VSTO\MyProject\bin\Debug\Resources\MyIcon.png”的一部分。

備注

我注意到雖然它是項目的一部分,但不會在Debug文件夾下創建Resources文件夾。 我已將帶有Build操作的 MyIcon.png 文件設置為ResourceCopy to output directory設置為Copy if newer new

我不知道為什么,但您應該將application與資源 Uri 和siteoforigin與 rest 一起使用。

您可以將 Uri 更改為:

Icon="pack://application:,,,/Resources/test_32.png"

或者,如果您想繼續使用siteoforigin ,而不是將構建選項更改為“內容”而不是“資源”。 相關話題:

什么是應用程序的原始站點以及何時使用它

打包 uris-in-wpf

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM