c# - 如何为框架设置背景图像?

标签 c# windows-phone-7

我知道 this article他们使用以下步骤设置背景颜色。我想知道是否有一种使用图像而不是颜色的方法。我尝试了以下但没有用:

ImageBrush brush = new ImageBrush();
brush.ImageSource = 
new BitmapImage(new Uri("/MyApp;component/Images/Backgrounds/myimage.jpg"));
Rootframe.Background = brush;

有没有人看到这是否可能?还是仅限于颜色?

最佳答案

我决定试一试并让它发挥作用。唯一的问题是,它有点变通,因为 ImageOpened 事件似乎有一些奇怪的行为。基本上,当您将背景分配给框架时,不会调用 BrushImageOpened 事件。奇怪的是,当您将它分配给一个元素时它确实会被调用。所以我只是创建了一个隐藏按钮并将画笔分配给它(以强制触发 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/

相关文章:

c# - 用于检查 x 是否以 y 开头的字符串的 IComparer

c# - Bing路由服务故障exception1错误

css - 位置 :fixed in Windows Phone 7

c# - Windows Phone 7 中的 HttpUtility.UrlEncode?

c# - 在 C# 中使用 Windows 窗体创建多个窗体?

c# - 递归 linq 和这个?

silverlight - 如何根据当前主题更改应用程序栏中的图标?

windows-phone-7 - Windows Phone : how to disable touch scrolling in ScrollViewer (Listbox)?

xaml - 如何从 "tap"中删除 “Group Header” 事件并手动调用 “Jump List” 中的 “Long List Selector” ?

c# - WCF 服务和对象构造函数