将图像绑定到页面背景怎么样?
<Grid x:Name="LayoutRoot" Background="{Binding BackgroundImage}">
<!-- other content for the page -->
</Grid>
然后,您可以随时在 ViewModel 的代码隐藏中更改图像
public class ViewModel
{
private ImageBrush _backgroundImage;
public ImageBrush BackgroundImage
{
get { return _backgroundImage; }
set
{
_backgroundImage = value;
OnPropertyChanged("BackgroundImage");
}
}
// pass the uri of the newly saved image. Even if it's the same location
// firing the PropertyChanged event will make the page get the new image
public void ChangeImage(Uri newImageLocation)
{
BitmapImage source = new BitmapImage(newImageLocation);
BackgroundImage = new ImageBrush { ImageSource = source};
}
}