MVVM 轻量级Blend 设计器视图错误:找不到名为“Locator”的资源。

发布于 2024-08-29 05:26:36 字数 119 浏览 4 评论 0原文

该应用程序运行良好,但我无法在设计器视图中看到我的设计。

它说找不到名为“定位器”的资源。显然,我没有更改代码中的任何内容,我只是使用数据绑定对话框进行了数据绑定...

有人面临同样的问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

别在捏我脸啦 2024-09-05 05:26:36

有两种已知的情况可能会发生这种情况。

  • 如果在构建应用程序之前更改为 Blend,则 DLL 尚不可用,并且会出现此错误。构建应用程序可以解决该问题。

  • Expression Blend 中存在一个错误,如果您将一个用户控件放置在另一个用户控件(或 WPF 中的窗口)中,并且内部用户控件使用全局资源,则无法找到该全局资源。在这种情况下,您也会收到错误。

不幸的是,我没有针对第二点的解决方法,因为它是一个 Blend 错误。我希望我们很快就能看到这个问题的解决方案,但它似乎仍然存在于 Blend 4 中。

您可以做的是

  • 在处理外部用户控件时忽略该错误。当您处理内部用户控件时,您应该看到设计时数据很好(我知道不是很令人满意)。

  • 使用 d:DataContext 临时设置 Blend 中的设计时数据上下文。

    使用

希望这有帮助,

劳伦特

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

若无相欠,怎会相见 2024-09-05 05:26:36

我已经为这个问题提出了一个合理可接受的解决方法,因为它似乎没有在 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 分钟的工作找到正确的路径,但是如果您束手无策,请尝试此操作我是!

I've come up with a reasonably acceptable workaround to this problem since it doesn't appear to have been fixed in Blend 4:

In the constructor for your XAML UserControl just add the resources it needs, provided you're in design mode within Blend. This may be just the Locator, or also Styles and Converters as appropriate.

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();

}

There may be some edge cases, but its working OK for me in the simple cases where before I'd get a big red error symbol. I'd LOVE to see suggestions on how to better solve this problem, but this at least allows me to animate user controls that otherwise are appearing as errors.


You could also extract out the creation of resources to 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());
        }
    }

and then in the control do this BEFORE InitializeComponent():

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

Note: At some point in time this stopped working for me and I ended up hardcoding the path to the Styles.xaml because I got frustrated trying to figure out which directory I was in.

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

I'm sure I could find the right path with 5 minutes work, but try this if you're at your wits end like I was!

幻梦 2024-09-05 05:26:36

在 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" 

In MyUserControl.xaml, instead of:

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

use:

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

where "d" has been previously defined as:

xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
青丝拂面 2024-09-05 05:26:36

原因和解决方法在这里解释
http://blogs .msdn.com/b/unnir/archive/2009/03/31/blend-wpf-and-resource-references.aspx

查看帖子的 (b) 部分。

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.

动听の歌 2024-09-05 05:26:36

我在用户控制资源方面也遇到了类似的问题。
我在用户控件 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 资源文件名。

希望这有帮助。

I had a similar problem with a user control resource.
I added this in my usercontrol xaml code:

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

Where GinaControls is the namespace where the control class is declared and /Resources/GinaControlsColors.xaml is the project folder and xaml resource file name.

Hope this helps.

似狗非友 2024-09-05 05:26:36

只需将其添加到 App.xaml.cs 的开头,

这是我的代码

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

public App(){
       main();
}

Just add this in your App.xaml.cs at the very beginning

here's my piece of code

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

public App(){
       main();
}
愚人国度 2024-09-05 05:26:36

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

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

Make sure the Blend has opened the entire solution and NOT just the single project containing the views. I was right-clicking in Visual Studio and selecting Open In Expression Blend. To my surprize, Blend could not find the solution file, so it only opened the single project.

When I realized this, I launched Blend directly, pointed it to the solution file, and then Blend was able to find the ViewModelLocator in my view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文