简体   繁体   English

图片未显示XAML

[英]Image not displaying XAML

I created my own button that has Icon on the side and text on the other but the problem is the image is not displaying. 我创建了自己的按钮,其侧面带有图标,另一侧带有文本,但是问题是图像未显示。 did i miss something here? 我在这里想念什么吗? any help would be appreciated. 任何帮助,将不胜感激。 TIA TIA

This is the XAML of the control. 这是控件的XAML。

<UserControl x:Name="QButtonControl"
    x:Class="CommonLayout.QButton"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:CommonLayout"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="36"
    d:DesignWidth="145" MinWidth="145" MinHeight="36" Loaded="QButtonControl_Loaded">

    <Grid PointerEntered="Grid_PointerEntered_1" PointerExited="Grid_PointerExited_1" MinWidth="145" MinHeight="36" Background="#FFDCD1D1">
        <TextBlock x:Name="btnLabel" Height="20" Margin="36,8,4,8" TextWrapping="Wrap" Text="Text Here" VerticalAlignment="Center" FontSize="18.667" Width="105"/>
        <Image x:Name="img" HorizontalAlignment="Left" Height="27" Margin="1,4,0,0" VerticalAlignment="Top" Width="29"/>

    </Grid>
</UserControl>

This is code behind the control. 这是控件后面的代码。

public sealed partial class QButton : UserControl
    {
        private ImageSource iconDefault;
        private Brush hoverBrush = new SolidColorBrush(Color.FromArgb(255, 228, 228, 228));

        public string Text
        {
            get
            {
                return btnLabel.Text;
            }
            set
            {
                btnLabel.Text = value;
            }
        }

        public ImageSource Icon
        {
            get
            {
                return iconDefault;
            }
            set
            {
                iconDefault = value;
                img.Source = value;
            }
        }

        public Brush HoverBrush
        {
            get
            {
                return hoverBrush;
            }
            set
            {
                hoverBrush = value;
            }
        }

        public QButton()
        {
            this.InitializeComponent();
        }

        private void Grid_PointerEntered_1(object sender, PointerRoutedEventArgs e)
        {
            btnLabel.Foreground = HoverBrush;
        }

        private void Grid_PointerExited_1(object sender, PointerRoutedEventArgs e)
        {
            btnLabel.Foreground = Foreground;
        }

        private void QButtonControl_Loaded(object sender, RoutedEventArgs e)
        {
            img.Source = iconDefault;       
        }


    }

First of all, do not use a hard coded file path to the image. 首先,请勿使用图像的硬编码文件路径。
Windows Store apps run in a sandbox, so you will not be able to get to any arbitrary file location when you deploy your app. Windows应用商店应用程序在沙箱中运行,因此在部署应用程序时您将无法到达任何文件位置。

Second, you can't use backslashes in Image URI. 其次,您不能在Image URI中使用反斜杠。 The backslashes are the technical reason you are setting the error you are getting. 反斜杠是您设置要获取的错误的技术原因。 But just changing to forward slashes in not the answer. 但是,只是改用正斜杠不能解决问题。

Access Image in XAML 在XAML中访问图像

If you add an image to your projects /Assets folder, you can use XAML like this to show it in QButton. 如果将图像添加到项目/Assets文件夹中,则可以使用XAML这样在QButton中显示它。

<local:QButton x:Name='qButton1'
               Icon='/assets/jellyfish.jpg'  />

In Code 在代码中

To change the Icon in code. 更改代码中的图标。

public MainPage()
{
  this.InitializeComponent();
  this.Loaded += MainPage_Loaded;

}

private async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
  var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/shrimp.jpg"));
  var fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

  var image = new BitmapImage();

  image.SetSource(fileStream);

  qButton1.Icon = image;
    }

If you want the user to choose the Image from their computer at runtime, look at the File Pickers. 如果希望用户在运行时从其计算机中选择图像,请查看“文件选择器”。

MSDN file pickers MSDN文件选取器

Are you forgetting to set the Icon property? 您是否忘记设置Icon属性?

This worked for me 这对我有用

<local:QButton Icon="C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg" />

This worked as well 这也很好

public QButton()
{
    this.InitializeComponent();

    Uri imageUri = new Uri(@"C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg");
    BitmapImage image = new BitmapImage(imageUri);
    this.Icon = image;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM