C# WP8单击按钮时更改背景图像
单击按钮时,我想更改主页中的背景图像。在我的xaml中C# WP8单击按钮时更改背景图像,c#,xaml,windows-phone-8,background-image,C#,Xaml,Windows Phone 8,Background Image,单击按钮时,我想更改主页中的背景图像。在我的xaml中Style=“{StaticResource LayoutGridStyle}用于背景。我如何实现这一点?谢谢!将样式或背景图像(无论您希望如何处理)作为属性包含在主页的ViewModel中。然后绑定到此 Style="{Binding NameOFViewModelProperty} 当您通过单击按钮的代码更改样式时,请确保引发属性更改事件 RaisePropertyChanged("NameOFViewModelProperty");
Style=“{StaticResource LayoutGridStyle}
用于背景。我如何实现这一点?谢谢!将样式或背景图像(无论您希望如何处理)作为属性包含在主页的ViewModel中。然后绑定到此
Style="{Binding NameOFViewModelProperty}
当您通过单击按钮的代码更改样式时,请确保引发属性更改事件
RaisePropertyChanged("NameOFViewModelProperty");
因此,页面知道如何使用更改刷新自身。可以这样做:
public static class FrameworkElementExtensions
{
public static object TryFindResource(this FrameworkElement element, object resourceKey)
{
var currentElement = element;
while (currentElement != null)
{
var resource = currentElement.Resources[resourceKey];
if (resource != null)
{
return resource;
}
currentElement = currentElement.Parent as FrameworkElement;
}
return Application.Current.Resources[resourceKey];
}
}
private void PageTitle_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
ApplicationTitle.Style = (Style)ApplicationTitle.TryFindResource("PhoneTextTitle1Style");
}
给出了样品