Visual Studio 2010 中的虚假 WPF/XAML 错误

发布于 2024-10-19 21:42:34 字数 2533 浏览 2 评论 0原文

虽然存在错误,但在运行时一切正常。

现在,我得到无法找到资源'img/icons/silk/arrow_refresh.png'。

“在此处输入图像描述”

我有一个名为 ImageButton 的简单 UserControl(不是每个人都这样吗?):

<UserControl x:Class="MyProj.src.controls.ImageButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d">
    <Button Name="btnButton" Click="btnButton_Click">
        <StackPanel Orientation="Horizontal">
            <Image Name="btnImage" Stretch="None" />
            <TextBlock Name="btnText" />
        </StackPanel>
    </Button>
</UserControl>

它符合您的期望:

[ContentProperty("Text")]
public partial class ImageButton : UserControl
{
    public String Image { set { btnImage.Source = GuiUtil.CreateBitmapImage(value); } }
    public String Text { set { btnText.Text = value; } }
    public double Gap { set { btnImage.Margin = new Thickness(0, 0, value, 0); } }
    public bool ToolBarStyle { set { if (value) { 
        btnButton.Style = (Style)FindResource(ToolBar.ButtonStyleKey); } }
    }
    public bool IsCancel { set { btnButton.IsCancel = value; } }
    public bool IsDefault { set { btnButton.IsDefault = value; } }

    public event RoutedEventHandler Click;

    public ImageButton()
    {
        InitializeComponent();
    }

    private void btnButton_Click(object sender, RoutedEventArgs e)
    {
        if (Click != null)
        {
            Click(sender, e);
        }
    }
}

其中 CreateBitmapImage 如下:

public static BitmapImage CreateBitmapImage(string imagePath)
{
    BitmapImage icon = new BitmapImage();
    icon.BeginInit();
    icon.UriSource = new Uri(String.Format("pack://application:,,,/{0}", imagePath));
    icon.EndInit();
    return icon;
}

我看不到任何 xaml 文件的设计视图使用这样的 ImageButton:

<Window
    foo="bar"
    xmlns:wpfControl="clr-namespace:MyProj.src.controls">
    <Grid>
        <wpfControl:ImageButton ToolBarStyle="True" Gap="3" 
            Click="btnRefresh_Click" Text="Refresh"
            Image="img/icons/silk/arrow_refresh.png" />
    </Grid>
</Window>

Why is VS 抱怨?

There are errors hanging around, but at runtime everything works.

Right now, I'm getting Cannot locate resource 'img/icons/silk/arrow_refresh.png'.

enter image description here

I've got a simple UserControl called ImageButton (doesn't everyone?):

<UserControl x:Class="MyProj.src.controls.ImageButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d">
    <Button Name="btnButton" Click="btnButton_Click">
        <StackPanel Orientation="Horizontal">
            <Image Name="btnImage" Stretch="None" />
            <TextBlock Name="btnText" />
        </StackPanel>
    </Button>
</UserControl>

Which does what you'd expect:

[ContentProperty("Text")]
public partial class ImageButton : UserControl
{
    public String Image { set { btnImage.Source = GuiUtil.CreateBitmapImage(value); } }
    public String Text { set { btnText.Text = value; } }
    public double Gap { set { btnImage.Margin = new Thickness(0, 0, value, 0); } }
    public bool ToolBarStyle { set { if (value) { 
        btnButton.Style = (Style)FindResource(ToolBar.ButtonStyleKey); } }
    }
    public bool IsCancel { set { btnButton.IsCancel = value; } }
    public bool IsDefault { set { btnButton.IsDefault = value; } }

    public event RoutedEventHandler Click;

    public ImageButton()
    {
        InitializeComponent();
    }

    private void btnButton_Click(object sender, RoutedEventArgs e)
    {
        if (Click != null)
        {
            Click(sender, e);
        }
    }
}

Where CreateBitmapImage is the following:

public static BitmapImage CreateBitmapImage(string imagePath)
{
    BitmapImage icon = new BitmapImage();
    icon.BeginInit();
    icon.UriSource = new Uri(String.Format("pack://application:,,,/{0}", imagePath));
    icon.EndInit();
    return icon;
}

I can't see the design view of any xaml file that uses an ImageButton like such:

<Window
    foo="bar"
    xmlns:wpfControl="clr-namespace:MyProj.src.controls">
    <Grid>
        <wpfControl:ImageButton ToolBarStyle="True" Gap="3" 
            Click="btnRefresh_Click" Text="Refresh"
            Image="img/icons/silk/arrow_refresh.png" />
    </Grid>
</Window>

Why is VS complaining?

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

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

发布评论

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

评论(1

毁我热情 2024-10-26 21:42:34

我建议在创建控件时使用依赖属性和绑定。这样做的好处有很多,例如,您将能够绑定 ImageButton 的 Image 属性,并且不会遇到像您问题中那样的问题。

您可以将它们绑定到 ImageButton UserControl 的属性,而不是使用属性中的设置器来设置图像源和 TextBlock 文本。

更新
我在这里用 ImageButton 组合了一个小示例项目:http://www.mediafire。 com/?vlk4m7svttyjd6t

你的 Xaml 看起来像这样

<UserControl ...
             x:Name="imageButton">
    <Button...>
        <StackPanel Orientation="Horizontal">
            <Image Source="{Binding ElementName=imageButton, Path=Image}" .../>
            <TextBlock Text="{Binding ElementName=imageButton, Path=Text}" .../>
        </StackPanel>
    </Button>
</UserControl>

然后你用依赖属性替换 CLR 属性,如下代码后面的代码

public partial class ImageButton : UserControl
{
    public static readonly DependencyProperty ImageProperty =
        DependencyProperty.Register("Image",
            typeof(ImageSource),
            typeof(ImageButton),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
    public ImageSource Image
    {
        get { return (ImageSource)base.GetValue(ImageProperty); }
        set { base.SetValue(ImageProperty, value); }
    }

    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text",
            typeof(string),
            typeof(ImageButton),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
    public string Text
    {
        get { return (string)base.GetValue(TextProperty); }
        set { base.SetValue(TextProperty, value); }
    }
    //...
}

对于 Gap 属性,你可以使用转换器绑定边距或使用 PropertyChangedCallback 将其设置在代码后面。

希望有帮助

I would suggest using Dependency Properties and Bindings when creating a Control. The benefits of this will be many, for example you'll be able to Bind the Image property for the ImageButton and you won't run in to problems like the one in your question.

Instead of using the setter in the Properties to set the Image source and TextBlock Text, you can bind them to the properties for the ImageButton UserControl.

Update
I put together a small sample project with ImageButton here: http://www.mediafire.com/?vlk4m7svttyjd6t

Your Xaml can look like this

<UserControl ...
             x:Name="imageButton">
    <Button...>
        <StackPanel Orientation="Horizontal">
            <Image Source="{Binding ElementName=imageButton, Path=Image}" .../>
            <TextBlock Text="{Binding ElementName=imageButton, Path=Text}" .../>
        </StackPanel>
    </Button>
</UserControl>

And then you replace the CLR Properties with Dependency properties like below in code behind

public partial class ImageButton : UserControl
{
    public static readonly DependencyProperty ImageProperty =
        DependencyProperty.Register("Image",
            typeof(ImageSource),
            typeof(ImageButton),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
    public ImageSource Image
    {
        get { return (ImageSource)base.GetValue(ImageProperty); }
        set { base.SetValue(ImageProperty, value); }
    }

    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text",
            typeof(string),
            typeof(ImageButton),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
    public string Text
    {
        get { return (string)base.GetValue(TextProperty); }
        set { base.SetValue(TextProperty, value); }
    }
    //...
}

For the Gap property you can either Bind Margin with a Converter or use the PropertyChangedCallback to set it in code behind.

Hope that helps

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