Bogus WPF / XAML Errors in Visual Studio 2010

Errors hang around, but at runtime everything works.

Now i get Cannot locate resource 'img/icons/silk/arrow_refresh.png'.

enter image description here

I have a simple UserControl named ImageButton (not all?):

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

What does what you expect does:

[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:

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 do not see the design view of any xaml file that uses ImageButton, for example:

<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?

+3
source share
1 answer

I would suggest using dependency and binding properties when creating a control. The benefits of this will be numerous, for example, you can bind the Image property to ImageButton, and you will not encounter problems such as the one asked in your question.

setter TextBlock Text ImageButton UserControl.


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 Properties Dependency, ,

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, .

,

+2

Source: https://habr.com/ru/post/1795527/


All Articles