如何在Visual Basic中更改vs 2012 Express中图像框的来源 [英] How to change the source of an image-box in vs 2012 express in visual basic
问题描述
我是初学者,所以这看起来像是一个愚蠢的问题。我想在单击按钮时更改图像框内的图像,我不知道如何通过代码更改源代码。我知道,直到image1.source = ...
I am a beginner so this may seem like a stupid question. I want to change the image inside an image-box when a button is clicked, I have no idea how to change the source through code. I know until image1.source=...
如何在我的资产中分配图像来替换图像框中的现有图像?我正在使用Microsoft Visual Studio 2012 for Windows 8.任何帮助都会非常感激,
How do i assign an image within my assets to replace an existing image in the image-box? I am using Microsoft Visual Studio 2012 for windows 8. Any help would be really appreciated,
谢谢你提前
Thanks in advance
推荐答案
这是:
页面:
<Page
x:Class="MSDN.Samples.ButtonChangePictureSample.View.ClickToChangePictureSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ImageToShow}" Width="200" Height="200" Stretch="None" Margin="120,0,20,0" />
<Button Content="{Binding TextToShow}" Command="{Binding ClickCommand}" />
</StackPanel>
</Grid>
</Page>
public ClickToChangePictureSample()
{
InitializeComponent();
DataContext = new MainViewModel();
}
$
视图模型:
public class MainViewModel:ViewModelBase
{
public BitmapSource _myImage1;
public BitmapSource _myImage2;
private string _textToShow;
private BitmapSource _imageToShow;
public MainViewModel()
{
_myImage1 = new BitmapImage(new Uri("ms-appx:///Images/110Banana.png"));
_myImage2 = new BitmapImage(new Uri("ms-appx:///Images/110Lemon.png"));
ClickCommand = new RelayCommand(ChangeIamges);
ChangeIamges();
}
private void ChangeIamges()
{
TextToShow = ImageToShow == _myImage1 ? "Banana" : "Lemon";
ImageToShow = ImageToShow == _myImage1 ? _myImage2 : _myImage1;
}
public string TextToShow
{
get { return _textToShow; }
set { _textToShow = value; RaisePropertyChanged(() => TextToShow); }
}
public BitmapSource ImageToShow
{
get { return _imageToShow; }
set { _imageToShow = value; RaisePropertyChanged(() => ImageToShow); }
}
public ICommand ClickCommand { get; set; }
}
我将尝试使用github公开示例。但是现在你可以看到它。
i will try to expose the sample using github. But for now you can see it.
我在github中提交了样本,请参见: 我的
MSDN.Samples(Win8)
I submitted the sample in my github, see where: My MSDN.Samples (Win8)
更新:抱歉,我在C#中做过示例...但是对于visual basic来说,这个想法是一样的。
UPDATE: Sorry i did the sample in C#... but the idea is the same for visual basic
这篇关于如何在Visual Basic中更改vs 2012 Express中图像框的来源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!