C# '在'System.Windows.Baml2006.TypeConverterMarkupExtension'上提供值引发异常。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12833294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:36:08  来源:igfitidea点击:

'Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.'

c#wpfxaml

提问by Chrisjan Lodewyks

The exception in the title is thrown when I open a window in WPF, the strange thing is that this does not happen on my Windows 7 development machine nor does it happen when it is deployed on Windows 7.

当我在 WPF 中打开一个窗口时,会抛出标题中的异常,奇怪的是,这在我的 Windows 7 开发机器上没有发生,在 Windows 7 上部署时也没有发生。

I only get this error on Windows XP, and only the second time that I open the window.

我只在 Windows XP 上收到此错误,并且仅在我第二次打开窗口时收到此错误。

Here is the code to open the window:

这是打开窗口的代码:

ReportParametersWindow win = null;

      bool canOverWrite = _shownReports.Contains(rpt.FriendlyName);

      if (!(canOverWrite))
        win = new ReportParametersWindow(rpt.FriendlyName, rpt.ReportParameters, canOverWrite);
      else
        win = new ReportParametersWindow(rpt.FriendlyName, (container.ParametersWindow as ReportParametersWindow).Controls, canOverWrite);

      win.ShowDialog();

And the XAML for the window:

和窗口的 XAML:

<Window xmlns:my="clr-namespace:MHA.Modules.Core.Controls;assembly=MHA.Modules.Core"  
    x:Class="MHA.Modules.Reports.Views.ReportParametersWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Parameters" Height="500" Width="600" MinWidth="500" MaxHeight="500" 
    Icon="/MHA.Modules.Reports;component/Images/Parameters.ico" SizeToContent="WidthAndHeight" 
    WindowStartupLocation="CenterScreen"
    xmlns:odc="clr-namespace:Odyssey.Controls;assembly=Odyssey" Closed="Window_Closed">

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="40"/>
    </Grid.RowDefinitions>
    <ScrollViewer Grid.Row="0" Name="ScrollViewer1" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" CanContentScroll="True">
        <StackPanel Name="ParameterStack">
            <my:LocationCtl Text="Parameters for report - " Name="loc"/>
        </StackPanel>
    </ScrollViewer>
    <Grid Grid.Row="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <CheckBox ToolTip="This will replace the first report of the same type that was shown." Name="chkOverwrite" Content="Overwrite old" VerticalAlignment="Center" Margin="5,0"></CheckBox>

        <Button Grid.Column="2" HorizontalAlignment="Right" Margin="5,0" Height="30" Style="{StaticResource DionysusButton}" Width="100" IsDefault="True" Click="Button_Click">
            <StackPanel Orientation="Horizontal">
                <Image Source="/MHA.Modules.Reports;component/Images/Success.png"></Image>
                <TextBlock Margin="5,0" Text="Accept" VerticalAlignment="Center"></TextBlock>
            </StackPanel>
        </Button>
    </Grid>
</Grid>

Does anyone have suggestions?

有没有人有建议?

回答by Chrisjan Lodewyks

The solution is quite a weird one but I have it figured out.

解决方案很奇怪,但我已经弄清楚了。

I realized that the error was occurring on the InitializeComponent() of the window, I then added a try catch to the constructor and showed the InnerException of the Exception.

我意识到错误发生在窗口的 InitializeComponent() 上,然后我向构造函数添加了一个 try catch 并显示了异常的 InnerException。

The error that I received is "Image format not recognized".

我收到的错误是“无法识别图像格式”。

I have no idea why this happens only on XP and the second time that the window is shown but by replacing my .ico with a .png the problem was resolved.

我不知道为什么这只发生在 XP 和第二次显示窗口时,但通过用 .png 替换我的 .ico 问题解决了。

Hope this helps someone.

希望这可以帮助某人。

回答by user2125523

This problem can also occur if the required image is not available at the specified location. So Check the inner exception and add any image that might have been missed or misspelled.

如果所需的图像在指定位置不可用,也会出现此问题。所以检查内部异常并添加任何可能遗漏或拼写错误的图像。

回答by Ludwo

In my case the root cause was wrong BuildAction property on all images. I fixed it by changing BuildAction from Content to Resource.

在我的情况下,根本原因是所有图像上的 BuildAction 属性错误。我通过将 BuildAction 从 Content 更改为 Resource 来修复它。

回答by Owen

To improve upon user2125523:

改进 user2125523:

If you've added the image to the project and checked and double checked that the file spelling is correct, try renaming the image to mirror a different existing image. Build/run, then put your image file name back and build/run again.

如果您已将图像添加到项目并检查并仔细检查文件拼写是否正确,请尝试重命名图像以镜像不同的现有图像。构建/运行,然后将您的图像文件名放回并再次构建/运行。

For example: My original code kept throwing the OP error on LargeImage="/img/32/delete.order.png"even though this file exists.

例如:LargeImage="/img/32/delete.order.png"即使此文件存在,我的原始代码仍不断抛出 OP 错误。

<telerik:RadRibbonButton Text="Object Properties" Size="Large"
    Name="PropertiesButton" IsTabStop="True"
    telerik:ScreenTip.Description="Get object properties" 
    Click="PropertiesButton_Click"
    LargeImage="/img/32/properties.png" 
    SmallImage="/img/16/properties.png" />
<telerik:RadRibbonButton Text="Reset Tab Order" Size="Large" 
    Name="ClearTabOrderButton" IsTabStop="True"
    telerik:ScreenTip.Description="Reset tab order of all fields"
    Click="ClearTabOrder_Click"
    LargeImage="/img/32/delete.order.png" 
    SmallImage="/img/16/delete.order.png" />

So, I changed LargeImage="/img/32/delete.order.png"to LargeImage="/img/32/properties.png", ran the program, and changed it back to "/img/32/delete.order.png". Finally the error was gone.

因此,我更改LargeImage="/img/32/delete.order.png"LargeImage="/img/32/properties.png",运行程序,然后将其更改回"/img/32/delete.order.png". 最后错误消失了。

FYI VS2012.3 Win8.1Preview

仅供参考 VS2012.3 Win8.1Preview

回答by Elyas

I had the same issue and to add an image to you solution you have to do it through the wizzard. In the solution explorer -> right click on the appropriate folder-> add existing Item -> and then browse to your image. That worked for me. Hope this helps. Thanks for you answers.

我遇到了同样的问题,要将图像添加到您的解决方案中,您必须通过向导来完成。在解决方案资源管理器中 -> 右键单击​​相应的文件夹 -> 添加现有项目 -> 然后浏览到您的图像。那对我有用。希望这可以帮助。谢谢你的回答。

回答by Kevin

I just ran into this issue as well... I know this is old, but what I had to end up doing was set the images to Resource, and Copy Always... only by browsing my /bin/Debug folder did I realize that the images were not at a valid path location

我也刚遇到这个问题...我知道这很旧,但我最终要做的是将图像设置为资源,并始终复制...只有通过浏览我的 /bin/Debug 文件夹我才意识到图像不在有效的路径位置

回答by Eternal21

I got this exception after moving my Resource Dictionary from root of my application to a subdirectory. In my case the problem were Image paths inside my Style setters inside the dictionary. After I preceded them with a forward slash '/', the application started to work again. If you're having a similar problem, open the resource dictionary, and the error will be highlighted with the blue 'squiggly' line.

将我的资源字典从应用程序的根目录移动到子目录后,我得到了这个异常。在我的情况下,问题是字典中样式设置器中的图像路径。在它们前面加上一个正斜杠“/”之后,应用程序又开始工作了。如果您遇到类似问题,请打开资源字典,错误将用蓝色“波浪线”突出显示。

回答by Amit K.S

In my case, I have added 'WpfToolkit' refrence to my module, and there is no need. After deleting this reference, everything was ok. Strange!

就我而言,我已将“WpfToolkit”引用添加到我的模块中,并且没有必要。删除此引用后,一切正常。奇怪的!

回答by McNos

I got this error because my Command Binding of a Button was wrong:

我收到此错误是因为我的按钮命令绑定错误:

<Button Command="MyCommand" />

instead of

代替

<Button Command="{Binding MyCommand}" />

回答by Will

In my case, I found the mew added icon(image) file is not added into my project. It is resolved after I added these new image files into my project, not just file copy.

就我而言,我发现新添加的图标(图像)文件未添加到我的项目中。在我将这些新的图像文件添加到我的项目中后,它就解决了,而不仅仅是文件复制。