我知道 this article他们使用以下步骤设置背景颜色。我想知道是否有一种使用图像而不是颜色的方法。我尝试了以下但没有用:
ImageBrush brush = new ImageBrush();
brush.ImageSource =
new BitmapImage(new Uri("/MyApp;component/Images/Backgrounds/myimage.jpg"));
Rootframe.Background = brush;
有没有人看到这是否可能?还是仅限于颜色?
最佳答案
我决定试一试并让它发挥作用。唯一的问题是,它有点变通,因为 ImageOpened
事件似乎有一些奇怪的行为。基本上,当您将背景分配给框架时,不会调用 Brush
的 ImageOpened
事件。奇怪的是,当您将它分配给一个元素时它确实会被调用。所以我只是创建了一个隐藏按钮并将画笔分配给它(以强制触发 ImageOpened
事件)。然后我将它分配给框架,它对我有用。看起来像是一个错误,但下面的解决方法对我有用。
ImageBrush brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri("/myImage.jpg", UriKind.Relative));
//hide the fake button and set the brush to be its background
button1.Visibility = System.Windows.Visibility.Collapsed;
button1.Background = brush;
//assign it to the frame (or using RootFrame in your case)
var frame = App.Current.RootVisual as PhoneApplicationFrame;
frame.Background = brush;
关于c# - 如何为框架设置背景图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8837264/