17

The application runs fine but i could not see my design in the designer view.

It says Cannot find resource named 'Locator'. Obviously, i did not change anything in the code, i just did the data binding using the data binding dialog...

anyone facing the same problem?

4

7 回答 7

8

There are two known occurrences where this can happen.

  • If you change to Blend before you built the application, the DLLs are not available yet and this error can be seen. Building the application solves the issue.

  • There is a bug in Expression Blend where, if you are placing a user control in another user control (or Window in WPF), and the inner user control uses a global resource, the global resource cannot be found. In that case you will get the error too.

Unfortunately I do not have a workaround for the second point, as it is a Blend bug. I hope we will see a resolution for that soon, but it seems to be still there in Blend 4.

What you can do is

  • Ignore the error when working on the outer user control. When you work on the inner user control, you should see the design time data fine (not very satisfying I know).

  • Use the d:DataContext to set the design time data context in Blend temporarily.

Hopefully this helps,

Laurent

于 2010-04-21T11:35:21.653 回答
7

我想出了一个合理可接受的解决方法来解决这个问题,因为它似乎没有在 Blend 4 中得到修复:

在 XAML UserControl 的构造函数中,只需添加它需要的资源,前提是您在 Blend 中处于设计模式。这可能只是定位器,也可能是适当的样式和转换器。

public partial class OrdersControl : UserControl
{
    public OrdersControl()
    {
        //  MUST do this BEFORE InitializeComponent()
        if (DesignerProperties.GetIsInDesignMode(this))
        {
             if (AppDomain.CurrentDomain.BaseDirectory.Contains("Blend 4"))
            {
                // load styles resources
                ResourceDictionary rd = new ResourceDictionary();
                rd.Source = new Uri(System.IO.Path.Combine(Environment.CurrentDirectory, "Resources/Styles.xaml"), UriKind.Absolute);
                Resources.MergedDictionaries.Add(rd);

                // load any other resources this control needs such as Converters
                Resources.Add("booleanNOTConverter", new BooleanNOTConverter());
            }
        }

        // initialize component
        this.InitializeComponent();

}

可能会有一些边缘情况,但在我得到一个大红色错误符号之前的简单情况下,它对我来说工作正常。我很想看到关于如何更好地解决这个问题的建议,但这至少允许我为用户控件设置动画,否则这些控件会显示为错误。


您还可以提取资源的创建App.xaml.cs

    internal static void CreateStaticResourcesForDesigner(Control element)
    {
        if (AppDomain.CurrentDomain.BaseDirectory.Contains("Blend 4"))
        {
            // load styles resources
            ResourceDictionary rd = new ResourceDictionary();
            rd.Source = new Uri(System.IO.Path.Combine(Environment.CurrentDirectory, "Resources/Styles.xaml"), UriKind.Absolute);
            element.Resources.MergedDictionaries.Add(rd);

            // load any other resources this control needs
            element.Resources.Add("booleanNOTConverter", new BooleanNOTConverter());
        }
    }

然后在控件中在 InitializeComponent() 之前执行此操作:

     // create local resources
     if (DesignerProperties.GetIsInDesignMode(this))
     {
         App.CreateStaticResourcesForDesigner(this);
     }

注意:在某个时间点,这对我不起作用,我最终对 Styles.xaml 的路径进行了硬编码,因为我试图弄清楚我在哪个目录中感到沮丧。

rd.Source = new Uri(@"R:\TFS-PROJECTS\ProjectWPF\Resources\Styles.xaml", UriKind.Absolute);

我确信我可以在 5 分钟的工作中找到正确的路径,但如果你像我一样无所适从,试试这个吧!

于 2010-08-26T02:45:50.650 回答
5

在 MyUserControl.xaml 中,而不是:

DataContext="{Binding Main, Source={StaticResource Locator}

利用:

d:DataContext="{Binding Main, Source={StaticResource Locator}

其中“d”先前已定义为:

xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
于 2012-03-20T17:31:00.923 回答
1

The reason and workaround explained here http://blogs.msdn.com/b/unnir/archive/2009/03/31/blend-wpf-and-resource-references.aspx

Look at (b) part of the post.

于 2011-09-27T18:19:31.960 回答
1

我在用户控制资源方面遇到了类似的问题。
我在我的 usercontrol xaml 代码中添加了这个:

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/GinaControls;component/Resources/GinaControlsColors.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

哪里GinaControls是声明控件类的命名空间,/Resources/GinaControlsColors.xaml是项目文件夹和xaml资源文件名。

希望这可以帮助。

于 2012-01-15T15:38:32.693 回答
1

只需在一开始就将其添加到您的 App.xaml.cs 中

这是我的一段代码

[STATThread()]
static void main(){
       App.Current.Resources.Add("Locator", new yournamespace.ViewModel.ViewModelLocator());
}

public App(){
       main();
}
于 2012-09-20T08:07:21.307 回答
0

确保 Blend 已打开整个解决方案,而不仅仅是包含视图的单个项目。我在 Visual Studio 中右键单击并选择在 Expression Blend 中打开。令我惊讶的是,Blend 找不到解决方案文件,所以它只打开了单个项目。

当我意识到这一点时,我直接启动了 Blend,将其指向解决方案文件,然后 Blend 能够在我的视图中找到 ViewModelLocator。

于 2011-08-11T23:47:36.810 回答