简体   繁体   English

Visual Studio无可用来源

[英]Visual Studio No Source Available

I am really at a loss here, and have been trying to find a solution to this for several hours. 我真的很茫然,并且一直试图找到解决方案几个小时。 I am lost. 我搞不清楚了。 I get the following exception during an operation that was working the last time I checked. 在我最后一次检查的操作期间,我收到以下异常。

'{DependencyProperty.UnsetValue}' is not a valid value for property 'Foreground'.

It does not take me to where the error is occuring. 我不会把错误发生在哪里。 It takes me to a page that says "No Source Available", and nothing else. 它带我到一个页面,上面写着“No Source Available”,没有别的。 I have tried locating the error by placing breakpoints in various places, but it seems to fail at different points during each run through. 我已经尝试通过在各个地方放置断点来定位错误,但是在每次运行期间它似乎在不同的点处失败。 The InnerException is null. InnerException为null。

I have seen this question , as well as various articles from Google. 我已经看到了这个问题 ,以及谷歌的各种文章。 I cannot figure out what is going on, and I do not know how to troubleshoot from here. 我无法弄清楚发生了什么,我不知道如何从这里解决问题。 The Visual Studio output doesn't appear to give any more detailed information, but I will paste it on request. Visual Studio输出似乎没有提供任何更详细的信息,但我会根据请求粘贴它。 Please, any help is appreciated. 请,任何帮助表示赞赏。

I'm willing to be you have a missing resource. 我愿意你缺少资源。 If you do something like: 如果您执行以下操作:

<Window x:Name="window" x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:WpfApplication4"
        Title="MainWindow" Height="350" Width="525" >
    <Window.Resources>
    <Style TargetType="Button">
        <Setter Property="Foreground" Value="{StaticResource NoSuchResourceKey}" />
    </Style>
    </Window.Resources>
    <StackPanel>
        <Button Content="Click Me" />
    </StackPanel>
</Window>

Then you will get such an exception. 然后你会得到这样的例外。 We can even use a ComponentResourceKey to produce this exception: 我们甚至可以使用ComponentResourceKey来产生这个异常:

<Style TargetType="Button">
    <Setter Property="Foreground" Value="{StaticResource {ComponentResourceKey TypeInTargetAssembly={x:Type FrameworkElement}, ResourceId=NoSuchResourceKey}}" />
</Style>

There are few things here that cause the issue. 这里几乎没有什么能够引发这个问题。 Normally, you would get a compiler error saying a resource doesn't exist when using StaticResource . 通常,在使用StaticResource时,您会收到编译器错误,指出资源不存在。 Such as in this case: 比如在这种情况下:

<Button Content="Click Me" Foreground="{StaticResource NoSuchResourceKey}" />

If instead, we had done: 相反,我们做了:

<Button Content="Click Me" Foreground="{StaticResource {ComponentResourceKey TypeInTargetAssembly={x:Type FrameworkElement}, ResourceId=NoSuchResourceKey}}" />

Then you would get a different exception (XamlParseException), saying: 然后你会得到一个不同的异常(XamlParseException),说:

Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' 提供'System.Windows.StaticResourceExtension'上的值引发异常。 Line number '6' and line position '22'. 行号“6”和行位置“22”。

With an internal exception of: 内部例外:

Cannot find resource named 'TargetType=System.Windows.FrameworkElement ID=NoSuchResourceKey'. 找不到名为'TargetType = System.Windows.FrameworkElement ID = NoSuchResourceKey'的资源。 Resource names are case sensitive. 资源名称区分大小写。

Which all leads us to the real problem (a missing resource). 这一切都将我们引向真正的问题(缺少资源)。 The reason the first two examples don't give us a useful exception, is that we are not setting the Foreground property. 前两个示例没有给我们一个有用的例外的原因是我们没有设置Foreground属性。 We are setting the Value property on a Setter object. 我们在Setter对象上设置Value属性。 So when the resource is not found, DependencyProperty.UnsetValue is used. 因此,当找不到资源时,使用DependencyProperty.UnsetValue Which is perfectly valid for the Setter.Value property. 这对于Setter.Value属性完全有效。

Later, when the Style is applied to the Button we get the exception, because that's when the DependencyProperty.UnsetValue is actually assigned to the Button.Foreground property. 稍后,当Style应用于Button我们得到异常,因为当DependencyProperty.UnsetValue实际分配给Button.Foreground属性时。

To fix this issue, I'd search over your entire solution for Property="Foreground" and look for any instances that use a resource that doesn't exist. 要解决此问题,我将搜索Property="Foreground"整个解决方案,并查找使用不存在的资源的任何实例。

I should add that you don't get an exception when using DynamicResource , because in that value passed to the Button.Foreground property is a "special value" (which allows deferred lookup). 我应该补充说,在使用DynamicResource时不会出现异常,因为传递给Button.Foreground属性的值是一个“特殊值”(允许延迟查找)。 This "special value" won't assign the given property unless the resource is found. 除非找到资源,否则此“特殊值”不会分配给定的属性。

Have you rebuilt your project? 你重建了你的项目吗? Your PDB files may not match your DLLs, so VS may not be loading them. 您的PDB文件可能与您的DLL不匹配,因此VS可能无法加载它们。 As for the error itself, it's likely a binding to a Foreground property is at fault. 至于错误本身,可能是对Foreground属性的绑定有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio 2013 - 源不可用 - Visual Studio 2013 - Source Not Available 没有可用于在Visual Studio 2017中运行测试的源 - no source available to run tests in Visual Studio 2017 “无可用源” - Visual Studio调试(即使已加载符号) - “No Source Available” - Visual Studio Debugging (even when symbols have loaded) 在没有 Visual Studio 的情况下进行调试,但可以使用源文件和 pdb 文件 - Debugging without Visual Studio, but source and pdb files available Visual Studio源代码步进 - visual studio source stepping Visual Studio 2019 | &quot;Microsoft.VisualStudio.Azure.Containers.Tools.Targets&quot; NuGet 包 | “在此来源中不可用” - Visual Studio 2019 | "Microsoft.VisualStudio.Azure.Containers.Tools.Targets" NuGet Package | "Not available in this source" 在ASP.NET MVC中的Visual Studio 2013中调用“全部中断”时,是否可以隐藏“源不可用”选项卡? - Is there a way to hide the “Source Not Available” tab when invoking “Break All” in Visual Studio 2013 in ASP.NET MVC? SelectedIndexChanged事件在Visual Studio中不可用? - SelectedIndexChanged Event not available in Visual Studio? 源控件上的Visual Studio错误 - Visual Studio Error on Source Control Visual Studio 2012中不提供“编辑样式”选项 - “Edit Style” option not available in Visual Studio 2012
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM