将图像绑定到按钮 [英] Bind an image to a button

查看:119
本文介绍了将图像绑定到按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在按钮(UWP)中显示图像.当我单步执行代码时,将正确创建所有对象,并调用Raise事件以更新XAML页面-但是不会显示任何图像.

在此示例中,TEXT内容设置在第一个按钮上-因此我知道绑定到我的对象正在工作.

带有对资产的硬编码引用的第二个按钮显示图像.

问题是第三个按钮什么也没显示,尽管我可以看到XAML正在调用myImage get.

MainPage.xaml

I''m trying to show an image within a button (UWP). When I step through the code, the objects are all created properly and the Raise event is being called to update the XAML page - however no image is displayed.

In this example the TEXT content is set on the first button - so I know binding to my object is working.

The 2nd button with the hard coded reference to the asset shows an image.

The problem is that the 3rd button shows nothing, though I can see the XAML is calling the myImage get.

MainPage.xaml

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
     <Grid.RowDefinitions>
         <RowDefinition/>
         <RowDefinition/>
         <RowDefinition/>
     </Grid.RowDefinitions>
     <Button Grid.Row="0" Name="Button1" Content="{Binding myText}"/>
     <Button Grid.Row="1" Name="Button2">
         <Image Source="/Assets/windows-sdk.png"/>
     </Button>
     <Button Grid.Row="2" Name="Button3">
         <Image Source="{Binding myImage}"/>
     </Button>
 </Grid>



mainpage.xaml.cs



mainpage.xaml.cs

sealed partial class MainPage : Page
    {

        myViewModel aViewModel;

        public MainPage()
        {
            this.InitializeComponent();

            aViewModel = new myViewModel();

            this.DataContext = aViewModel;
        }
    }



myViewModel.cs



myViewModel.cs

class myViewModel : INotifyPropertyChanged
    {
        BitmapImage anImage;
        string someText;

        public BitmapImage myImage
        {
            get { return anImage; }
        }
        public string myText
        {
            get { return someText; }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void RaiseProperty(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));

        public myViewModel()
        {
            someText = "Some Text";
            RaiseProperty("myText");

            anImage = new BitmapImage(new Uri("ms-appx:/MyAssembly/Assets/windows-sdk.png"));
            RaiseProperty("myImage");
        }
    }



我尝试过的事情:

我尝试返回一个字符串/uri而不是BitmapImage.



What I have tried:

I''ve tried returning a string / uri instead of a BitmapImage.

推荐答案



我不知道Image.Source是否可以与BitmapImage绑定.

但是我是用字符序列做到的.
char []可以与Image.Source绑定.

这是示例.

XAML:
Hi,

I don''t know exactly that Image.Source can bind with BitmapImage.

But I did it using character sequence.
char[] can bind with the Image.Source.

here is the example.

XAML :
<Image
    Source="{Binding ImageSource}"
    />



CS:



CS:

public bool ViewingImage(string subject)
{
   string strPath = @"C:\Users\Public\Pictures\Sample Pictures\";
   strPath += subject;
   strPath += ".bmp";
   ImageSource = LoadImageForTest(strPath);
   return true;
}

private byte[] LoadImageForTest(string selectedFileName)
{
   BitmapImage bitmapImage = new BitmapImage();
   bitmapImage.BeginInit();
   bitmapImage.UriSource = new Uri(selectedFileName);
   bitmapImage.EndInit();

   byte[] data;
   JpegBitmapEncoder encoder = new JpegBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(bitmapImage));
    using (MemoryStream ms = new MemoryStream())
    {
        encoder.Save(ms);
        data = ms.ToArray();
     }

     return data;
}



希望对您有帮助.
如需更多信息,请与我联系.
谢谢



I hope that it will be helpful for you.
If you want more information, please contact me.
Thanks you


终于可以正常工作了..

MainPage.xaml
Finally got this working....

MainPage.xaml
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Button Grid.Row="0" Name="Button1" Content="{Binding myText}"/>
    <Button Grid.Row="1" Name="Button2">
        <Image Source="/Assets/windows-sdk.png"/>
    </Button>
    <Button Grid.Row="2" Name="Button3">
        <Image Source="{Binding myImage}"/>
    </Button>
</Grid>



myViewModel.cs



myViewModel.cs

class myViewModel : INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;
        public void RaiseProperty(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); string someText;

        private BitmapImage _myImage;
        public BitmapImage myImage
        {
            get
            {
                Uri myURI = new Uri("ms-appx:///Assets/windows-sdk.png");
               
                _myImage = new BitmapImage(myURI);

                return _myImage;
            }
        }

        public string myText
        {
            get { return someText; }
        }


        public myViewModel()
        {
            someText = "Some Text";
            RaiseProperty("myText");

            RaiseProperty("myImage");
        }
    }


这篇关于将图像绑定到按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆