wpf 将 VS2010 项目加载到 Expression Blend 中时,属性无法识别或无法访问错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10146250/
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
Property not recognized or not accessible error when loading VS2010 project into Expression Blend
提问by Brian Leeming
I have a project in VS2010 that uses XAML and now I need to load it into Expression Blend 4. The project builds and runs in VS2010 and this is the first time it has been loaded into Blend. It DOES build and run in Blend even though the members are not recognized.
我在 VS2010 中有一个使用 XAML 的项目,现在我需要将它加载到 Expression Blend 4 中。该项目在 VS2010 中构建和运行,这是它第一次加载到 Blend 中。即使无法识别成员,它也会在 Blend 中构建和运行。
Why is the Scale property not recognized and why does it show up as an error when it functionally works?
为什么无法识别 Scale 属性,为什么它在正常工作时显示为错误?
EDITAlthough this builds and runs, the XAML is not displayed graphically in Blend and so cannot be modified by a non-technical user.
编辑虽然这会生成并运行,但 XAML 不会在 Blend 中以图形方式显示,因此非技术用户无法修改。
In a number of the .xaml files that contain references to usercontrols there is an attribute that is not recognized by Blend with the error:
在许多包含对用户控件的引用的 .xaml 文件中,Blend 无法识别一个属性并显示错误:
The member "XXXX" is not recognized or is not accessible
The property exists in the .cs code behind file and in each case the error message is the same.
该属性存在于 .cs 代码隐藏文件中,在每种情况下,错误消息都是相同的。
I've looked at a lot of possible answers to this on the internet but none of them is a solution. The referenced items are not read-only. The various classes and properties are Public. I've also added the following WPF reference into the .csproj file, which was missing.
我在互联网上查看了很多可能的答案,但没有一个是解决方案。引用的项目不是只读的。各种类和属性都是公共的。我还在缺少的 .csproj 文件中添加了以下 WPF 引用。
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
In the following code, the Scale attribute is not recognized even though it exists as a property in the user control.
在以下代码中,即使 Scale 特性作为属性存在于用户控件中,也无法识别。
Here is the UserControl in MyLogo.xaml:
这是 MyLogo.xaml 中的 UserControl:
<UserControl x:Class="NamespaceX.NamespaceY.UI.Shapes.MyLogo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="132" Width="105">
<Canvas>
<Canvas.LayoutTransform>
<ScaleTransform x:Name="st" CenterX="0" CenterY="0" />
</Canvas.LayoutTransform>
<Image Source="/Client;component/Images/MyLogo.png"/>
</Canvas>
Here is the code behind in MyLogo.xaml.cs:
这是 MyLogo.xaml.cs 中的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace NamespaceX.NamespaceY.UI.Shapes
{
/// <summary>
/// Interaction logic for MyLogo.xaml
/// </summary>
public partial class MyLogo : UserControl
{
public double Scale
{
get
{
return st.ScaleX;
}
set
{
st.ScaleX = value;
st.ScaleY = value;
}
}
public MyLogo()
{
InitializeComponent();
}
}
}
In my Navigation.xaml file I have this:
在我的 Navigation.xaml 文件中,我有这个:
<UserControl x:Class="NamespaceX.NamespaceY.UI.UserControls.Navigation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:shape="clr-namespace:NamespaceX.NamespaceY.UI.Shapes"
Height="185" Width="1280" Loaded="UserControl_Loaded">
<FrameworkElement.Resources>
<ResourceDictionary Source="../Resources/Main.xaml" />
</FrameworkElement.Resources>
<Canvas>
<shape:MyLogo Scale="1.2" Height="181.483" Canvas.Left="38" Canvas.Top="4" Width="188" />
<StackPanel Canvas.Left="205" Canvas.Top="-2" Width="1062">
</StackPanel>
</Canvas>
回答by Brian Leeming
Here's the solution. In the application's .csproj file, change this:
这是解决方案。在应用程序的 .csproj 文件中,更改以下内容:
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
to this:
对此:
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Do not be fooled by the fact that Visual Studio reports that you are running in AnyCPU more in the configuration manager. You must hand-edit the .csproj file.
不要被 Visual Studio 报告您在配置管理器中更多地在 AnyCPU 中运行的事实所迷惑。您必须手动编辑 .csproj 文件。
回答by AMissico
They are related in the fact that changing the platform changes the cached assemblies. The accepted answer is not acceptable.
它们与更改平台会更改缓存的程序集这一事实有关。接受的答案是不可接受的。
This is what I recommend:
这是我推荐的:
- Close all files
- Clean the solution
- Rebuild the solution
- 关闭所有文件
- 清洁溶液
- 重建解决方案
The affected XAML should now be clear of build errors.
受影响的 XAML 现在应该没有构建错误。
This works for me, your results may vary.
这对我有用,您的结果可能会有所不同。
回答by Nohim Ys
Changing the build properties of the project to 'Any CPU' solved the problem for me. I still can't understand how that is related to the problem. But of course it did the magic.
将项目的构建属性更改为“任何 CPU”为我解决了这个问题。我仍然无法理解这与问题的关系。但它当然发挥了神奇的作用。
回答by robstel
As others have said, changing the target from x64 to AnyCPU fixes the errors in the designer, bizarre as this seems.
正如其他人所说,将目标从 x64 更改为 AnyCPU 修复了设计器中的错误,这看起来很奇怪。
I found the following on the Visual Studio Forum:
我在 Visual Studio 论坛上找到了以下内容:
(Q) Design view is unavailable for x64 and ARM target platforms when the document contains custom elements (XAML with a user control, for example).
(Q) 当文档包含自定义元素(例如,带有用户控件的 XAML)时,设计视图对于 x64 和 ARM 目标平台不可用。
(A) Starting with Visual Studio 2015, we have enabled you to design/author your XAML even when you target anything but x86. Visual Studio 2015 Update 2 should bring together several fixes and changes to have this experience work even better.
(A) 从 Visual Studio 2015 开始,我们使您能够设计/创作 XAML,即使您的目标不是 x86。Visual Studio 2015 更新 2 应该汇集了几个修复和更改,以使这种体验工作得更好。
However, it is worth noting that we will not be able to execute any code you might have written in the designer when the project is targeting anything but x86. This is because of the restriction that an x86 process (which the XAML designer is) cannot run ARM or x64 code. You should be able to view and edit all of the XAML on the page, with any custom types replaced with replacements to preserve the WYSIWYG experience as much as possible (and switch to x86 when you really care about running the code in the designer).
但是,值得注意的是,当项目针对 x86 以外的任何对象时,我们将无法执行您可能在设计器中编写的任何代码。这是因为 x86 进程(XAML 设计器是)不能运行 ARM 或 x64 代码的限制。您应该能够查看和编辑页面上的所有 XAML,将任何自定义类型替换为替换项,以尽可能保留 WYSIWYG 体验(并在您真正关心在设计器中运行代码时切换到 x86)。
回答by user3227623
I had also the same exception. My problem was, that the Type of the property was in an assembly, which was not referenced in the Project, where I use the UserControl.
我也有同样的例外。我的问题是,属性的类型在一个程序集中,在我使用 UserControl 的项目中没有引用。
回答by Irving r
I had the same problem on Silverlight turns out it was the namespace I had on my user control was too long, I just made it shorter and it works. Hope it helps!
我在 Silverlight 上遇到了同样的问题,结果是我在用户控件上的命名空间太长,我只是把它缩短了,它可以工作。希望能帮助到你!
回答by oldDavid
I was able to reproduce / fix this problem consistently with a vb.net project in VS2012. In Project ... Properties ... Compile ... Target CPU is set to AnyCPU (default). Everything works fine.
我能够在 VS2012 中使用 vb.net 项目一致地重现/修复这个问题。在 Project ... Properties ... Compile ... Target CPU 中设置为 AnyCPU(默认)。一切正常。
Change the Target CPU to x64, Save and Build
将目标 CPU 更改为 x64,保存并构建
Close and reopen the solution.
关闭并重新打开解决方案。
You now get the "The member "XXXX" is not recognized or is not accessible" error
您现在收到“无法识别或无法访问成员“XXXX”错误
Change the Target CPU back to Any CPU, Save and Build
将目标 CPU 改回 Any CPU,保存并构建
The window now displays properly.
该窗口现在可以正确显示。